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];
// 创建alertLabel
self.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的attributedText
self.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
UILabel和NSAttributedString那些事的更多相关文章
- UILabel中NSAttributedString和其LinebarkModel等属性之间的关系
如果设置了一个富文本给一个UILabel,那么后续改变这个UILabel的属性时将会同时改变UILabel.attributedText的属性描述,此时若改变了其它的大小.换行模式(如果在显示时我们可 ...
- NSAttributedString设置行间距,间接设置了uilabel的行间距
假设有UIlabel实例:_testLabel NSString * testString = @"明月当空,隐隐约约听到低吟,似有若无.面对大千世界的奢华糜烂,还不如在这一方小城,静静品一 ...
- 解决NSAttributedString与UILabel高度自适应计算问题
两个类扩展方法: /** * 修改富文本的颜色 * * @param str 要改变的string * @param color 设置颜色 * @param range 设置颜色的文字范围 ...
- UILabel添加图片之富文本的简单应用
若想对UILabel添加图片,那么就需要使用NSMutableAttributedString来定义先定义一个普通的label UILabel *lab = [[UILabel alloc]initW ...
- 你真的了解UIButton、UILabel 吗?
一:首先查看一下关于UIButton的定义 @class UIImage, UIFont, UIColor, UIImageView, UILabel; //设置UIButton的样式 typedef ...
- iOS开发小技巧--即时通讯项目:使用富文本在UILabel中显示图片和文字;使用富文本占位显示图片
Label借助富文本显示图片 1.即时通讯项目中语音消息UI的实现,样式如图: 借助富文本在UILabel中显示图片和文字 // 1.创建一个可变的富文本 NSMutableAttributedStr ...
- 优化UITableViewCell高度计算的那些事
优化UITableViewCell高度计算的那些事 我是前言 这篇文章是我和我们团队最近对 UITableViewCell 利用 AutoLayout 自动高度计算和 UITableView 滑动优化 ...
- UILabel 的高度根据文字内容调整
1.UILabel 对文字的自适应有两种方法. 1)将label的numberOfLines设为0;并添加自适应方法[titleLabel sizeToFit],但是这种方法并不理想. 2)根据文字的 ...
- Multi-line NSAttributedString with truncated text
http://stackoverflow.com/questions/7611816/multi-line-nsattributedstring-with-truncated-text/1017279 ...
随机推荐
- .Net Task<T>的一种比较神奇的卡死情况(Wait/Result卡死, await能得到结果)
出现的环境.Net4.0 + WebApi1(4.0.30506.0) + Microsoft.Bcl.Async.1.0.168 自己死活看不出原因, 分享出来给大家看看,希望有人能找到问题的关键 ...
- Razor练习4
今天练习Razor的逻辑处理.一般会使用下面 1. if 2. else 3. else if 4. switch 下面演示中,Insus.NET分别演示1,2, 4:xxx.cshtml代码如下: ...
- Tarjan算法---强联通分量
1.基础知识 在有向图G,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连通图.非强连通图有向图的极大强连通子 ...
- iOS 阶段学习第25天笔记(iOS沙盒机制介绍)
iOS学习(OC语言)知识点整理 一.iOS沙盒机制介绍 1)概念: 每个ios应用都有自己的应用沙盒,应用沙盒就是文件系统目录,与其他应用放入文件 系统隔离,ios系统不允许访问 其他应用的应用沙盒 ...
- 如何改变 FMX ListView 颜色
需求:改变 ListView 颜色 适用:Firemonkey 任何平台 操作:Style 是改变控件外观最便捷的途径,ListView 也不例外,下面示范使用 StyleBook 来设定 ListV ...
- MyEclipse 不能将WAR包导出的解决方法
不能导出WAR包的原因是破解没有完全导致的. 解决办法: 找到MyEclipse安装目录下MyEclipse\Common\plugins文件夹中的com.genuitec.eclipse.expor ...
- 上中下三个DIV,高度自适应(上高度固定,下固定,中间自适应)(代码来自X人)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DivDemo.aspx.c ...
- JProfiler 8下载地址和注册码
JProfiler---- 一个很好的java性能监控工具,现在附上下载地址和注册码,此破解版仅供学习交流使用, 其他用途请购买正版授权!!!windows x64 zip下载地址:http://do ...
- 通过 imagick 让 PHP 生成 PSD 文件缩略图
第一步.安装ImageMagick 首先需要安装 ImageMagick ,因为 imagick 是一个可以供 PHP 调用 ImageMagick 功能的PHP扩展.(目前最新版本是:ImageMa ...
- Charted – 自动化的可视化数据生成工具
Charted 是一个让数据自动生成可视化图表的工具.只需要提供一个数据文件的链接,它就能返回一个美丽的,可共享的图表.Charted 不会存储任何数据.它只是获取和让链接提供的数据可视化. 在线演示 ...