An NSAttributedString object manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string.

这句话就是对这个类的一个最简明扼要的概括。NSAttributedString管理一个字符串,以及与该字符串中的单个字符或某些范围的字符串相关的属性。比如这个字符串“我北京天安门”,“我”跟其他字符的颜色不一样,而“北京”与其他的字体和大小不一样,等等。NSAttributedString就是用来存储这些信息的,具体实现时,NSAttributedString维护了一个NSString,用来保存最原始的字符串,另有一个NSDictionary用来保存各个子串/字符的属性。

Create Attributed String

有3种方法创建Attributed String。

1. 使用initWithString:, initWithString:attributes:, 或者 initWithAttributedString: ,下面是一个实例代码:

NSFont *font = [NSFont fontWithName:@"Palatino-Roman" size:14.0];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:fontforKey:NSFontAttributeName];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"strigil"attributes:attrsDictionary];

可以看到上面创建的整个字符串关联了Font属性。如果希望只是对某一范围的字符串施加某个属性应该使用NSMutableAttributedString的 setAttributes:range:方法。这里例子是使用了Font属性,在Appkit中特殊定义了若干属性,这些属性被用于Core Text中。其他的属性包括前景色、背景色、是否有shadow等,具体可见本文。

2. 使用initWithRTF:documentAttributes:, initWithRTFD:documentAttributes:, and initWithRTFDFileWrapper:documentAttributes:从rich text (RTF) 或者 rich text with attachments (RTFD) 数据中创建。

NSData *rtfData = ...;  // assume rtfData is an NSData object containing valid RTF data
NSDictionary *docAttributes;
NSSize paperSize;NSAttributedString *attrString;if ((attrString = [[NSAttributedString alloc]initWithRTF: rtfData documentAttributes: &docAttributes])) {NSValue *value = [docAttrs objectForKey:@"PaperSize"];paperSize = [value sizeValue];// implementation continues...

3. 使用initWithHTML:documentAttributes: 和 initWithHTML:baseURL:documentAttributes:从HTML数据中创建。有线程安全问题,使用时需要注意。

对RTF和HTML的支持都是AppKit对NSAttributedString的扩展。

Accessing Attributes

从上面对这个类的介绍可以知道,如果我们要访问某个子串/字符的属性,需要提供子串的位置和属性的名字,而如果不提供属性名字,那就把所有属性都返回。下面就是其对应的APIs:

attributesAtIndex:effectiveRange:
attributesAtIndex:longestEffectiveRange:inRange:
attribute:atIndex:effectiveRange:
attribute:atIndex:longestEffectiveRange:inRange:
fontAttributesInRange:
rulerAttributesInRange:

fontAttributesInRange: 和 rulerAttributesInRange: 是由AppKit扩展的属性。

The first four methods also return by reference the effective range and the longest effective range of the attributes. These ranges allow you to determine the extent of attributes. Conceptually, each character in an attributed string has its own collection of attributes; however, it’s often useful to know when the attributes and values are the same over a series of characters. This allows a routine to progress through an attributed string in chunks larger than a single character. In retrieving the effective range, an attributed string simply looks up information in its attribute mapping, essentially the dictionary of attributes that apply at the index requested. In retrieving the longest effective range, the attributed string continues checking characters past this basic range as long as the attribute values are the same. This extra comparison increases the execution time for these methods but guarantees a precise maximal range for the attributes requested.

Methods that return an effective range by reference are not guaranteed to return the maximal range to which the attribute(s) apply; they are merely guaranteed to return some range over which they apply. In practice they will return whatever range is readily available from the attributed string's internal storage mechanisms, which may depend on the implementation and on the precise history of modifications to the attributed string.

那些用reference返回有效范围的方法并不保证一定返回attribute应用的最大范围。它们只保证返回那些attribute有效的一些范围。实际上,它们只是返回attributed string中容易返回的那些信息,是否容易与内部实现,已经对attributed string的精确修改历史有关。

Methods that return a longest effective range by reference, on the other hand, are guaranteed to return the longest range containing the specified index to which the attribute(s) in question apply (constrained by the value of the argument passed in forinRange:). For efficiency, it is important that the inRange: argument should be as small as appropriate for the range of interest to the client.

那些返回最长有效范围的方法时能够保证返回制定attribute有效的最长的range的。为了效率,inRange: 参数应该尽量小,能满足客户需要就好。

When you iterate over an attributed string by attribute ranges, either sort of method may be appropriate depending on the situation. If there is some processing to be done for each range, and you know that the full range for a given attribute is going to have to be handled eventually, it may be more efficient to use the longest-effective-range variant, so as not to have to handle the range in pieces. However, you should use the longest-effective-range methods with caution, because the longest effective range could be quite long—potentially the entire length of the document, if the inRange: argument is not constrained.

Changing an Attributed String

NSMutableAttributedString提供若干方法,即可以修改字符串,又可以修改字符串的属性。经过多次修改后,有些信息可能变的不一致了,为了让信息保持一致,可以使用下面的方法:

fixAttributesInRange:
fixAttachmentAttributeInRange:
fixFontAttributeInRange:
fixParagraphStyleAttributeInRange:
beginEditing
endEditing

这些方法都是AppKit的扩展功能。

Reference:

1. https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/AttributedStrings/AttributedStrings.html#//apple_ref/doc/uid/10000036-BBCCGDBG

2. https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSAttributedString_AppKitAdditions/Reference/Reference.html#//apple_ref/doc/uid/20000167-BAJJCCFC

转载于:https://www.cnblogs.com/whyandinside/p/3493475.html

理解NSAttributedString相关推荐

  1. ios 表情符号 键盘_使用iOS键盘键入时,表情符号在NSAttributedString中不显示,在Android上键入时表示...

    我正在制作一个混合应用程序,当我从 Android端发送表情符号时,它在iOS端显示正常,但iOS方面不能(似乎)显示来自iOS自己键盘的表情符号! 我在显示表情符号的标签使用了属性文本,文本来自HT ...

  2. iOS 深入理解列表卡顿原理和滑动优化方案

    转自 ibireme 保持界面流程的介绍 大佬的文章太好太精彩 这篇文章会非常详细的分析 iOS 界面构建中的各种性能问题以及对应的解决思路,同时给出一个开源的微博列表实现,通过实际的代码展示如何构建 ...

  3. c语言 结构体 占位符,深入理解结构体中占位符的用法

    深入理解结构体中占位符的用法 复制代码 代码如下: typedef union { struct x{ char a1 : 2; char b1 : 3; char c1 : 3; }x1; char ...

  4. 通用解题法——回溯算法(理解+练习)

    积累算法经验,积累解题方法--回溯算法,你必须要掌握的解题方法! 什么是回溯算法呢? 回溯算法实际上一个类似枚举的搜索尝试过程,主要是在搜索尝试过程中寻找问题的解,当发现已不满足求解条件时,就&quo ...

  5. stream流对象的理解及使用

    我的理解:用stream流式处理数据,将数据用一个一个方法去 . (点,即调用) 得到新的数据结果,可以一步达成. 有多种方式生成 Stream Source: 从 Collection 和数组 Co ...

  6. Linux shell 学习笔记(11)— 理解输入和输出(标准输入、输出、错误以及临时重定向和永久重定向)

    1. 理解输入和输出 1.1 标准文件描述符 Linux 系统将每个对象当作文件处理.这包括输入和输出进程.Linux 用文件描述符(file descriptor)来标识每个文件对象.文件描述符是一 ...

  7. java局部变量全局变量,实例变量的理解

    java局部变量全局变量,实例变量的理解 局部变量 可以理解为写在方法中的变量. public class Variable {//类变量static String name = "小明&q ...

  8. 智能文档理解:通用文档预训练模型

    预训练模型到底是什么,它是如何被应用在产品里,未来又有哪些机会和挑战? 预训练模型把迁移学习很好地用起来了,让我们感到眼前一亮.这和小孩子读书一样,一开始语文.数学.化学都学,读书.网上游戏等,在脑子 ...

  9. 熵,交叉熵,散度理解较为清晰

    20210511 https://blog.csdn.net/qq_35455503/article/details/105714287 交叉熵和散度 自己给自己编码肯定是最小的 其他的编码都会比这个 ...

最新文章

  1. linux的开机网络设置
  2. 关于7z结尾的压缩包操作系统
  3. 建议重写equals方法时也一并重写hashCode方法
  4. 联想90W快充是假的吗?官方回应:我们不玩文字游戏
  5. spring-boot-starter-parent的主要作用
  6. java美图秀秀,SpringMvc整合美图秀秀M4(头像编辑器)
  7. 企业软件,WEB和对于他们无限美好的遐想(第五日)--2014-3-14--IT日 + Struts 原理分析 (四)
  8. 使用echarts模拟迁徙图
  9. ubuntu 10.04 trackpoint
  10. mac安装win10_VMware mac虚拟机安装Win10系统的详细教程
  11. Promise的三种状态
  12. springboot2.0之配置spring security记住我(rememberMe功能)不起作用的原因
  13. 上班时间如何偷偷刷抖音不被发现?教你一招搞定
  14. ue的xml格式转换_迷你档-迷你档(minidown)下载 v2.5官方版--pc6下载站
  15. 第3.8节 Python百分号占位符的字符串格式化方法
  16. 2021年10月24日马丁加德纳聚会线上主题分享开启通知
  17. OSG 之学习五:OSG 漫游
  18. CAR-T疗法新突破
  19. 新消费品牌如何做KOL营销
  20. COI2016 Palinilap(manacher+后缀数组)

热门文章

  1. LAMP 关键数据集锦技术选项参考
  2. list控件响应鼠标键的单双击
  3. Python3中上下文管理器介绍
  4. #Ruby# Introspect (2)
  5. 2021潍坊市高考成绩查询,潍坊2021高考成绩排名榜单,潍坊各高中高考成绩喜报
  6. oracle判断值是否为0的高数,SQLServer和Oracle的常用函数对比
  7. java字符存储_用java的类集框架做一个字符存储器(15)
  8. python2.7 mysql mock_Python中Mock的示例
  9. linux创建redis容器,docker-compose实现redis部署及键值添加
  10. java 重载与覆盖_Java重载与覆盖