iOS 删除字符串中某些字符,改变字符串中某一段字符的字体大小

2017-06-12  本文已影响0人  贪吃的猫cx

//删除字符串中某些字符

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;

上一篇下一篇

猜你喜欢

热点阅读