iOS开发-同一段文字显示不同颜色
2023-09-11 14:21:22 时间
开发中有些UI经常会出现同一段话显示颜色不一致的情况,在没有好办法的情况下我们会用两个label来实现,但是这样的话适配起来并不方便,这里我有一种方法跟大家分享一下:
UILabel* noteLabel = [[UILabel alloc] init];
noteLabel.frame = CGRectMake(60, 100, 200, 100);
noteLabel.textColor = [UIColor blackColor];
noteLabel.numberOfLines = 2;
NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:@"注册:同意"];
NSRange redRange = NSMakeRange([[noteStr string] rangeOfString:@"注册:"].location, [[noteStr string] rangeOfString:@"注册:"].length);
[noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:redRange];
NSRange redRangeTwo = NSMakeRange([[noteStr string] rangeOfString:@"同意"].location, [[noteStr string] rangeOfString:@"同意"].length);
[noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:redRangeTwo];
[noteLabel setAttributedText:noteStr];
[noteLabel sizeToFit];
[self.view addSubview:noteLabel];
也可到我的github去下载代码:https://github.com/codeliu6572/Label-different-color
相关文章
- [iOS]怎样在iOS开发中切换显示语言实现国际化
- iOS mac添加证书 不能修改“System Roots”钥匙串错误
- 解决Xcode 6 编译Cocos2d-x iOS项目失败
- IOS Swift语言开发 tableView的重用以及自cell的自适应高度
- 《iOS 8开发指南(第2版)》——第6章,第6.2节Xcode中的MVC
- 《iOS 8案例开发大全》——实例008 实现断点调试
- iOS 使用宏定义函数和代码块
- iOS开发UI篇—UITabBarController简单介绍
- iOS 开发大全之如何制作像 Unsplash 应用程序这样的现代无限滚动视图
- 阿里开源 iOS 协程开发框架 coobjc!--异步编程的问题与解决方案
- iOS开发HTTPS实现之信任SSL证书和自签名证书
- Clubhouse 对iOS开发工程师的七点要求是什么
- 成为一名iOS开发人员需要准备哪些软硬件设备
- iOS 开发中你是否遇到这些经验问题(一)
- iOS应用内支付(内购)的个人开发过程及坑!
- iOS开发UI篇—ios应用数据存储方式(归档)
- 知乎iOS客户端下午瘫了 原来是第三方防火墙变更害的
- 知名黑客确认iOS 10.2越狱工具 基于Safari开发
- iOS开发 - 怎么让支持iPad/iPhone的app取消支持iPad或者iPhone中的一项
- iOS开发-百度地图基本定位和罗盘跟随的使用,附加地理编码和反地理编码
- iOS开发-自定义欢迎页和显示时间
- iOS开发 - 若把上线版本下架,重新提交新的版本,通过审核后显示下架状态
- iOS开发之网络编程--4、NSURLSessionDataTask实现文件下载(离线断点续传下载) <进度值显示优化>
- IOS开发之功能模块--自定义UITabBarViewController的备用代码
- iOS 学习之分段Table View的使用(Grouped样式表格)