富文字帶屬性的字串NSMutableAttributedString
阿新 • • 發佈:2019-01-03
// NSFontAttributeName 設定字型屬性,預設值:字型:Helvetica(Neue) 字號:12
// NSForegroundColorAttributeNam 設定字型顏色,取值為 UIColor物件,預設值為黑色
// NSBackgroundColorAttributeName 設定字型所在區域背景顏色,取值為 UIColor物件,預設值為nil, 透明色
// NSLigatureAttributeName 設定連體屬性,取值為NSNumber 物件(整數),0 表示沒有連體字元,1 表示使用預設的連體字元
// NSKernAttributeName 設定字元間距,取值為 NSNumber 物件(整數),正值間距加寬,負值間距變窄
// NSStrikethroughStyleAttributeName 設定刪除線,取值為 NSNumber 物件(整數)
// NSStrikethroughColorAttributeName 設定刪除線顏色,取值為 UIColor 物件,預設值為黑色
// NSUnderlineStyleAttributeName 設定下劃線,取值為 NSNumber 物件(整數),列舉常量 NSUnderlineStyle中的值,與刪除線類似
// NSUnderlineColorAttributeName 設定下劃線顏色,取值為 UIColor 物件,預設值為黑色
// NSStrokeWidthAttributeName 設定筆畫寬度,取值為 NSNumber 物件(整數),負值填充效果,正值中空效果
// NSStrokeColorAttributeName 填充部分顏色,不是字型顏色,取值為 UIColor 物件
// NSShadowAttributeName 設定陰影屬性,取值為 NSShadow 物件
// NSTextEffectAttributeName 設定文字特殊效果,取值為 NSString 物件,目前只有圖版印刷效果可用:
// NSBaselineOffsetAttributeName 設定基線偏移值,取值為 NSNumber (float),正值上偏,負值下偏
// NSObliquenessAttributeName 設定字形傾斜度,取值為 NSNumber (float),正值右傾,負值左傾
// NSExpansionAttributeName 設定文字橫向拉伸屬性,取值為 NSNumber (float),正值橫向拉伸文字,負值橫向壓縮文字
// NSWritingDirectionAttributeName 設定文字書寫方向,從左向右書寫或者從右向左書寫
// NSVerticalGlyphFormAttributeName 設定文字排版方向,取值為 NSNumber 物件(整數),0 表示橫排文字,1 表示豎排文字
// NSLinkAttributeName 設定連結屬性,點選後呼叫瀏覽器開啟指定URL地址
// NSAttachmentAttributeName 設定文字附件,取值為NSTextAttachment物件,常用於文字圖片混排
// NSParagraphStyleAttributeName 設定文字段落排版格式,取值為 NSParagraphStyle 物件
下面就一一舉例說明:
1. NSFontAttributeName
//NSForegroundColorAttributeName 設定字型顏色,取值為 UIColor,預設為黑色
NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };
NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };
NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };
_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
注意:
NSForegroundColorAttributeName設定的顏色與UILabel的textColor屬性設定的顏色在地位上是相等的,誰最後賦值,最終顯示的就是誰的顏色。
2. NSBackgroundColorAttributeName
//NSForegroundColorAttributeName 設定字型顏色,取值為 UIColor,預設為黑色
NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };
NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };
NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };
_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
//NSBackgroundColorAttributeName 設定字型所在區域背景的顏色,取值為UIColor,預設值為nil
NSDictionary *attrDict4 = @{ NSBackgroundColorAttributeName: [UIColor orangeColor] };
NSDictionary *attrDict5 = @{ NSBackgroundColorAttributeName: [UIColor redColor] };
NSDictionary *attrDict6 = @{ NSBackgroundColorAttributeName: [UIColor cyanColor] };
_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict4];
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict5];
_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict6];
仔細觀察會發現個問題,我並沒有關閉 NSForegroundColorAttributeName 屬性,但是在執行結果中,所有字型的顏色都變成了預設色——黑色,這說明 NSForegroundColorAttributeName 和 NSBackgroundColorAttributeName 的低位是相等的,跟前面介紹的 textColor 一樣,哪個屬性最後一次賦值,就會沖掉前面的效果,若是我們把屬性程式碼順序交換一下
//NSBackgroundColorAttributeName 設定字型所在區域背景的顏色,取值為UIColor,預設值為nil
NSDictionary *attrDict4 = @{ NSBackgroundColorAttributeName: [UIColor orangeColor] };
NSDictionary *attrDict5 = @{ NSBackgroundColorAttributeName: [UIColor redColor] };
NSDictionary *attrDict6 = @{ NSBackgroundColorAttributeName: [UIColor cyanColor] };
_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict4];
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict5];
_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict6];
//NSForegroundColorAttributeName 設定字型顏色,取值為 UIColor,預設為黑色
NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };
NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };
NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };
_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
但是textColor屬性可以與 NSBackgroundColorAttributeName 屬性疊加
_label01.textColor = [UIColor greenColor];
_label02.textColor = [UIColor yellowColor];
_label03.textColor = [UIColor blueColor];
//NSForegroundColorAttributeName 設定字型顏色,取值為 UIColor,預設為黑色
NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };
NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };
NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };
_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
//NSBackgroundColorAttributeName 設定字型所在區域背景的顏色,取值為UIColor,預設值為nil
NSDictionary *attrDict4 = @{ NSBackgroundColorAttributeName: [UIColor orangeColor] };
NSDictionary *attrDict5 = @{ NSBackgroundColorAttributeName: [UIColor redColor] };
NSDictionary *attrDict6 = @{ NSBackgroundColorAttributeName: [UIColor cyanColor] };
_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict4];
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict5];
_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict6];
雖然 textColor 在 NSFontAttributeName 之前賦值,但是由於 NSFontAttributeName 的屬性效果被NSBackgroundColorAttributeName 屬性沖掉了,所以最終顯示了 textColor 的顏色。
3. NSLigatureAttributeName
//NSLigatureAttributeName 設定連體屬性,取值為NSNumber 物件(整數),0 表示沒有連體字元,1 表示使用預設的連體字元,
// 2 表示使用所有連體符號,預設值為 1(iOS 不支援 2)
NSString *ligatureStr = @"flush";
NSDictionary *attrDict1 = @{ NSLigatureAttributeName: [NSNumber numberWithInt: 0],
NSFontAttributeName: [UIFont fontWithName: @"futura" size: 30] };
_label01.attributedText = [[NSAttributedString alloc] initWithString: ligatureStr attributes: attrDict1];
NSDictionary *attrDict2 = @{ NSLigatureAttributeName: @(1),
NSFontAttributeName: [UIFont fontWithName: @"futura" size: 30]
};
_label02.attributedText = [[NSAttributedString alloc] initWithString: ligatureStr attributes: attrDict2];
由於要展示連體字元,所以將前面使用的帶有中文的字串換成 flush
NSLigatureAttributeName的取值為NSNumber物件,所以不能直接將一個整數值賦給它,建立 NSNumber 物件的方法有很多,或者可以簡寫成 @(int)
注意觀察字母f和l之間的變化。
感覺連寫就是一個藝術字功能,當字元f和l組合使用組合符號(所謂的字形(glyph))繪製時,看起來確實更加美觀。但是並非所有的字元之間都有組合符號,事實上,只有某些字型中得某些字元的組合(如字元f和l,字元f和i等)才具有美觀的組合符號。
4. NSKernAttributeName
//NSKernAttributeName 設定字元間距,取值為 NSNumber 物件(整數),正值間距加寬,負值間距變窄
NSDictionary *attrDict1 = @{ NSKernAttributeName: @(-3),
NSFontAttributeName: [UIFont systemFontOfSize: 20]
};
_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
NSDictionary *attrDict2 = @{ NSKernAttributeName: @(0),
NSFontAttributeName: [UIFont systemFontOfSize: 20]
};
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
NSDictionary *attrDict3 = @{ NSKernAttributeName: @(10),
NSFontAttributeName: [UIFont systemFontOfSize: 20]
};
_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
5. NSStrikethroughStyleAttributeName
//NSStrikethroughStyleAttributeName 設定刪除線,取值為 NSNumber 物件(整數),列舉常量 NSUnderlineStyle中的值
// NSUnderlineStyleNone 不設定刪除線
// NSUnderlineStyleSingle 設定刪除線為細單實線
// NSUnderlineStyleThick 設定刪除線為粗單實線
// NSUnderlineStyleDouble 設定刪除線為細雙實線
NSDictionary *attrDict1 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle),
NSFontAttributeName: [UIFont systemFontOfSize:20] };
_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
NSDictionary *attrDict2 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleThick),
NSFontAttributeName: [UIFont systemFontOfSize:20] };
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
NSDictionary *attrDict3 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleDouble),
NSFontAttributeName: [UIFont systemFontOfSize:20] };
_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
注意:
雖然使用了列舉常量,但是列舉常量的本質仍為整數,所以同樣必須先轉化為 NSNumber 才能使用
刪除線和下劃線使用相同的列舉常量作為其屬性值
目前iOS中只有上面列出的4中效果,雖然我們能夠在標頭檔案中發現其他更多的取值,但是使用後沒有任何效果
可以看出,中文和英文刪除線的位置有所不同
另外,刪除線屬性取值除了上面的4種外,其實還可以取其他整數值,有興趣的可以自行試驗,取值為 0 - 7時,效果為單實線,隨著值得增加,單實線逐漸變粗,取值為 9 - 15時,效果為雙實線,取值越大,雙實線越粗。
NSDictionary *attrDict1 = @{ NSStrikethroughStyleAttributeName: @(1),
NSFontAttributeName: [UIFont systemFontOfSize:20] };
_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
NSDictionary *attrDict2 = @{ NSStrikethroughStyleAttributeName: @(3),
NSFontAttributeName: [UIFont systemFontOfSize:20] };
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
NSDictionary *attrDict3 = @{ NSStrikethroughStyleAttributeName: @(7),
NSFontAttributeName: [UIFont systemFontOfSize:20] };
_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
6. NSStrikethroughColorAttributeName
//NSStrikethroughColorAttributeName 設定刪除線顏色,取值為 UIColor 物件,預設值為黑色
NSDictionary *attrDict1 = @{ NSStrikethroughColorAttributeName: [UIColor blueColor],
NSStrikethroughStyleAttributeName: @(1),
NSFontAttributeName: [UIFont systemFontOfSize:20] };
_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
NSDictionary *attrDict2 = @{ NSStrikethroughColorAttributeName: [UIColor orangeColor],
NSStrikethroughStyleAttributeName: @(3),
NSFontAttributeName: [UIFont systemFontOfSize:20] };
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
NSDictionary *attrDict3 = @{ NSStrikethroughColorAttributeName: [UIColor greenColor],
NSStrikethroughStyleAttributeName: @(7),
NSFontAttributeName: [UIFont systemFontOfSize:20] };
_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
7. NSUnderlineStyleAttributeName
下劃線除了線條位置和刪除線不同外,其他的都可以完全參照刪除線設定。
//NSUnderlineStyleAttributeName 設定下劃線,取值為 NSNumber 物件(整數),列舉常量 NSUnderlineStyle中的值,與刪除線類似
NSDictionary *attrDict1 = @{ NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),
NSFontAttributeName: [UIFont systemFontOfSize:20] };
_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
NSDictionary *attrDict2 = @{ NSUnderlineStyleAttributeName: @(NSUnderlineStyleThick),
NSFontAttributeName: [UIFont systemFontOfSize:20] };
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
NSDictionary *attrDict3 = @{ NSUnderlineStyleAttributeName: @(NSUnderlineStyleDouble),
NSFontAttributeName: [UIFont systemFontOfSize:20] };
_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
8. NSUnderlineColorAttributeName
可以完全參照下劃線顏色設定
//NSUnderlineColorAttributeName 設定下劃線顏色,取值為 UIColor 物件,預設值為黑色
NSDictionary *attrDict1 = @{ NSUnderlineColorAttributeName: [UIColor blueColor],
NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),
NSFontAttributeName: [UIFont systemFontOfSize:20] };
_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
NSDictionary *attrDict2 = @{ NSUnderlineColorAttributeName: [UIColor orangeColor],
NSUnderlineStyleAttributeName: @(NSUnderlineStyleThick),
NSFontAttributeName: [UIFont systemFontOfSize:20] };
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
NSDictionary *attrDict3 = @{ NSUnderlineColorAttributeName: [UIColor greenColor],
NSUnderlineStyleAttributeName: @(NSUnderlineStyleDouble),
NSFontAttributeName: [UIFont systemFontOfSize:20] };
_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
9. NSStrokeWidthAttributeName
//NSStrokeWidthAttributeName 設定筆畫寬度,取值為 NSNumber 物件(整數),負值填充效果,正值中空效果
NSDictionary *attrDict1 = @{ NSStrokeWidthAttributeName: @(-3),
NSFontAttributeName: [UIFont systemFontOfSize:30] };
_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
NSDictionary *attrDict2 = @{ NSStrokeWidthAttributeName: @(0),
NSFontAttributeName: [UIFont systemFontOfSize:30] };
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
NSDictionary *attrDict3 = @{ NSStrokeWidthAttributeName: @(3),
NSFontAttributeName: [UIFont systemFontOfSize:30] };
_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
10. NSStrokeColorAttributeName
//NSStrokeColorAttributeName 填充部分顏色,不是字型顏色,取值為 UIColor 物件
NSDictionary *attrDict1 = @{ NSStrokeWidthAttributeName: @(-3),
NSStrokeColorAttributeName: [UIColor orangeColor],
NSFontAttributeName: [UIFont systemFontOfSize:30] };
_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
NSDictionary *attrDict2 = @{ NSStrokeWidthAttributeName: @(0),
NSStrokeColorAttributeName: [UIColor blueColor],
NSFontAttributeName: [UIFont systemFontOfSize:30] };
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
NSDictionary *attrDict3 = @{ NSStrokeWidthAttributeName: @(3),
NSStrokeColorAttributeName: [UIColor greenColor],
NSFontAttributeName: [UIFont systemFontOfSize:30] };
_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];