服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS利用NSMutableAttributedString实现富文本的方法小结

iOS利用NSMutableAttributedString实现富文本的方法小结

2021-04-26 18:43coder小鹏 IOS

这篇文章主要给大家介绍了关于iOS利用NSMutableAttributedString如何实现富文本的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

前言

在iOS开发过程中,经常会用到给字体加下划线,显示不同颜色和大小的字体等需求,经常遇到这种需求都是直接到百度或者谷歌直接把代码粘过来,并没有做系统的整理,今天刚好有时间,把这部分的内容整理一下,便于后续的开发,闲话不说,接下来就跟着我一起来了解一下NSMutableAttributedString吧.

NSAttributedString

NSAttributedString对象管理适用于字符串中单个字符或字符范围的字符串和关联的属性集(例如字体和字距)。NSAttributedString对象的默认字体是Helvetica 12点,可能与平台的默认系统字体不同。因此,您可能希望创建适用于您的应用程序的非默认属性的新字符串。您还可以使用NSParagraphStyle类及其子类NSMutableParagraphStyle来封装NSAttributedString类使用的段落或标尺属性。

实例化方法和使用方法

实例化方法

使用字符串初始化

?
1
- (instancetype)initWithString:(NSString *)str;

代码示例

?
1
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"测试数据"];

字典中存放一些属性名和属性值

?
1
- (instancetype)initWithString:(NSString *)str attributes:(NSDictionary<NSString *,id> *)attrs;

代码示例

?
1
2
3
4
5
6
NSDictionary *attributedDict = @{
          NSFontAttributeName:[UIFont systemFontOfSize:16.0],
          NSForegroundColorAttributeName:[UIColor redColor],
          NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick)
          };
 NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"测试数据" attributes:attributedDict];

使用NSAttributedString初始化,与NSMutableString,NSString类似

?
1
- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;

使用方法

为某一范围内的文字设置多个属性的方法

?
1
- (void)setAttributes:(NSDictionary<NSString *,id> *)attrs range:(NSRange)range;

//代码示例

?
1
2
3
4
5
6
7
8
9
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)];

为某一范围内的文字添加某个属性的方法

?
1
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;

//代码示例

?
1
2
3
4
NSString *string = @"测试数据";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
 
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, string.length)];

为某一范围内的文字添加多个属性的方法

?
1
- (void)addAttributes:(NSDictionary<NSString *,id> *)attrs range:(NSRange)range;

//代码示例

?
1
2
3
4
5
6
7
8
9
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)];

移除某个范围内的某个属性的方法

?
1
- (void)removeAttribute:(NSString *)name range:(NSRange)range;

//代码示例

?
1
2
3
4
5
6
7
8
9
10
11
12
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是UIFont对象
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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:https://www.jianshu.com/p/583417467e94

延伸 · 阅读

精彩推荐
  • IOSiOS常见的几个修饰词深入讲解

    iOS常见的几个修饰词深入讲解

    这篇文章主要给大家介绍了关于iOS常见的几个修饰词的相关资料,iOS修饰词包括assign、weak、strong、retain、copy、nonatomic、atomic、readonly、readwrite,文中通过示...

    郡王丶千夜7422021-05-10
  • IOS谈一谈iOS单例模式

    谈一谈iOS单例模式

    这篇文章主要和大家谈一谈iOS中的单例模式,单例模式是一种常用的软件设计模式,想要深入了解iOS单例模式的朋友可以参考一下...

    彭盛凇11872021-01-19
  • IOSxcode8提交ipa失败无法构建版本问题的解决方案

    xcode8提交ipa失败无法构建版本问题的解决方案

    xcode升级到xcode8后发现构建不了新的版本。怎么解决呢?下面小编给大家带来了xcode8提交ipa失败无法构建版本问题的解决方案,非常不错,一起看看吧...

    Cinna丶7542021-02-03
  • IOSiOS逆向教程之logify跟踪方法的调用

    iOS逆向教程之logify跟踪方法的调用

    这篇文章主要给大家介绍了关于iOS逆向教程之logify跟踪方法调用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学...

    Mr.Guo11472021-04-28
  • IOSIOS网络请求之AFNetWorking 3.x 使用详情

    IOS网络请求之AFNetWorking 3.x 使用详情

    本篇文章主要介绍了IOS网络请求之AFNetWorking 3.x 使用详情,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    总李写代码6892021-03-04
  • IOSiOS APP实现微信H5支付示例总结

    iOS APP实现微信H5支付示例总结

    这篇文章主要介绍了iOS APP实现微信H5支付示例总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下...

    一张小A11332021-06-01
  • IOSiOS10 Xcode8适配7个常见问题汇总

    iOS10 Xcode8适配7个常见问题汇总

    这篇文章主要为大家详细汇总了iOS10 Xcode8适配7个常见问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    索马里猫10332021-02-01
  • IOSiOS中时间与时间戳的相互转化实例代码

    iOS中时间与时间戳的相互转化实例代码

    这篇文章主要介绍了iOS中时间与时间戳的相互转化实例代码,非常具有实用价值,需要的朋友可以参考下。...

    张无忌!4812021-03-09