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

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

前言

在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是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

总结

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

相关文章

  • iOS实现带文字的圆形头像效果

    iOS实现带文字的圆形头像效果

    随着腾讯QQ的普及,现在越来越多的社交类APP在显示头像的时候,都选择了圆形头像,本文将更进一步的介绍如何实现带文字的圆形头像效果,效果非常不错,感兴趣的朋友们可以参考借鉴,下面来一起看看吧。
    2016-10-10
  • IOS多线程编程NSThread的使用方法

    IOS多线程编程NSThread的使用方法

    这篇文章主要介绍了IOS多线程编程NSThread的使用方法的相关资料,希望通过本文能帮助到大家,让大家理解使用多线程的方法,需要的朋友可以参考下
    2017-10-10
  • 百度地图PC端判断用户是否在配送范围内

    百度地图PC端判断用户是否在配送范围内

    在pc端设置商家的配送范围,用户在下单时,根据用户设置的配送地点判断是否在可配送范围内,并给用户相应的提示,下面通过本文给大家分享具有实现代码,感兴趣的朋友一起学习吧
    2016-01-01
  • iOS上下拉刷新控件MJRefresh使用方法详解

    iOS上下拉刷新控件MJRefresh使用方法详解

    这篇文章主要为大家详细介绍了iOS上下拉刷新控件MJRefresh的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • IOS开发之CocoaPods安装和使用教程

    IOS开发之CocoaPods安装和使用教程

    CocoaPods应该是iOS最常用最有名的类库管理工具了,通过cocoaPods,只需要一行命令就可以完全解决,当然前提是你必须正确设置它。重要的是,绝大部分有名的开源类库,都支持CocoaPods。所以,作为iOS程序员的我们,掌握CocoaPods的使用是必不可少的基本技能了。
    2014-09-09
  • IOS NSNotification 键盘遮挡问题的解决办法

    IOS NSNotification 键盘遮挡问题的解决办法

    这篇文章主要介绍了IOS NSNotification 键盘遮挡问题的解决办法的相关资料,希望通过本文能帮助到大家,解决这样的问题,需要的朋友可以参考下
    2017-09-09
  • iOS键盘自适应弹出效果

    iOS键盘自适应弹出效果

    这篇文章主要为大家详细介绍了iOS键盘自适应弹出效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • iOS仿微博客户端一条微博的展示效果

    iOS仿微博客户端一条微博的展示效果

    这篇文章主要为大家详细介绍了iOS仿微博客户端,一条微博的布局,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • ios中图像进行压缩方法汇总

    ios中图像进行压缩方法汇总

    在Iphone上有两种读取图片数据的简单方法: UIImageJPEGRepresentation和UIImagePNGRepresentation. UIImageJPEGRepresentation函数需要两个参数:图片的引用和压缩系数.而UIImagePNGRepresentation只需要图片引用作为参数.
    2015-05-05
  • IOS实现的简单画板功能

    IOS实现的简单画板功能

    本文主要介绍了IOS实现简单画板的示例代码。具有很好的参考价值。下面跟着小编一起来看下吧
    2017-04-04

最新评论