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).
- 歸因字符串對象管理字符串和相關的屬性集(例如驴党,字體和字距調(diào)整)贬丛,這些屬性適用于字符串中的單個字符或字符范圍。 NSAttributedString和NSMutableAttributedString類分別為只讀屬性字符串和可修改的屬性字符串聲明編程接口淌铐。 Foundation Kit定義了基本功能类少,而Application Kit中定義了其他Objective-C方法倦青。 Application Kit還使用NSMutableAttributedString的子類(稱為NSTextStorage)為擴展文本處理系統(tǒng)提供存儲(請參閱文本系統(tǒng)存儲層概述)卧土。
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屬性字符串在函數(shù)或方法調(diào)用中可以與相應的橋接Core Foundation類型互換。 因此予颤,在您看到NSMutableAttributedString *參數(shù)的方法中囤官,您可以傳入CFMutableAttributedStringRef類型的變量,并且在您看到CFAttributedStringRef參數(shù)的函數(shù)中蛤虐,您可以傳入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協(xié)議蹲堂,可以方便地將屬性字符串從一種類型轉(zhuǎn)換為另一種類型。
NSAttributedString
and NSMutableAttributedString
add a number of features to the basic content storage of NSString
:
NSAttributedString和NSMutableAttributedString為NSString的基本內(nèi)容存儲添加了許多功能:
-
Association of arbitrary, programmer-defined attributes with ranges of characters.
- 任意贝淤,程序員定義的屬性與字符范圍的關聯(lián)柒竞。
-
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 toNSString
as well)- 在NSView對象中繪圖(請注意,Application Kit也將繪圖方法添加到NSString中)
-
Linguistic unit (word) and line calculation.
- 語言單位(單詞)和行計算离陶。
An attributed string identifies attributes by name, storing their values as opaque id
s 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對象渡蜻。 您可以將任何對象值(通過任何名稱)與屬性字符串中給定的字符范圍相關聯(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.
- 可變屬性字符串跟蹤屬性映射计济,因為字符被添加到其中并從中刪除茸苇,并且屬性也會更改。 它允許您使用beginEditing和endEditing方法對批量編輯進行分組沦寂,并使用fix ...方法合并對屬性到字符映射的更改学密。