//删除字符串中某些字符
NSMutableString *responseString = [NSMutableString stringWithString:label.text];
NSString *character = nil;
for (int i = 0; i < responseString.length; i ++) {
character = [responseString substringWithRange:NSMakeRange(i, 1)];
if ([character isEqualToString:@";"]||[character isEqualToString:@"&"])
[responseString deleteCharactersInRange:NSMakeRange(i, 1)];
}
label.text=responseString;
//改变字符串中某一段字符的字体大小
NSMutableAttributedString *numString = [[NSMutableAttributedString alloc] initWithString:MBLabel2.text];
NSRange ran2 = NSMakeRange(0, numString.length-2);
[numString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:ran2];
MBLabel2.attributedText=numString;
//只修改字符串中的数字的颜色
NSString * content = @"fgd87484fdjdhj322";
NSArray * number = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"."];
NSMutableAttributedString * attributeString = [[NSMutableAttributedString alloc]initWithString:content];
for (int i = 0; i < content.length; i ++) {
//每次只截取一个字符的范围
NSString * str = [content substringWithRange:NSMakeRange(i, 1)];
//判断装有0-9的字符串的数字数组是否包含截取字符串出来的单个字符,从而筛选出符合要求的数字字符的范围NSMakeRange
if ([number containsObject:str]) {
[attributeString setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:15]} range:NSMakeRange(i, 1)];
}
}
//完成查找数字并显示
self.label.attributedText = attributeString;
网友评论