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).
- 歸因字符串對(duì)象管理字符串和相關(guān)的屬性集(例如,字體和字距調(diào)整),這些屬性適用于字符串中的單個(gè)字符或字符范圍。 NSAttributedString和NSMutableAttributedString類分別為只讀屬性字符串和可修改的屬性字符串聲明編程接口。 Foundation Kit定義了基本功能,而Application Kit中定義了其他Objective-C方法。 Application Kit還使用NSMutableAttributedString的子類(稱為NSTextStorage)為擴(kuò)展文本處理系統(tǒng)提供存儲(chǔ)(請(qǐng)參閱文本系統(tǒng)存儲(chǔ)層概述)。
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分別免費(fèi)橋接到他們的Core Foundation對(duì)應(yīng)物,CFAttributedString和CFMutableAttributedString。 這意味著Foundation屬性字符串在函數(shù)或方法調(diào)用中可以與相應(yīng)的橋接Core Foundation類型互換。 因此,在您看到NSMutableAttributedString *參數(shù)的方法中,您可以傳入CFMutableAttributedStringRef類型的變量,并且在您看到CFAttributedStringRef參數(shù)的函數(shù)中,您可以傳入NSAttributedString(或NSMutableAttributedString)的實(shí)例。
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的子類。 它包含一個(gè)應(yīng)用屬性的NSString對(duì)象。 這可以保護(hù)屬性字符串的用戶免受由簡(jiǎn)單字符串和屬性字符串之間的語(yǔ)義差異引起的歧義。 例如,不能在NSString和屬性字符串之間簡(jiǎn)單地定義相等性。 屬性字符串類采用NSCopying和NSMutableCopying協(xié)議,可以方便地將屬性字符串從一種類型轉(zhuǎn)換為另一種類型。
NSAttributedString and NSMutableAttributedString add a number of features to the basic content storage of NSString:
NSAttributedString和NSMutableAttributedString為NSString的基本內(nèi)容存儲(chǔ)添加了許多功能:
-
Association of arbitrary, programmer-defined attributes with ranges of characters.
- 任意,程序員定義的屬性與字符范圍的關(guān)聯(lián)。
-
Preservation of attribute-to-character mapping after changes (
NSMutableAttributedString).- 更改后保留屬性到字符的映射(NSMutableAttributedString)。
-
Support for RTF, including file attachments and graphics.
- 支持RTF,包括文件附件和圖形。
-
Drawing in
NSViewobjects (note that the Application Kit adds drawing methods toNSStringas well)- 在NSView對(duì)象中繪圖(請(qǐng)注意,Application Kit也將繪圖方法添加到NSString中)
-
Linguistic unit (word) and line calculation.
- 語(yǔ)言單位(單詞)和行計(jì)算。
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.
- 屬性字符串按名稱標(biāo)識(shí)屬性,將其值作為不透明的id存儲(chǔ)在NSDictionary對(duì)象中。 例如,文本字體以NSFontAttributeName給出的名稱存儲(chǔ)為NSFont對(duì)象。 您可以將任何對(duì)象值(通過(guò)任何名稱)與屬性字符串中給定的字符范圍相關(guān)聯(lián)。
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.
- 可變屬性字符串跟蹤屬性映射,因?yàn)樽址惶砑拥狡渲胁闹袆h除,并且屬性也會(huì)更改。 它允許您使用beginEditing和endEditing方法對(duì)批量編輯進(jìn)行分組,并使用fix ...方法合并對(duì)屬性到字符映射的更改。