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开发:字符串设置指定内容的文字颜色、文字大小、文字字体类型
- iOS开发-设置NavigationBar的颜色和字体属性
- ios开发 之 设置多种文字颜色/背景色/文字下划线/行间距
- Ios开发Swift自定义UIbutton增加一个能设置高亮状态下背景颜色的方法
- iOS开发--UIButton 设置圆角 边框颜色 点击回调方法
- iOS开发-设置导航栏title字体颜色
- iOS开发技巧(系列十八:扩展UIColor,支持十六进制颜色设置)
- iOS开发之iOS7设置状态栏字体颜色
- 把所有界面的状态栏字体颜色设置为白色--iOS开发系列---项目中成长的知识一
- iOS开发之设置UIView边框颜色及边线厚度以及圆角及弧度
- iOS开发设置状态栏字体颜色
- ios开发 之 设置多种文字颜色/背景色/文字下划线/行间距
- iOS开发UITableViewCell的选中时的颜色设置
- iOS颜色开发设置详解
- iOS开发之iOS7设置状态栏字体颜色
- IOS开发怎么UINavigationController设置title标题的颜色?
- 【iOS开发-31】UITabBar背景、icon图标颜色、被选中背景设置以及隐藏UITabBar的两种方式
- iOS开发设置textFiled的placeholder颜色
- ios开发之16进制颜色(html颜色值)字符串转为UIColor
- iOS开发中设置UITableViewCell选中时的颜色