首先,来复习复习正则表达试NSRegularExpression;
正则表达式是用来处理字符串的,可以判断字符串是否符合某些规则,看代码:
NSString *testStr = @"15779698838";//手机号
NSString *pattern = @"^1[3578]\\d{9}$";//这就是上面所说的“规则”
NSRegularExpression*regex = [[NSRegularExpressionalloc] initWithPattern:pattern options:0error:nil];//创建正则表达式字符串对象
//根据“规则”匹配字符串testStr,方法是返回的一个数组,这里返回的数组是有值的(即testStr是一个手机号码)
NSArray*result = [regex matchesInString:testStr options:0range:NSMakeRange(0, testStr.length)];
if (1 == result.count){
NSLog(@"testStr是一个手机号");
} else{
NSLog(@"不是手机号");
}
没错,这样就可以判断一个字符串不是是一个手机号了,也可以判断邮箱,密码等,把“规则“改一下就行了。
为什么要讲正则表达式呢?因为下面要用到它,,,
----------------------------------------------
还记得我之前做图文混排的时候还是用的imageView和label拼接起来,特他妈的蛋疼-_-|
现在才发现有个这么好用的东西:NSMutableAttributedString//这是一个可变的属性字符串
NSString*string = @"哈哈哈哈";
//先把普通字符串转成属性字符串
NSMutableAttributedString*attStr = [[NSMutableAttributedStringalloc] initWithString:string];
//看名字就知道了,这是一个文字附件
NSTextAttachment*attchment = [[NSTextAttachmentalloc] init];
attchment.image= [UIImageimageNamed:@"000@2x.png"];//给文字附件设置图
attchment.bounds= CGRectMake(0, -3, 25, 25);// 设置附件的bound值,不然可能显示不了
//属性字符串拼接
[attStr appendAttributedString:[NSAttributedStringattributedStringWithAttachment:attchment]];//用attributedStringWithAttachment:这个方法创建一个属性字符串,参数就是刚刚的附件对象
testLabel.attributedText= attStr;//设置label的属性字符串
*OK,这样就完成了一个简单的图文混排了,来看看效果:

当然,也可以把图片插到前面
[attStr insertAttributedString:[NSAttributedStringattributedStringWithAttachment:attchment] atIndex:2];

是不是很简单,嘻嘻。。。
转载于:https://www.cnblogs.com/programTravel/p/4857873.html