版本記錄
版本號 | 時(shí)間 |
---|---|
V1.0 | 2017.12.16 |
前言
NSString是我們經(jīng)常使用的一個(gè)類绞蹦,但是蘋果的API很多用起來不是那么方便力奋,需要根據(jù)我們特殊需求進(jìn)行個(gè)性化的改變和定制,接下來坦辟,我就寫一個(gè)NSString分類工具刊侯,以后在使用的時(shí)候直接調(diào)用這個(gè)分類中的方法,可以實(shí)現(xiàn)很多小的功能锉走,用起來也很方便滨彻。相關(guān)代碼已經(jīng)上傳至GitHub - 刀客傳奇。感興趣的可以看我上面寫的幾篇挪蹭。
1. 一個(gè)實(shí)用的NSString分類工具 (一)—— 玩轉(zhuǎn)數(shù)字
獲取拼音首字母
獲取拼音的首字母亭饵,看著簡單好像取第一個(gè)字符就可以,但是要考慮很多情況梁厉,比如說聲調(diào)正好在首位怎么辦辜羊?下面我們看一下代碼。
//獲取拼音的首字母
- (NSString *)jj_gainPinYinFirstCharacter;
//獲取拼音的首字母
- (NSString *)jj_gainPinYinFirstCharacter
{
//轉(zhuǎn)成了可變字符串
NSMutableString *str = [NSMutableString stringWithString:self];
//先轉(zhuǎn)換為帶聲調(diào)的拼音
CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO);
//再轉(zhuǎn)換為不帶聲調(diào)的拼音
CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformStripDiacritics,NO);
//轉(zhuǎn)化為大寫拼音
NSString *pinYin = [str capitalizedString];
//獲取并返回首字母
return [pinYin substringToIndex:1];
}
下面看一下調(diào)用
//獲取拼音首字母
- (void)gainPinYinFirstChracter
{
NSString *str = @"womendejia";
NSString *result = [str jj_gainPinYinFirstCharacter];
NSLog(@"result = %@", result);
NSString *str1 = @"ǎi";
NSString *result1 = [str1 jj_gainPinYinFirstCharacter];
NSLog(@"result1 = %@", result1);
}
看一下輸出結(jié)果
2017-12-16 09:30:47.665652+0800 JJNSStringTool[902:17277] result = W
2017-12-16 09:30:47.669444+0800 JJNSStringTool[902:17277] result1 = A
另一種計(jì)算多行字符串占有的尺寸
還是先看代碼词顾。
//獲取多行文字的寬高
- (CGSize)jj_sizeWithFont:(UIFont *)font andMaxSize:(CGSize)maxSzie;
//獲取多行文字的寬高
- (CGSize)jj_sizeWithFont:(UIFont *)font andMaxSize:(CGSize)maxSzie
{
if (![font isKindOfClass:[UIFont class]]) {
font = [UIFont systemFontOfSize:13];
}
if (NSFontAttributeName == nil || font == nil) {
return CGSizeZero;
}
NSDictionary *atte = @{NSFontAttributeName : font};
return [self boundingRectWithSize:maxSzie options:NSStringDrawingUsesLineFragmentOrigin attributes:atte context:nil].size;
}
下面我們就看一下調(diào)用
- (void)calculateMultiLineSize
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 100.0, 250, 500)];
label.backgroundColor = [UIColor blueColor];
label.text = @"這是什么鬼???這是什么鬼???這是什么鬼???這是什么鬼???這是什么鬼???這是什么鬼???這是什么鬼???這是什么鬼???";
label.numberOfLines = 0;
[label sizeToFit];
[self.view addSubview:label];
CGSize size = [label.text jj_sizeWithFont:[UIFont systemFontOfSize:20.0] andMaxSize:CGSizeMake(250, MAXFLOAT)];
NSLog(@"%lf --- %lf", size.width, size.height);
}
看一下輸出結(jié)果
2017-12-16 09:45:28.169732+0800 JJNSStringTool[1091:27948] 242.660000 --- 143.203125
驗(yàn)證弱密碼
還是先看代碼
//密碼太弱
- (BOOL)jj_isPasswordWeak;
//密碼太弱
- (BOOL)jj_isPasswordWeak
{
if ([self isEqualToString:@"112233"] ||
[self isEqualToString:@"123123"] ||
[self isEqualToString:@"123321"] ||
[self isEqualToString:@"abcabc"] ||
[self isEqualToString:@"abc123"] ||
[self isEqualToString:@"a1b2c3"] ||
[self isEqualToString:@"aaa111"] ||
[self isEqualToString:@"123qwe"] ||
[self isEqualToString:@"qweasd"] ||
[self isEqualToString:@"admin"] ||
[self isEqualToString:@"password"] ||
[self isEqualToString:@"p@ssword"] ||
[self isEqualToString:@"passwd"] ||
[self isEqualToString:@"iloveyou"] ||
[self isEqualToString:@"5201314"]||
[self isEqualToString:@"asdfghjkl"]||
[self isEqualToString:@"qwerty"]) {
return YES;
}
NSString *CT_NUM = @"^(0{6,16})|(1{6,16})|(2{6,16})|(3{6,16})|(4{6,16})|(5{6,16})|(6{6,16})|(7{6,16})|(8{6,16})|(9{6,16})|(a{6,16})|(b{6,16})|(c{6,16})|(d{6,16})|(e{6,16})|(f{6,16})|(g{6,16})|(h{6,16})|(i{6,16})|(j{6,16})|(k{6,16})|(l{6,16})|(m{6,16})|(n{6,16})|(o{6,16})|(p{6,16})|(q{6,16})|(r{6,16})|(s{6,16})|(t{6,16})|(u{6,16})|(v{6,16})|(w{6,16})|(x{6,16})|(y{6,16})|(z{6,16})|(A{6,16})|(B{6,16})|(C{6,16})|(D{6,16})|(E{6,16})|(F{6,16})|(G{6,16})|(H{6,16})|(I{6,16})|(J{6,16})|(K{6,16})|(L{6,16})|(M{6,16})|(N{6,16})|(O{6,16})|(P{6,16})|(Q{6,16})|(R{6,16})|(S{6,16})|(T{6,16})|(U{6,16})|(V{6,16})|(W{6,16})|(X{6,16})|(Y{6,16})|(Z{6,16})$";//
NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
BOOL isMatch1 = [pred1 evaluateWithObject:self];
if (isMatch1==YES) {
return YES;
}
NSString *model = @"0123456789";
NSString *alphe = @"abcdefghijklmnopqrstuvwxyz";
NSString *alphe2 = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if ([model rangeOfString:self].location != NSNotFound) {
return YES;
}
if ([alphe rangeOfString:self].location != NSNotFound) {
return YES;
}
if ([alphe2 rangeOfString:self].location != NSNotFound) {
return YES;
}
return NO;
}
下面我們就調(diào)用一下
//密碼太弱
- (void)evaluateWeakPassword
{
NSString *str = @"123456";
BOOL result = [str jj_isPasswordWeak];
NSLog(@"result = %d", result);
NSString *str1 = @"eoowehohowh";
BOOL result1 = [str1 jj_isPasswordWeak];
NSLog(@"result1 = %d", result1);
}
看輸出結(jié)果
2017-12-16 09:53:52.298812+0800 JJNSStringTool[1268:33541] result = 1
2017-12-16 09:53:52.300740+0800 JJNSStringTool[1268:33541] result1 = 0
URL轉(zhuǎn)義
這個(gè)大家很熟悉了八秃,但是還是給大家列了出來。還是直接看代碼肉盹。
//URL轉(zhuǎn)義
- (NSURL *)jj_encodeURL;
//URL 轉(zhuǎn)義
- (NSURL *)jj_encodeURL
{
NSString *collectUrl = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *urlEncodeString = [collectUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlEncodeString];
return url;
}
下面我們調(diào)用一下
// URL 轉(zhuǎn)義
- (void)encodeURL
{
NSString *str = @"http://www.baidu.com?a=我的家";
NSURL *result = [str jj_encodeURL];
NSLog(@"result = %@", result);
NSString *str1 = @"http://www.baidu.com?a=b";
NSURL *result1 = [str1 jj_encodeURL];
NSLog(@"result1 = %@", result1);
}
看一下輸出結(jié)果
2017-12-16 10:13:54.158231+0800 JJNSStringTool[1455:45285] result = http://www.baidu.com?a=%E6%88%91%E7%9A%84%E5%AE%B6
2017-12-16 10:13:54.158519+0800 JJNSStringTool[1455:45285] result1 = http://www.baidu.com?a=b
字符串的轉(zhuǎn)義
先看一下代碼
- (NSString *)jj_encodedString;
- (NSString *)jj_encodedString
{
NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8));
return result;
}
下面調(diào)用一下
- (void)encodeString
{
NSString *str = @"womendejia";
NSString *result = [str jj_encodedString];
NSLog(@"result = %@", result);
NSString *str1 = @"ode家";
NSString *result1 = [str1 jj_encodedString];
NSLog(@"result1 = %@", result1);
}
下面看輸出結(jié)果
2017-12-16 10:22:32.906892+0800 JJNSStringTool[1570:49951] result = womendejia
2017-12-16 10:22:32.907132+0800 JJNSStringTool[1570:49951] result1 = ode%E5%AE%B6
字符串轉(zhuǎn)義解碼
下面我們就還原到轉(zhuǎn)義前的字符串昔驱。
- (NSString *)jj_decodedString;
- (NSString *)jj_decodedString
{
NSString *result = CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (CFStringRef)self, CFSTR(""), kCFStringEncodingUTF8));
return result;
}
下面我們就調(diào)用一下
- (void)decodeString
{
NSString *str = @"womendejia";
NSString *result = [str jj_decodedString];
NSLog(@"result = %@", result);
NSString *str1 = @"ode%E5%AE%B6";
NSString *result1 = [str1 jj_decodedString];
NSLog(@"result1 = %@", result1);
}
下面看輸出結(jié)果
2017-12-16 10:34:50.994303+0800 JJNSStringTool[1726:56128] result = womendejia
2017-12-16 10:34:50.994607+0800 JJNSStringTool[1726:56128] result1 = ode家
后記
未完,待續(xù)~~~