02. Attributed Strings

Attributed string objects manage character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string. The classes NSAttributedString and NSMutableAttributedString declare the programmatic interface for read-only attributed strings and modifiable attributed strings, respectively. The Foundation Kit defines the basic functionality, while additional Objective-C methods are defined in the Application Kit. The Application Kit also uses a subclass of NSMutableAttributedString, called NSTextStorage, to provide the storage for the extended text-handling system (see Text System Storage Layer Overview).

  • 归因字符串对象管理字符串和相关的属性集(例如,字体和字距调整),这些属性适用于字符串中的单个字符或字符范围。 NSAttributedString和NSMutableAttributedString类分别为只读属性字符串和可修改的属性字符串声明编程接口。 Foundation Kit定义了基本功能,而Application Kit中定义了其他Objective-C方法。 Application Kit还使用NSMutableAttributedString的子类(称为NSTextStorage)为扩展文本处理系统提供存储(请参阅文本系统存储层概述)。

NSAttributedString and NSMutableAttributedString are toll-free bridged to their Core Foundation counterparts, CFAttributedString and CFMutableAttributedString respectively. This means that a Foundation attributed string is interchangeable in function or method calls with the corresponding bridged Core Foundation type. Therefore, in a method where you see an NSMutableAttributedString * parameter, you can pass in a variable of type CFMutableAttributedStringRef, and in a function where you see a CFAttributedStringRef parameter, you can pass in an instance of NSAttributedString (or NSMutableAttributedString).

  • NSAttributedString和NSMutableAttributedString分别免费桥接到他们的Core Foundation对应物,CFAttributedString和CFMutableAttributedString。 这意味着Foundation属性字符串在函数或方法调用中可以与相应的桥接Core Foundation类型互换。 因此,在您看到NSMutableAttributedString *参数的方法中,您可以传入CFMutableAttributedStringRef类型的变量,并且在您看到CFAttributedStringRef参数的函数中,您可以传入NSAttributedString(或NSMutableAttributedString)的实例。

NSAttributedString is not a subclass of NSString. It contains an NSString object to which it applies attributes. This protects users of attributed strings from ambiguities caused by the semantic differences between simple and attributed strings. For example, equality can’t be simply defined between an NSString and an attributed string. The attributed string classes adopt the NSCopying and NSMutableCopying protocols, making it convenient to convert an attributed string from one type to the other.

  • NSAttributedString不是NSString的子类。 它包含一个应用属性的NSString对象。 这可以保护属性字符串的用户免受由简单字符串和属性字符串之间的语义差异引起的歧义。 例如,不能在NSString和属性字符串之间简单地定义相等性。 属性字符串类采用NSCopying和NSMutableCopying协议,可以方便地将属性字符串从一种类型转换为另一种类型。

NSAttributedString and NSMutableAttributedString add a number of features to the basic content storage of NSString:
NSAttributedString和NSMutableAttributedString为NSString的基本内容存储添加了许多功能:

  • Association of arbitrary, programmer-defined attributes with ranges of characters.

    • 任意,程序员定义的属性与字符范围的关联。
  • Preservation of attribute-to-character mapping after changes (NSMutableAttributedString).

    • 更改后保留属性到字符的映射(NSMutableAttributedString)。
  • Support for RTF, including file attachments and graphics.

    • 支持RTF,包括文件附件和图形。
  • Drawing in NSView objects (note that the Application Kit adds drawing methods to NSString as well)

    • 在NSView对象中绘图(请注意,Application Kit也将绘图方法添加到NSString中)
  • Linguistic unit (word) and line calculation.

    • 语言单位(单词)和行计算。

An attributed string identifies attributes by name, storing their values as opaque ids in an NSDictionary object. For example, the text font is stored as an NSFont object under the name given by NSFontAttributeName. You can associate any object value, by any name, with a given range of characters in the attributed string.

  • 属性字符串按名称标识属性,将其值作为不透明的id存储在NSDictionary对象中。 例如,文本字体以NSFontAttributeName给出的名称存储为NSFont对象。 您可以将任何对象值(通过任何名称)与属性字符串中给定的字符范围相关联。

A mutable attributed string keeps track of the attribute mapping as characters are added to and deleted from it and as attributes are changed. It allows you to group batches of edits with the beginEditing and endEditing methods, and to consolidate changes to the attribute-to-character mapping with the fix... methods.

  • 可变属性字符串跟踪属性映射,因为字符被添加到其中并从中删除,并且属性也会更改。 它允许您使用beginEditing和endEditing方法对批量编辑进行分组,并使用fix ...方法合并对属性到字符映射的更改。

你可能感兴趣的:(02. Attributed Strings)