iOS使用NSMutableAttributedString实现富文本小结
NSAttributedString
NSAttributedString对象管理适用于字符串中单个字符或字符范围的字符串和关联的属性集(例如字体和字距)。NSAttributedString对象的默认字体是Helvetica 12点,可能与平台的默认系统字体不同。因此,您可能希望创建适用于您的应用程序的非默认属性的新字符串。您还可以使用NSParagraphStyle类及其子类NSMutableParagraphStyle来封装NSAttributedString类使用的段落或标尺属性。
实例化方法和使用方法
实例化方法
使用字符串初始化
- (instancetype)initWithString:(NSString *)str;
代码示例
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"测试数据"];
字典中存放一些属性名和属性值
- (instancetype)initWithString:(NSString *)str attributes:(NSDictionary<NSString *,id> *)attrs;
代码示例
NSDictionary *attributedDict = @{
NSFontAttributeName:[UIFont systemFontOfSize:16.0],
NSForegroundColorAttributeName:[UIColor redColor],
NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick)
};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"测试数据" attributes:attributedDict];
使用NSAttributedString初始化,与NSMutableString,NSString类似
- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;
使用方法
为某一范围内的文字设置多个属性的方法
- (void)setAttributes:(NSDictionary<NSString *,id> *)attrs range:(NSRange)range;
//代码示例
NSString *string = @"测试数据";
NSDictionary *attributedDict = @{
NSFontAttributeName:[UIFont systemFontOfSize:16.0],
NSForegroundColorAttributeName:[UIColor redColor],
NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick)
};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
[attributedString setAttributes:attributedDict range:NSMakeRange(0, string.length)];
为某一范围内的文字添加某个属性的方法
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
//代码示例
NSString *string = @"测试数据";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, string.length)];
为某一范围内的文字添加多个属性的方法
- (void)addAttributes:(NSDictionary<NSString *,id> *)attrs range:(NSRange)range;
//代码示例
NSString *string = @"测试数据";
NSDictionary *attributedDict = @{
NSFontAttributeName:[UIFont systemFontOfSize:16.0],
NSForegroundColorAttributeName:[UIColor redColor],
NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick)
};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
[attributedString addAttributes:attributedDict range:NSMakeRange(0, string.length)];
移除某个范围内的某个属性的方法
- (void)removeAttribute:(NSString *)name range:(NSRange)range;
//代码示例
NSString *string = @"测试数据";
NSDictionary *attributedDict = @{
NSFontAttributeName:[UIFont systemFontOfSize:16.0],
NSForegroundColorAttributeName:[UIColor redColor],
NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick)
};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
[attributedString addAttributes:attributedDict range:NSMakeRange(0, string.length)];
[attributedString removeAttribute:NSForegroundColorAttributeName range:NSMakeRange(0, string.length)];
属性及说明
| key | 说明 |
|---|---|
| NSFontAttributeName | 字体,value是UIFont对象 |
| NSParagraphStyleAttributeName | 绘图的风格(居中,换行模式,间距等诸多风格),value是NSParagraphStyle对象 |
| NSForegroundColorAttributeName | 文字颜色,value是UIColor对象 |
| NSLigatureAttributeName | 字符连体,value是NSNumber |
| NSKernAttributeName | 字符间隔 |
| NSStrikethroughStyleAttributeName | 删除线,value是NSNumber |
| NSUnderlineStyleAttributeName | 下划线,value是NSNumber |
| NSStrokeColorAttributeName | 描绘边颜色,value是UIColor |
| NSStrokeWidthAttributeName | 描边宽度,value是NSNumber |
| NSShadowAttributeName | 阴影,value是NSShadow对象 |
| NSTextEffectAttributeName | 文字效果,value是NSString |
| NSAttachmentAttributeName | 附属,value是NSTextAttachment 对象 |
| NSLinkAttributeName | 链接,value是NSURL or NSString |
| NSBaselineOffsetAttributeName | 基础偏移量,value是NSNumber对象 |
| NSStrikethroughColorAttributeName | 删除线颜色,value是UIColor |
| NSObliquenessAttributeName | 字体倾斜 |
| NSExpansionAttributeName | 字体扁平化 |
| NSVerticalGlyphFormAttributeName | 垂直或者水平,value是 NSNumber,0表示水平,1垂直 |
富文本段落排版格式属性说明
| 属性 | 说明 |
|---|---|
| lineSpacing | 字体的行间距 |
| firstLineHeadIndent | 首行缩进 |
| alignment | (两端对齐的)文本对齐方式:(左,中,右,两端对齐,自然) |
| lineBreakMode | 结尾部分的内容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz") |
| headIndent | 整体缩进(首行除外) |
| minimumLineHeight | 最低行高 |
| maximumLineHeight | 最大行高 |
| paragraphSpacing | 段与段之间的间距 |
| paragraphSpacingBefore | 段首行空白空间 |
| baseWritingDirection | 书写方向(一共三种) |
| hyphenationFactor | 连字属性 在iOS,唯一支持的值分别为0和1 |
作者:coder小鹏
iOS使用NSMutableAttributedString实现富文本小结的更多相关文章
- 【转】iOS使用NSMutableAttributedString实现富文本
iOS使用NSMutableAttributedString实现富文本 在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘 ...
- iOS使用NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)
在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘UILabel的textLayer,有的是用html5实现的,都比较麻烦 ...
- [转] iOS使用NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)
转自: 在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘UILabel的textLayer,有的是用html5实现的,都 ...
- [iOS] 利用 NSAttributedString 进行富文本处理
/iOS /[iOS] 利用 NSAttributedString 进行富文本处理 2016年4月4日 刘小龙 iOS 许多时候我们需要以各种灵活的形式展现文本信息,即富文本.普通的 text 属性显 ...
- 【改】iOS学习之NSAttributedString(富文本)
NSAttributedString 叫做富文本,是一种带有属性的字符串,通过它可以轻松的在一个字符串中表现出多种字体.字号.字体大小等各不相同的风格,还可以对段落进行格式化,一般都是对可变富文本(N ...
- iOS学习之NSAttributedString(富文本)
NSAttributedString 叫做富文本,是一种带有属性的字符串,通过它可以轻松的在一个字符串中表现出多种字体.字号.字体大小等各不相同的风格,还可以对段落进行格式化,一般都是对可变富文本(N ...
- 关于NSMutableAttributedString进行富文本 UILabel 的制作
//1.初始化与其他无异 NSMutableAttributedString *AttributedStr2 = [[NSMutableAttributedString alloc]initWithS ...
- #iOS问题记录# UITextview富文本链接,禁止长按事件
UITextView的富文本组装,添加图片点击事件,启动 - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *) ...
- iOS开发--使用NSMutableAttributedString 实现富文本
在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘UILabel的textLayer,有的是用html5实现的,都比较麻烦 ...
随机推荐
- javaSE——字符流
字符流: 读取数据的单位是字符,即每次可以读取至少一个字符(一个字母.数字.汉字.符号). 和字节流一样,管子搭载的对象不同,则字符流就不同. 类 FileReader: 用于读取文件的便捷类. 继承 ...
- Solo 博客系统 1.7.0 发布 - 新版 MD 编辑器
本次发布主要是更新了 Markdown 编辑器,加入了全屏.Emoji 自动完成.粘贴时自动转换为 MD 格式的特性.(1.7.0 版本变更记录请看这里) 目前的 Markdown 编辑器有多好用呢? ...
- React—Native开发之原生模块向JavaScript发送事件
首先,由RN中文网关于原生模块(Android)的介绍可以看到,RN前端与原生模块之 间通信,主要有三种方法: (1)使用回调函数Callback,它提供了一个函数来把返回值传回给JavaScript ...
- sqlserver为数据库表增加自增字段
需求: 数据库为SQLServer.对已有的数据库表customer加一个序号字段,一次性对所有现存客户加上编号,并在新建客户时自动增加一个编号,数值自增1. 解决方法: 1. 复制表结构.把原 ...
- Android保持屏幕常亮
Android保持屏幕常亮,PowerManager.WakeLock的使用 package com.hebaijun.wakelock; import android.app.Activi ...
- 11 tensorflow在tf.while_loop循环(非一般循环)中使用操纵变量该怎么做
代码(操纵全局变量) xiaojie=1 i=tf.constant(0,dtype=tf.int32) batch_len=tf.constant(10,dtype=tf.int32) loop_c ...
- mysqldump的假注释
今天在查看mysqldump内容的时候,发现类似注释的东西,仔细了解了下, If you add a version number after the “!” character, the synta ...
- 【Kettle】2、文件夹与界面介绍
1.文件夹介绍 下载Kettle6.1解压后出现下图相关文件夹以及文件夹介绍说明: Lib:存放Kettle的核心(core)jar包.工作引擎(engine)jar包.数据库(DB) jar包.图形 ...
- 向服务器post或者get数据返回
#region 向服务器端Get值返回 /// <summary> /// 向服务器端Get返回 /// </summary> ///<see cref="Au ...
- RHEL7: How to configure a rc-local service
问题: linux7 /etc/rc.local 不生效: [root@bogon mysql3306]# uname -aLinux bogon 3.10.0-862.el7.x86_64 #1 S ...