您的位置:首页 > 其它

改变字符串中指定字符的颜色

2016-10-08 17:07 281 查看
有的时候我们有这样的需求:一行字符串中的字符需要显示不同的颜色,这时候 我们就需要指定特定的字符显示特定的颜色

- (void)viewDidLoad {
NSMutableAttributedString *gitStr = [self ChangeStrColor:[Util IncreaseDecimal:@"不同颜色的字符组成的字符串显示"] Loction:5];

label.attributedText = gitStr;
}

- (NSMutableAttributedString *)ChangeStrColor:(NSString *)ColorStr Loction:(NSInteger)loction {
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:ColorStr];
NSInteger lengh = [str length];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,loction)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(lengh-3,3)];

return str;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  字符串-颜色