NSString *tipsStr = @"假如你有一段很长很长的字符串,然后我需要修改其中几个字的颜色:[这几个字包括括号的颜色需要被修改],后面还接着很多字的,而且字体颜色和中括号中的不一样,但是和前面的字体颜色又是一样的";
NSMutableAttributedString *colorStr = [[NSMutableAttributedString alloc] initWithString:tipsStr];
[colorStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:13*1.0/255 green:136*1.0/255 blue:232*1.0/255 alpha:1] range:NSMakeRange(32, 18)];
[colorStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:159*1.0/255 green:162*1.0/255 blue:167*1.0/255 alpha:1] range:NSMakeRange(0, 32)];
[colorStr addAttribute:NSForegroundColorAttributeName value:``[UIColor colorWithRed:159*1.0/255 green:162*1.0/255 blue:167*1.0/255 alpha:1] range:NSMakeRange(50, tipsStr.length-50)];
UITextView *textView = [[UITextView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.view addSubview:textView];
textView.attributedText = colorStr;
网友评论