您的位置:首页 > 移动开发 > IOS开发

iOS开发 字符串中截取关键字并给他设置颜色

2017-07-28 11:10 239 查看
    

UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 300, 200)];
contentLabel.textColor = [UIColor blackColor];
[self.view addSubview:contentLabel];
NSString *keyName = @"关键字";
NSString *string = @"我是一句话,哪个字是关键字";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
NSRange range = NSMakeRange([[attributedString string] rangeOfString:keyName].location, keyName.length);
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];

[contentLabel setAttributedText:attributedString];
[contentLabel sizeToFit];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios开发 nsstring