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实现的,都比较麻烦 ...
随机推荐
- Java 中父类怎么调用子类的方法?
父类能调用子类的方法吗? 答:能. 怎么做? ● 把子类传递到父类的有参构造中,然后调用. ● 使用反射的方式调用,你使用了反射还有谁不能调用的?! ● 父类调用子类的静态方法. 案例展示: pack ...
- Android Apk增量更新
前言 有关APK更新的技术比较多,例如:增量更新.插件式开发.热修复.RN.静默安装. 下面简单介绍一下: 什么是增量更新? 增量更新就是原有app的基础上只更新发生变化的地方,其余保持原样. 与 ...
- shell_processing
1.文本处理_2:grep,sed,awk 2.regular_expression 3.Test 一.文本处理_2 1.grep --Linux处理正则表达式的主要程序.正则表达式是一种符号表示 ...
- iOS设计模式 - 命令
iOS设计模式 - 命令 原理图 说明 命令对象封装了如何对目标执行指令的信息,因此客户端或调用者不必了解目标的任何细节,却仍可以对他执行任何已有的操作.通过把请求封装成对象,客户端可以把它参数化并置 ...
- [翻译] CRPixellatedView-用CIPixellate滤镜动态渲染UIView
CRPixellatedView-用CIPixellate滤镜动态渲染UIView https://github.com/chroman/CRPixellatedView 本人测试的效果: Usage ...
- 使用ModelForm表单验证
1.定义model.py model中定义的字段类型,只有在通过form进行验证的时候才有效,数据库中的字段类型与其并不完全一致,如数据库中并没有ipaddress类型.如果不通过form对字段进行验 ...
- 工具类-vim在shell中卡死的情况
time:2015/11/35 在xshell下面使用vim编辑,有时候会出现突然卡死的情况.但是如果重新开一个终端的话,打开文件又是一大堆问题,今天又碰到了,搜了一下就找到一个帮助了[1] 原因:按 ...
- Java——并发编程
1.在java中守护线程和本地线程区别? java中的线程分为两种:守护线程(Daemon)和用户线程(User). 任何线程都可以设置为守护线程和用户线程,通过方法Thread.setDaemon( ...
- Mysql源码安装shell脚本
#!/bin/bash#date:2019/1/20#by author zhangjia#install mysql#shell_name:mysql_auto_install.sh######## ...
- Redis学习---Redis操作之有序集合
有序集合,在集合的基础上,为每元素排序:元素的排序需要根据另外一个值来进行比较,所以,对于有序集合,每一个元素有两个值,即:值和分数,分数专门用来做排序. zadd(name, *args, **kw ...