NSString+YOHO.m
13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
//
// NSString+YOHO.m
// YH_Mall
//
// Created by 张同海 on 15/5/28.
// Copyright (c) 2015年 YOHO. All rights reserved.
//
#import "NSString+YOHO.h"
#import <CommonCrypto/CommonDigest.h>
#import "JTSHardwareInfo.h"
#import "OpenUDID.h"
#import <AdSupport/AdSupport.h>
@implementation NSString (YOHO)
- (NSString *)yh_md5String
{
const char *cStr = [self UTF8String];
unsigned char result[16];
CC_MD5( cStr, (CC_LONG)strlen(cStr), result); // This is the md5 call
return [NSString stringWithFormat:
@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
];
}
- (CGFloat)yh_priceValue
{
return [[self stringByReplacingOccurrencesOfString:@"," withString:@""] floatValue];
}
- (BOOL)yh_isEmptyAfterTrimmingWhitespaceAndNewlineCharacters
{
return [[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0;
}
- (NSString *)yh_stringByTrimmingWhitespaceAndNewlineCharacters
{
return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
- (NSString *)URLEncodedStringWithCFStringEncoding:(CFStringEncoding)encoding
{
return CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (__bridge CFStringRef)self, NULL, CFSTR("=,!$&'()*+;@?\n\"<>#\t :/"), encoding));
}
- (NSString *)yh_URLEncodedString
{
return [self URLEncodedStringWithCFStringEncoding:kCFStringEncodingUTF8];
}
+ (NSString *)stringWithDate:(NSDate *)date dateFormat:(NSString *)format
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSString *dateFormat = nil;
if (format == nil) {
dateFormat = @"y.MM.dd HH: mm: ss";
} else {
dateFormat = format;
}
formatter.dateFormat = dateFormat;
NSString *dateString = [formatter stringFromDate:date];
return dateString;
}
// 长度是否在一个范围之内
- (BOOL)yh_isLenghGreaterThanOrEqual:(NSInteger)minimum lessThanOrEqual:(NSInteger)maximum
{
return ([self length] >= minimum) && ([self length] <= maximum);
}
- (NSRange)yh_firstRangeOfURLSubstringi
{
static NSDataDetector *dataDetector = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dataDetector = [NSDataDetector dataDetectorWithTypes:(NSTextCheckingTypeLink | NSTextCheckingTypeLink)
error:nil];
});
NSRange range = [dataDetector rangeOfFirstMatchInString:self
options:0
range:NSMakeRange(0, [self length])];
return range;
}
- (NSString *)yh_firstURLSubstring
{
NSRange range = [self yh_firstRangeOfURLSubstringi];
if (range.location == NSNotFound) {
return nil;
}
return [self substringWithRange:range];
}
- (NSArray *)yh_URLSubstrings
{
static NSDataDetector *dataDetector = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dataDetector = [NSDataDetector dataDetectorWithTypes:(NSTextCheckingTypeLink | NSTextCheckingTypeLink)
error:nil];
});
NSArray *matches = [dataDetector matchesInString:self
options:0
range:NSMakeRange(0, [self length])];
NSMutableArray *substrings = [NSMutableArray arrayWithCapacity:[matches count]];
for (NSTextCheckingResult *result in matches) {
[substrings addObject:[result.URL absoluteString]];
}
return [NSArray arrayWithArray:substrings];
}
- (NSString *)yh_firstMatchUsingRegularExpression:(NSRegularExpression *)regularExpression
{
NSRange range = [regularExpression rangeOfFirstMatchInString:self
options:0
range:NSMakeRange(0, [self length])];
if (range.location == NSNotFound) {
return nil;
}
return [self substringWithRange:range];
}
- (NSString *)yh_firstMatchUsingRegularExpressionPattern:(NSString *)regularExpressionPattern
{
NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:regularExpressionPattern
options:NSRegularExpressionCaseInsensitive
error:nil];
return [self yh_firstMatchUsingRegularExpression:regularExpression];
}
- (BOOL)yh_matchesRegularExpressionPattern:(NSString *)regularExpressionPattern
{
NSRange fullRange = NSMakeRange(0, [self length]);
NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:regularExpressionPattern
options:NSRegularExpressionCaseInsensitive
error:nil];
NSRange range = [regularExpression rangeOfFirstMatchInString:self
options:0
range:fullRange];
if (NSEqualRanges(fullRange, range)) {
return YES;
}
return NO;
}
- (NSRange)yh_rangeOfFirstMatchUsingRegularExpressionPattern:(NSString *)regularExpressionPattern
{
NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:regularExpressionPattern
options:NSRegularExpressionCaseInsensitive
error:nil];
NSRange range = [regularExpression rangeOfFirstMatchInString:self
options:0
range:NSMakeRange(0, [self length])];
return range;
}
- (NSString *)yh_stringByReplacingMatchesUsingRegularExpressionPattern:(NSString *)regularExpressionPattern withTemplate:(NSString *)templ
{
NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:regularExpressionPattern
options:NSRegularExpressionCaseInsensitive
error:nil];
NSString *string = [regularExpression stringByReplacingMatchesInString:self
options:0
range:NSMakeRange(0, [self length])
withTemplate:templ];
return string;
}
- (NSDictionary *)yh_URLParameters
{
NSString *urlString = [self stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSRange rangeOfQuestionMark = [urlString rangeOfString:@"?" options:NSBackwardsSearch];
if (rangeOfQuestionMark.location == NSNotFound) {
return nil;
}
NSString *parametersString = [urlString substringFromIndex:(rangeOfQuestionMark.location + 1)];
NSArray *pairs = [parametersString componentsSeparatedByString:@"&"];
NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithCapacity:[pairs count]];
for (NSString *aPair in pairs) {
NSArray *keyAndValue = [aPair componentsSeparatedByString:@"="];
if ([keyAndValue count] == 2) {
[parameters setObject:keyAndValue[1] forKey:keyAndValue[0]];
}
}
return parameters;
}
- (BOOL)yh_isPureDigital
{
NSScanner* scan = [NSScanner scannerWithString:self];
long long val;
return [scan scanLongLong:&val] && [scan isAtEnd];
}
- (NSString *)yh_splitUrlWithWidth:(NSString *)width height:(NSString *)height mode:(NSString *)mode
{
return [[[[self stringByReplacingOccurrencesOfString:@"{width}" withString:[NSString stringWithFormat:@"%zd",width.integerValue]] stringByReplacingOccurrencesOfString:@"{height}" withString:[NSString stringWithFormat:@"%zd",height.integerValue]] stringByReplacingOccurrencesOfString:@"{mode}" withString:mode] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
- (NSString *)yh_splitUrlWithWidth:(NSString *)width height:(NSString *)height
{
return [self yh_splitUrlWithWidth:width height:height mode:@"2"];
}
- (NSMutableAttributedString *)yh_attributedStringWithLineSpacing:(CGFloat)lineSpacing lineBreakMode:(NSLineBreakMode)breakMode textAlignment:(NSTextAlignment)alignment
{
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:self];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:lineSpacing];
[paragraphStyle setLineBreakMode:breakMode];
[paragraphStyle setAlignment:alignment];
[attributedStr addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0, [self length])];
return attributedStr;
}
-(NSMutableAttributedString *)yh_attributedStringFromStingWithFont:(UIFont *)font withLineSpacing:(CGFloat)lineSpacing withLineBreakMode:(NSLineBreakMode)breakMode
{
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:self attributes:@{NSFontAttributeName:font}];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:lineSpacing];
[paragraphStyle setLineBreakMode:breakMode];
[attributedStr addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0, [self length])];
return attributedStr;
}
-(NSMutableAttributedString *)yh_attributedStringFromStingWithFont:(UIFont *)font lineSpacing:(CGFloat)lineSpacing lineBreakMode:(NSLineBreakMode)breakMode alignment:(NSTextAlignment)alignment
{
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:self attributes:@{NSFontAttributeName:font}];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:lineSpacing];
[paragraphStyle setLineBreakMode:breakMode];
[paragraphStyle setAlignment:alignment];
[attributedStr addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0, [self length])];
return attributedStr;
}
-(CGSize)yh_boundingRectWithSize:(CGSize)size withTextFont:(UIFont *)font withLineSpacing:(CGFloat)lineSpacing withLineBreakMode:(NSLineBreakMode)breakMode
{
NSMutableAttributedString *attributedText = [self yh_attributedStringFromStingWithFont:font
withLineSpacing:lineSpacing withLineBreakMode:breakMode];
CGSize textSize = CGSizeZero;
if (attributedText.length > 0) {
textSize = [attributedText boundingRectWithSize:size
options:NSStringDrawingUsesLineFragmentOrigin
context:nil].size;
}
return textSize;
}
- (CGFloat)yh_heightWithFont:(UIFont *)font constrainedToWidth:(CGFloat)width
{
return ceilf([self boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil].size.height);
}
- (NSString *)yh_floatString
{
if ([self isKindOfClass:[NSString class]]) {
CGFloat tmp = [self floatValue];
NSString *str = [NSString stringWithFormat:@"%0.2f",tmp];
return str;
} else {
return @"";
}
}
- (NSString *)yh_anonymousName
{
if(self.length == 0) {
return self;
}
NSString *username = self;
if ([username length] == 1||[username length] == 2) {
NSRange range = NSMakeRange([username length]-1, 1);
username = [username stringByReplacingCharactersInRange:range withString:@"*"];
return username;
} else {
NSRange range = NSMakeRange(1, username.length-2);
username = [username stringByReplacingCharactersInRange:range withString:@"***"];
}
return username;
}
- (NSString *)yh_stringByRemoveRMBSymbol
{
return [[self stringByReplacingOccurrencesOfString:@"¥" withString:@""] stringByReplacingOccurrencesOfString:@"¥" withString:@""];
}
- (NSString *)yh_anonymousMoblie
{
if(self.length == 0) {
return self;
}
NSString *anonymousStr = self;
for (int i = 0; i < anonymousStr.length; i ++) {
if (anonymousStr.length < 3) {
break;
}else{
if (i >=3 && i < 7) {
NSRange range = NSMakeRange(i, 1);
anonymousStr = [anonymousStr stringByReplacingCharactersInRange:range withString:@"*"];
}
}
}
return anonymousStr;
}
@end