靈活使用nsattributedstring可以更輕松的建立出内容複雜的标簽。需要注意一點:如果一個label設定了這個屬性,那它其他的設定都将失效。
首先,我們初始化一個nsmutableattributedstring對象。
<a href="http://my.oschina.net/u/2340880/blog/397500#">?</a>
1
2
3
4
5
6
7
8
<code>//通過字元串初始化</code>
<code>//- (instancetype)initwithstring:(nsstring *)str;</code>
<code>//通過字元串和屬性字典直接初始化</code>
<code>//- (instancetype)initwithstring:(nsstring *)str attributes:(nsdictionary *)attrs;</code>
<code>//通過自身對象初始化</code>
<code>//- (instancetype)initwithattributedstring:(nsattributedstring *)attrstr;</code>
<code> </code><code>nsmutableattributedstring * attribute = [[nsmutableattributedstring alloc]initwithstring:@</code><code>"123!@#你好麼qwe"</code><code>];</code>
可以通過下面兩個函數對attrebute字元串進行設定與修改
<code>//可以替換字元</code>
<code>- (</code><code>void</code><code>)replacecharactersinrange:(nsrange)range withstring:(nsstring *)str;</code>
<code>//屬性設定</code>
<code>- (</code><code>void</code><code>)setattributes:(nsdictionary *)attrs range:(nsrange)range;</code>
<code>//設定一定範圍内字元屬性</code>
<code>- (</code><code>void</code><code>)addattribute:(nsstring *)name value:(id)value range:(nsrange)range;</code>
字典的鍵值對應如下:
9
10
11
12
13
14
15
16
17
18
<code>//kctfontattributename 這個鍵是字型的名稱 必須傳入ctfont對象</code>
<code>//kctkernattributename 這個鍵設定字型間距 傳入必須是數字對象 預設為0</code>
<code>//kctligatureattributename 這個鍵設定連字方式 必須傳入cfnumber對象</code>
<code>//kctparagraphstyleattributename 段落對其方式</code>
<code>//kctforegroundcolorattributename 字型顔色 必須傳入cgcolor對象</code>
<code>//kctstrokewidthattributename 筆畫寬度 必須是cfnumber對象</code>
<code>//kctstrokecolorattributename 筆畫顔色</code>
<code>//kctsuperscriptattributename 控制垂直文本定位 cfnumber對象</code>
<code>//kctunderlinecolorattributename 下劃線顔色</code>
<code>[attribute addattribute:(nsstring*)kctkernattributename value:@5 range:nsmakerange(0, 5)];</code>
<code>[attribute addattribute:(nsstring *)kctfontattributename</code>
<code> </code><code>value:(id)cfbridgingrelease(ctfontcreatewithname((cfstringref)[uifont boldsystemfontofsize:14].fontname,</code>
<code> </code><code>14,</code>
<code> </code><code>null))</code>
<code> </code><code>range:nsmakerange(0, 4)];</code>
<code> </code><code>[attribute addattribute:(nsstring *)kctunderlinestyleattributename</code>
<code> </code><code>value:(id)[nsnumber numberwithint:kctunderlinestyledouble]</code>
通過測試,發現上面有些鍵值并沒有作用,可以替換下面的方法,效果相同,不同的地方在于其傳值的類型不同,下面的方法更加友善(使用uifont uicolor nsstring 和一些系統枚舉)
19
20
<code> </code><code>nsparagraphstyleattributename</code>
<code>nsforegroundcolorattributename</code>
<code>nsbackgroundcolorattributename</code>
<code>nsligatureattributename</code>
<code>nskernattributename</code>
<code>nsstrikethroughstyleattributename</code>
<code>nsunderlinestyleattributename</code>
<code>nsstrokecolorattributename</code>
<code> </code><code>nsstrokewidthattributename</code>
<code> </code><code>nsshadowattributename</code>
<code> </code><code>nstexteffectattributename</code>
<code>nsattachmentattributename</code>
<code> </code><code>nslinkattributename</code>
<code> </code><code>nsbaselineoffsetattributename</code>
<code> </code><code>nsunderlinecolorattributename</code>
<code>nsstrikethroughcolorattributename</code>
<code>nsobliquenessattributename</code>
<code> </code><code>nsexpansionattributename</code>
<code> </code><code>nswritingdirectionattributename</code>
<code>nsverticalglyphformattributename</code>
<code></code>