NSAttributedString使用方法整理


NSAttributedString,是帶有屬性的字符串(富文本),分為NSAttributedString和NSMutableAttributedString. 可以用在 UILabel、UITextView 等處。

屬性就是一個以屬性名為 key 的字典。常見的屬性有:

 1     // NSFontAttributeName                設置字體屬性,默認值:字體:Helvetica(Neue) 字號:12
 2     // NSForegroundColorAttributeNam      設置字體顏色,取值為 UIColor對象,默認值為黑色
 3     // NSBackgroundColorAttributeName     設置字體所在區域背景顏色,取值為 UIColor對象,默認值為nil, 透明色
 4     // NSLigatureAttributeName            設置連體屬性,取值為NSNumber 對象(整數),0 表示沒有連體字符,1 表示使用默認的連體字符
 5     // NSKernAttributeName                設定字符間距,取值為 NSNumber 對象(整數),正值間距加寬,負值間距變窄
 6     // NSStrikethroughStyleAttributeName  設置刪除線,取值為 NSNumber 對象(整數)
 7     // NSStrikethroughColorAttributeName  設置刪除線顏色,取值為 UIColor 對象,默認值為黑色
 8     // NSUnderlineStyleAttributeName      設置下划線,取值為 NSNumber 對象(整數),枚舉常量 NSUnderlineStyle中的值,與刪除線類似
 9     // NSUnderlineColorAttributeName      設置下划線顏色,取值為 UIColor 對象,默認值為黑色
10     // NSStrokeWidthAttributeName         設置筆畫寬度,取值為 NSNumber 對象(整數),負值填充效果,正值中空效果
11     // NSStrokeColorAttributeName         填充部分顏色,不是字體顏色,取值為 UIColor 對象
12     // NSShadowAttributeName              設置陰影屬性,取值為 NSShadow 對象
13     // NSTextEffectAttributeName          設置文本特殊效果,取值為 NSString 對象,目前只有圖版印刷效果可用:
14     // NSBaselineOffsetAttributeName      設置基線偏移值,取值為 NSNumber (float),正值上偏,負值下偏
15     // NSObliquenessAttributeName         設置字形傾斜度,取值為 NSNumber (float),正值右傾,負值左傾
16     // NSExpansionAttributeName           設置文本橫向拉伸屬性,取值為 NSNumber (float),正值橫向拉伸文本,負值橫向壓縮文本
17     // NSWritingDirectionAttributeName    設置文字書寫方向,從左向右書寫或者從右向左書寫
18     // NSVerticalGlyphFormAttributeName   設置文字排版方向,取值為 NSNumber 對象(整數),0 表示橫排文本,1 表示豎排文本
19     // NSLinkAttributeName                設置鏈接屬性,點擊后調用瀏覽器打開指定URL地址
20     // NSAttachmentAttributeName          設置文本附件,取值為NSTextAttachment對象,常用於文字圖片混排
21     // NSParagraphStyleAttributeName      設置文本段落排版格式,取值為 NSParagraphStyle 對象

使用方法:

1.初始化一個NSMutableAttributedString,給它添加屬性,再把它賦給控件的AttributedText,適用於文本較少又需要精細控制的情況。

 1     NSString *originStr = @"測試富文本屬性";
 2     //創建 NSMutableAttributedString
 3     NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString: originStr];
 4     
 5     //添加屬性
 6     //設置全部字體為14號粗體
 7     [attributedStr addAttribute: NSFontAttributeName value: [UIFont  boldSystemFontOfSize:14]
 8                             range: NSMakeRange(0, originStr.length)];
 9     //設置前4個字體為藍色
10     [attributedStr addAttribute: NSForegroundColorAttributeName value: [UIColor blueColor] range: NSMakeRange(0, 4)];
11     
12     //賦值給顯示控件label的 attributedText
13     _label.attributedText = attributedStr;

2.創建一個屬性字典,初始化各項屬性。然后和需要控制的文本一起創建並賦值給控件的AttributedText,該方法適合於需要控制的文本較多整體控制的情況,通常是從文件中讀取的大段文本控制。

1     NSString *originStr = @"測試富文本屬性";
2     //創建屬性字典
3     NSDictionary *attrDict = @{NSFontAttributeName: [UIFont systemFontOfSize:15],
4                                NSForegroundColorAttributeName: [UIColor blueColor]};
5     //將字符串和屬性賦給控件的 AttributedText,也可以指定 range
6     label.attributedText = [[NSAttributedString alloc] initWithString:[originStr substringWithRange:NSMakeRange(0, 6)] attributes:attrDict];

使用詳解:

1.NSForegroundColorAttributeName設置的顏色與UILabel的textColor屬性設置的顏色在地位上是相等的,誰最后賦值,最終顯示的就是誰的顏色。但是textColor屬性可以與 NSBackgroundColorAttributeName 屬性疊加。

2. NSStrikethroughStyleAttributeName用來設置NSUnderlineStyle中的值

1 // NSUnderlineStyleNone   不設置刪除線
2 // NSUnderlineStyleSingle 設置刪除線為細單實線
3 // NSUnderlineStyleThick  設置刪除線為粗單實線
4 // NSUnderlineStyleDouble 設置刪除線為細雙實線
這些枚舉常量是包含一個整數的 NSNumber,所以同樣必須先轉化為 NSNumber 才能使用刪除線和下划線使用相同的枚舉常量作為其屬性值。
1 NSDictionary *attrDict = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle),
2                              NSFontAttributeName: [UIFont systemFontOfSize:20] };
3 _label.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict];
中文和英文刪除線的位置有所不同。
另外,刪除線屬性取值除了上面的4種外,其實還可以取其他整數值:取值為 0 - 7時,效果為單實線,隨着值得增加,單實線逐漸變粗,取值為 9 - 15時,效果為雙實線,取值越大,雙實線越粗。
1 NSDictionary *attrDict = @{ NSStrikethroughStyleAttributeName: @(1),
2                               NSFontAttributeName: [UIFont systemFontOfSize:20] };
3 _label.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict];

3.下划線和刪除線類似,可以參考設置

 1         NSUnderlineStyleNone //沒有下划線
 2         NSUnderlineStyleSingle //單下划線
 3         NSUnderlineStyleThick //粗下划線
 4         NSUnderlineStyleDouble //雙下划線
 5         NSUnderlinePatternSolid //實心下划線
 6         NSUnderlinePatternDot //點下划線
 7         NSUnderlinePatternDash //破折號下划線
 8         NSUnderlinePatternDashDot //破折號和點下划線
 9         NSUnderlinePatternDashDotDot //一個破折號和兩個點的下划線
10         NSUnderlineByWord //下划線緊貼文字

4.NSParagraphStyleAttributeName 設置段落樣式,包括:

 1 //alignment 對齊;
 2 //firstLineHeadIndent:首行縮進
 3 //tailIndent:段落尾部縮進
 4 //headIndent:段落前部縮進(左縮進?)
 5 //maximumLineHeight:最大行高
 6 //minimumLineHeight:最小行高
 7 //lineSpacing:行間距
 8 //paragraphSpacing:段落間距
 9 //lineBreakMode:斷行模式
10 //defaultWritingDirectionForLanguage:默認書寫方向(可以指定某種語言)
11 //baseWritingDirection(基礎的書寫方向)

lineBreakMode有6種:

1 //NSLineBreakByCharWrapping;以字符為顯示單位顯示,后面部分省略不顯示。
2 //NSLineBreakByClipping;剪切與文本寬度相同的內容長度,后半部分被刪除。
3 //NSLineBreakByTruncatingHead;前面部分文字以……方式省略,顯示尾部文字內容。
4 //NSLineBreakByTruncatingMiddle;中間的內容以……方式省略,顯示頭尾的文字內容。
5 //NSLineBreakByTruncatingTail;結尾部分的內容以……方式省略,顯示頭的文字內容。
6 //NSLineBreakByWordWrapping;以單詞為顯示單位顯示,后面部分省略不顯示。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM