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

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

作者: 不疯魔难以成佛 | 来源:发表于2017-01-10 17:09 被阅读0次
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;

相关文章

网友评论

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

    本文链接:https://www.haomeiwen.com/subject/auzkbttx.html