iOS开发中让一个字符串中相同的文本变色

IMG_0297.PNG

<code></pre>
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 200, 21)];

[self.view addSubview:label];

NSString *str = @"akdjfldjfldjlfksdjfla";

NSMutableAttributedString *att = [[NSMutableAttributedString alloc]initWithString:str];


NSString *temp =nil;

for(int i =0; i < [str length]; i++)
{
    temp = [str substringWithRange:NSMakeRange(i,1)];
    if ([temp isEqualToString:@"d"]) {
        
        NSRange range = NSMakeRange(i, 1);
        [att addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:range];
    }

}

label.attributedText = att;

推荐阅读更多精彩内容