版本记录
版本号 | 时间 |
---|---|
V1.0 | 2017.12.16 |
前言
NSString是我们经常使用的一个类,但是苹果的API很多用起来不是那么方便,需要根据我们特殊需求进行个性化的改变和定制,接下来,我就写一个NSString分类工具,以后在使用的时候直接调用这个分类中的方法,可以实现很多小的功能,用起来也很方便。相关代码已经上传至GitHub - 刀客传奇。感兴趣的可以看我上面写的几篇。
1. 一个实用的NSString分类工具 (一)—— 玩转数字
获取拼音首字母
获取拼音的首字母,看着简单好像取第一个字符就可以,但是要考虑很多情况,比如说声调正好在首位怎么办?下面我们看一下代码。
//获取拼音的首字母
- (NSString *)jj_gainPinYinFirstCharacter;
//获取拼音的首字母
- (NSString *)jj_gainPinYinFirstCharacter
{
//转成了可变字符串
NSMutableString *str = [NSMutableString stringWithString:self];
//先转换为带声调的拼音
CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO);
//再转换为不带声调的拼音
CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformStripDiacritics,NO);
//转化为大写拼音
NSString *pinYin = [str capitalizedString];
//获取并返回首字母
return [pinYin substringToIndex:1];
}
下面看一下调用
//获取拼音首字母
- (void)gainPinYinFirstChracter
{
NSString *str = @"womendejia";
NSString *result = [str jj_gainPinYinFirstCharacter];
NSLog(@"result = %@", result);
NSString *str1 = @"ǎi";
NSString *result1 = [str1 jj_gainPinYinFirstCharacter];
NSLog(@"result1 = %@", result1);
}
看一下输出结果
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
另一种计算多行字符串占有的尺寸
还是先看代码。
//获取多行文字的宽高
- (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;
}
下面我们就看一下调用
- (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);
}
看一下输出结果
2017-12-16 09:45:28.169732+0800 JJNSStringTool[1091:27948] 242.660000 --- 143.203125
验证弱密码
还是先看代码
//密码太弱
- (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;
}
下面我们就调用一下
//密码太弱
- (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);
}
看输出结果
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转义
这个大家很熟悉了,但是还是给大家列了出来。还是直接看代码。
//URL转义
- (NSURL *)jj_encodeURL;
//URL 转义
- (NSURL *)jj_encodeURL
{
NSString *collectUrl = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *urlEncodeString = [collectUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlEncodeString];
return url;
}
下面我们调用一下
// URL 转义
- (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);
}
看一下输出结果
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
字符串的转义
先看一下代码
- (NSString *)jj_encodedString;
- (NSString *)jj_encodedString
{
NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8));
return result;
}
下面调用一下
- (void)encodeString
{
NSString *str = @"womendejia";
NSString *result = [str jj_encodedString];
NSLog(@"result = %@", result);
NSString *str1 = @"ode家";
NSString *result1 = [str1 jj_encodedString];
NSLog(@"result1 = %@", result1);
}
下面看输出结果
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
字符串转义解码
下面我们就还原到转义前的字符串。
- (NSString *)jj_decodedString;
- (NSString *)jj_decodedString
{
NSString *result = CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (CFStringRef)self, CFSTR(""), kCFStringEncodingUTF8));
return result;
}
下面我们就调用一下
- (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);
}
下面看输出结果
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家
后记
未完,待续~~~