博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
改变label中的某字体颜色
阅读量:4318 次
发布时间:2019-06-06

本文共 802 字,大约阅读时间需要 2 分钟。

 NSString *allString=@"你家在哪里,需要我送你么";

    NSString *subString=@"在哪里";

    UILabel *stringLabel=[[UILabel alloc]init];

    stringLabel.frame=CGRectMake(0, 100, 300, 30);

    stringLabel.font=[UIFont systemFontOfSize:17];

    stringLabel.backgroundColor=[UIColor blackColor];

    stringLabel.text=allString;

    stringLabel.textColor=[UIColor whiteColor];

    [self.view addSubview:stringLabel];

    

    NSString *allS=[allString lowercaseString];

    NSString *subS=[subString lowercaseString];

    NSRange range=[allS rangeOfString:subS];

    if (range.location!=NSNotFound) {

        NSMutableAttributedString  *string=[[NSMutableAttributedString alloc]initWithString:allString];

        [string addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor] range:range];

        stringLabel.attributedText=string;

    }

 

转载于:https://www.cnblogs.com/106dapeng/p/4675987.html

你可能感兴趣的文章
CKplayer:视频推荐和分享插件设置
查看>>
CentOS系统将UTC时间修改为CST时间
查看>>
redis常见面试题
查看>>
导航控制器的出栈
查看>>
玩转CSS3,嗨翻WEB前端,CSS3伪类元素详解/深入浅出[原创][5+3时代]
查看>>
iOS 9音频应用播放音频之播放控制暂停停止前进后退的设置
查看>>
Delphi消息小记
查看>>
HNOI2016
查看>>
JVM介绍
查看>>
将PHP数组输出为HTML表格
查看>>
Java中的线程Thread方法之---suspend()和resume() 分类: ...
查看>>
经典排序算法回顾:选择排序,快速排序
查看>>
BZOJ2213 [Poi2011]Difference 【乱搞】
查看>>
c# 对加密的MP4文件进行解密
查看>>
AOP面向切面编程C#实例
查看>>
AngularJs学习笔记-慕课网AngularJS实战
查看>>
数据库三大范式
查看>>
工作总结之二:bug级别、优先级别、bug状态
查看>>
访问修饰符、封装、继承
查看>>
更换pip源到国内镜像,提升pip下载速度.
查看>>