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).

  • 歸因字符串對象管理字符串和相關的屬性集(例如驴党,字體和字距調(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 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對象渡蜻。 您可以將任何對象值(通過任何名稱)與屬性字符串中給定的字符范圍相關聯(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 ...方法合并對屬性到字符映射的更改学密。
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市传藏,隨后出現(xiàn)的幾起案子腻暮,更是在濱河造成了極大的恐慌彤守,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件哭靖,死亡現(xiàn)場離奇詭異具垫,居然都是意外死亡,警方通過查閱死者的電腦和手機试幽,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進店門筝蚕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人铺坞,你說我怎么就攤上這事起宽。” “怎么了济榨?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵坯沪,是天一觀的道長。 經(jīng)常有香客問我擒滑,道長腐晾,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任丐一,我火速辦了婚禮赴魁,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘钝诚。我一直安慰自己,他們只是感情好榄棵,可當我...
    茶點故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布凝颇。 她就那樣靜靜地躺著,像睡著了一般疹鳄。 火紅的嫁衣襯著肌膚如雪拧略。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天瘪弓,我揣著相機與錄音垫蛆,去河邊找鬼。 笑死腺怯,一個胖子當著我的面吹牛袱饭,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播呛占,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼虑乖,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了晾虑?” 一聲冷哼從身側(cè)響起疹味,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤仅叫,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后糙捺,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體诫咱,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年洪灯,在試婚紗的時候發(fā)現(xiàn)自己被綠了坎缭。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡婴渡,死狀恐怖幻锁,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情边臼,我是刑警寧澤哄尔,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站柠并,受9級特大地震影響岭接,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜臼予,卻給世界環(huán)境...
    茶點故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一鸣戴、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧粘拾,春花似錦窄锅、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至械哟,卻和暖如春疏之,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背暇咆。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工锋爪, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人爸业。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓其骄,卻偏偏與公主長得像,于是被迫代替她去往敵國和親扯旷。 傳聞我的和親對象是個殘疾皇子年栓,可洞房花燭夜當晚...
    茶點故事閱讀 45,675評論 2 359

推薦閱讀更多精彩內(nèi)容