UILabel和NSAttributedString那些事
注:通常的label用来现实普通的文字。但是,你常常会遇到这样的情况:一段文字中不仅有文字,也有图片,甚至文字中的某段文字与其他的文字的appearance不一致的情况,这样的一段文字就可以称得上是富文本了。label的attributedText属性就是用来接受这样的文本内容的。
场景
如图
- 若你遇到这样的需求,不妨考虑一下使用NSAttributedString了创建这样的文本。如果这段文字具有点击事件,实现方法有以下两种:
- 将这段文字设置为button的attributedTitle
- 将这段文字设置为label的attributedText,并给label添加点击手势
- 若你遇到这样的需求,不妨考虑一下使用NSAttributedString了创建这样的文本。如果这段文字具有点击事件,实现方法有以下两种:
实现思路
- 这段文字由图片和文字共同组成
- 将图片封装到NSTextAttachment实例中,然后通过NSAttributedString的类构造方法初始化为NSAttributedString实例。
- 使用NSMutableDictionary来封装文本的现实属性
- 使用NSAttributedString的对象方法addAttributes:range:改变指定范围文字的现实属性
具体实现
集成Masonry框架
- 创建pch文件
- pch文件通常的命名方法:项目名-prefix.pch,如:AttributedStringInLabel-Prefix.pch
pch文件的配置
将通用的头文件添加到pch文件中
- 定义通过RGBA创建UIColor对象的宏
- 我们通常会将经常使用的方法定义成宏,来提高开发效率和屏蔽复杂操作
带参数的宏定义中的参数名,不能与其后的形式参数名相同(宏定义其实就是替换,将文本替换成指定的文本)
// redValue 不能写成red #define UIColorWithInt(redValue, greenValue, blueValue, alphaValue) [UIColor colorWithRed:(redValue)/255.0f green:(greenValue)/255.0f blue:(blueValue)/255.0f alpha:(alphaValue)]
- alertLabel
包含alertLabel属性
@interface ViewController () /** alertLabel */ @property (nonatomic, strong) UILabel *alertLabel; @end
创建alertLabel
- (void)viewDidLoad {[super viewDidLoad];// 创建alertLabelself.alertLabel = [[UILabel alloc] init];[self.view addSubview:self.alertLabel];// 设置alertLabel的富文本属性[self setupAlertLabel]; }
- 使用Masonry框架布局alertLabel的位置
- 若是在控制器中,通常在viewDidLayoutSubviews方法中布局子控件
若是自定以控制,通常在layoutSubviews方法中布局子控件
/*** 布局alertLabel*/ - (void)viewDidLayoutSubviews {[super viewDidLayoutSubviews];[self.alertLabel mas_makeConstraints:^(MASConstraintMaker *make) {make.centerX.centerY.equalTo(self.view);}]; }
设置alertLabel的attributedText属性
/*** 设置alertLabel*/ - (void)setupAlertLabel {// 文本的显示样式NSMutableDictionary *appearanceDictionary = [NSMutableDictionary dictionary];appearanceDictionary[NSForegroundColorAttributeName] = UIColorWithInt(117, 117, 117, 1.0);appearanceDictionary[NSFontAttributeName] = [UIFont boldSystemFontOfSize:15];// 文本内容(指定显示属性的文本)NSString *normolString = @" 莫将支付宝密码告诉他人,谢谢!";NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:normolString attributes:appearanceDictionary];// 改变文本中某段文字的现实属性NSMutableDictionary *subAppearanceDictionary = [NSMutableDictionary dictionary];subAppearanceDictionary[NSForegroundColorAttributeName] = [UIColor redColor];subAppearanceDictionary[NSFontAttributeName] = [UIFont systemFontOfSize:17];NSRange subRange = [normolString rangeOfString:@"谢谢!"];[attributedString addAttributes:subAppearanceDictionary range:subRange];// 添加图片NSTextAttachment *attachment = [[NSTextAttachment alloc] init];attachment.image = [UIImage imageNamed:@"alert"];attachment.bounds = CGRectMake(0, 0, 14, 14);NSAttributedString *imageString = [NSAttributedString attributedStringWithAttachment:attachment];[attributedString insertAttributedString:imageString atIndex:0];// 设置alertLabel的attributedTextself.alertLabel.attributedText = attributedString;// 给alertLabel添加点击事件[self addTargetToAlertLabel]; }
- 给alertLabel添加点击手势
UILabel对象默认是不具备与用户交互的能力,若要保证添加的手势有效果,需要是其具备与用户交互的能力
- (void)addTargetToAlertLabel {self.alertLabel.userInteractionEnabled = YES;UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(alertLabelClick:)];[self.alertLabel addGestureRecognizer:tap]; } /*** alertLabel的点击事件*/ - (void)alertLabelClick:(UILabel *)label {NSLog(@"alertLabelClick"); }
VVDocument
- VVDocument是一款快速编写注释的Xcode插件,但是升级Xcode之后,出现了VVDocument不可用的情况,以下是解决方案
打开“Finder”->“应用程序”->“Xcode”->"显示包内容"->"contents"->"Info.plist",拷贝如图所示内容
command+shift+G,进入指定路径文件夹:~/Library/Application Support/Developer/Shared/Xcode
“显示包内容”->“Contents”->"Info.plist", 新建Item,粘贴拷贝的字符串
重启Xcode,使用三个斜杠(///)来使用VVDocument
转载于:https://www.cnblogs.com/theDesertIslandOutOfTheWorld/p/5143193.html
UILabel和NSAttributedString那些事相关推荐
- iOS上文本处理之简史
iOS 文字简史 iPhone OS 2 UILabel UITextField UITextView iPhone OS 3 New Feature: 复制 && 粘贴 iOS 3. ...
- UILabel上展示不同颜色的文字(NSAttributedString)
时间 2014-03-31 21:18:28 CSDN博客原文 http://blog.csdn.net/u011439689/article/details/22693679 首先导入CoreT ...
- 关于字体适配的那些事
前言 之前做过很多项目都没考虑过字体适配问题.相信绝大多数人在做项目时,都没仔细去考虑这件事.一般都是根据UI出的图做个估算,有耐心的估计会自己拿工具测量下.如今,考虑到iPhone机型的多样性,UI ...
- iostext添加点击事件_iOS给UILabel添加点击事件
前言:笔者最近需要实现给UILabel中的链接添加点击事件的功能.使用so.com查了下,发现TTTAttributedLabel的封装程度比较好.整理了TTTAttributedLabel的基本使用 ...
- Swift4之NSAttributedString的使用
Swift富文本使用 NSAttributeString的优点 1.多样式的显示富文本信息. 2.可用于图文混排,借助NSTextAttachment. 3.一条语句代码(属性字典)可以设置多个属性. ...
- UILabel中的多行文本
是否可以像在UITextView那样在UILabel具有多行文本,还是应该改用第二行? #1楼 您可以使用\\r转到下一行,同时使用NSString填充UILabel . UILabel * labe ...
- iOS UILabel加载html点击图片查看大图 附demo
我们在有些时候,因为性能和加载时间的问题,需要用UILabel加载html的方式来代替webview. 大部分情况,UILabel都可以很好的展示出想要的效果,但是却不能满足点击查看大图的需求. 本解 ...
- iOS app性能优化的那些事
iPhone上面的应用一直都是以流畅的操作体验而著称,但是由于之前开发人员把注意力更多的放在开发功能上面,比较少去考虑性能的问题,可能这其中涉及到objective-c,c++跟lua,优化起来相对 ...
- barbuttonitem 文字换行_IOS TableView的Cell高度自适应,UILabel自动换行适应
ios tableView那些事 (八) tableview的插入移动.self.tableview.editinganimated:YES];- (BOOL)tableView:(UITableVi ...
最新文章
- 新建JRapid项目(idea创建maven多模块项目)
- HTML基础部分(1)字体,照片,链接
- 如何在PowerBuilder 11.x 中通过ADO.NET访问Sybase ASE?
- luogu P1037 【产生数】
- 如何成为一个设计师和程序员混合型人才
- 精心挑选的23款美轮美奂的 jQuery 图片特效插件
- mysql所支持的比较运算符_mysql比较运算符有哪些?Mysql比较运算符详解
- JDK源码解析之java.util.AbstractCollection
- 华硕无双新品首爆:H45标压处理器+全球首款2.8K 120Hz OLED屏
- HTML5变化 (一)
- java多网卡组播,多网卡 组播
- 论文中的i.e.等简写
- 北京的“狗不理”包子与傻X
- 硬件工程师七夕鹊桥设计锦集
- Java设计模式策略模式(附实例代码)每天一学设计模式
- HP刀片服务器系统Flex,深入解析Flex System新一代刀片系统
- vue组件间通信传递数据的四种方式(路由传参、父子组件传参、兄弟组件传参、深层次传参)
- 前端加密中文,后端解密java
- Java语言实现小学数学练习
- python自动化生成请假条
热门文章
- django实现web分页的三种方法
- 使用C# 未解决的问题(VS2012)
- 《java入门第一季》之java语法部分小案例
- fglrx 9.8与kernel 2.6.30
- Spark源码分析之Master主备切换机制
- (33)FPGA面试题附加约束的作用
- apache目录 vscode_CentOS 上使用vscode 调试百度大数据分析框架Apache Doris BE
- C++ 深拷贝和浅拷贝std::move移动构造函数,移动赋值函数
- SDL环境初始化测试代码
- 12018.LTC2631电压调节芯片