根据搜素的字符串改变label包含该字符串的文字

http://www.2cto.com/kf/201504/391811.html

 

     NSString *text =@"人生若只如初见";

    //判断字符串所在的位置,并不区分大小写

    NSRange range = [text rangeOfString:@"若" options:NSCaseInsensitiveSearch];

    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];

  //设置其文字的颜色

    [attStr addAttribute:NSForegroundColorAttributeName value:RGBA(0, 161, 176, 1) range:range];

  //把字符串赋值给label

    [self.nameLabel setAttributedText:attStr];

 

posted @ 2016-07-15 16:14  一人前行  阅读(137)  评论(0编辑  收藏  举报