-
是不是一直在找富文本的相關(guān)信息,哈哈,別找了,就在這呢.
NSFontAttributeName(字體)
該屬性所對(duì)應(yīng)的值是一個(gè) UIFont 對(duì)象。該屬性用于改變一段文本的字體。如果不指定該屬性,則默認(rèn)為12-point Helvetica(Neue)。
NSParagraphStyleAttributeName(段落)
該屬性所對(duì)應(yīng)的值是一個(gè) NSParagraphStyle 對(duì)象。該屬性在一段文本上應(yīng)用多個(gè)屬性。如果不指定該屬性,則默認(rèn)為 NSParagraphStyle 的defaultParagraphStyle 方法返回的默認(rèn)段落屬性。
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentCenter;
NSForegroundColorAttributeName(字體顏色)
該屬性所對(duì)應(yīng)的值是一個(gè) UIColor 對(duì)象。該屬性用于指定一段文本的字體顏色。如果不指定該屬性,則默認(rèn)為黑色。
// NSForegroundColorAttributeName 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 設(shè)置的顏色與 UILabel 的 textColor 屬性設(shè)置的顏色在地位上是相等的,與 NSBackgroundColorAttributeName 地位上也相等,誰(shuí)最后賦值,最終顯示的就是誰(shuí)的顏色,但是textColor屬性可以與 NSBackgroundColorAttributeName 屬性可疊加。
NSBackgroundColorAttributeName(字體背景色)
該屬性所對(duì)應(yīng)的值是一個(gè) UIColor 對(duì)象。該屬性用于指定一段文本的背景顏色。如果不指定該屬性,則默認(rèn)無(wú)背景色。
NSLigatureAttributeName(連字符)
該屬性所對(duì)應(yīng)的值是一個(gè) NSNumber 對(duì)象(整數(shù))。連體字符是指某些連在一起的字符,它們采用單個(gè)的圖元符號(hào)。0 表示沒(méi)有連體字符。1 表示使用默認(rèn)的連體字符。2表示使用所有連體符號(hào)。默認(rèn)值為 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對(duì)象,所以不能直接將一個(gè)整數(shù)值賦給它,創(chuàng)建 NSNumber 對(duì)象的方法有很多,或者可以簡(jiǎn)寫(xiě)成 @(int)
注意觀察字母f和l之間的變化。
感覺(jué)連寫(xiě)就是一個(gè)藝術(shù)字功能,當(dāng)字符f和l組合使用組合符號(hào)(所謂的字形(glyph))繪制時(shí),看起來(lái)確實(shí)更加美觀。但是并非所有的字符之間都有組合符號(hào),事實(shí)上,只有某些字體中得某些字符的組合(如字符f和l,字符f和i等)才具有美觀的組合符號(hào)。
NSKernAttributeName(字間距)
NSKernAttributeName 設(shè)定字符間距,取值為 NSNumber 對(duì)象(整數(shù)),正值間距加寬,負(fù)值間距變窄
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];
NSStrikethroughStyleAttributeName(刪除線(xiàn))
NSStrikethroughStyleAttributeName 設(shè)置刪除線(xiàn),取值為 NSNumber 對(duì)象(整數(shù)),枚舉常量 NSUnderlineStyle中的值:
- NSUnderlineStyleNone 不設(shè)置刪除線(xiàn)
- NSUnderlineStyleSingle 設(shè)置刪除線(xiàn)為細(xì)單實(shí)線(xiàn)
- NSUnderlineStyleThick 設(shè)置刪除線(xiàn)為粗單實(shí)線(xiàn)
- NSUnderlineStyleDouble 設(shè)置刪除線(xiàn)為細(xì)雙實(shí)線(xiàn)
默認(rèn)值是NSUnderlineStyleNone。
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];
注意
- 雖然使用了枚舉常量,但是枚舉常量的本質(zhì)仍為整數(shù),所以同樣必須先轉(zhuǎn)化為 NSNumber 才能使用
- 刪除線(xiàn)和下劃線(xiàn)使用相同的枚舉常量作為其屬性值
- 目前iOS中只有上面列出的4中效果,雖然我們能夠在頭文件中發(fā)現(xiàn)其他更多的取值,但是使用后沒(méi)有任何效果
可以看出,中文和英文刪除線(xiàn)的位置有所不同
另外,刪除線(xiàn)屬性取值除了上面的4種外,其實(shí)還可以取其他整數(shù)值,有興趣的可以自行試驗(yàn),取值為 0 - 7時(shí),效果為單實(shí)線(xiàn),隨著值得增加,單實(shí)線(xiàn)逐漸變粗,取值為 9 - 15時(shí),效果為雙實(shí)線(xiàn),取值越大,雙實(shí)線(xiàn)越粗。
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];
NSStrikethroughColorAttributeName
NSStrikethroughColorAttributeName 設(shè)置刪除線(xiàn)顏色,取值為 UIColor 對(duì)象,默認(rèn)值為黑色
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];
NSUnderlineStyleAttributeName(下劃線(xiàn))
該屬性所對(duì)應(yīng)的值是一個(gè) NSNumber 對(duì)象(整數(shù))。該值指定是否在文字上加上下劃線(xiàn),該值參考“Underline Style Attributes”。默認(rèn)值是NSUnderlineStyleNone。
下劃線(xiàn)除了線(xiàn)條位置和刪除線(xiàn)不同外,其他的都可以完全參照刪除線(xiàn)設(shè)置。
NSUnderlineColorAttributeName
NSUnderlineColorAttributeName 設(shè)置下劃線(xiàn)顏色,取值為 UIColor 對(duì)象,默認(rèn)值為黑色
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];
NSStrokeColorAttributeName(邊線(xiàn)顏色) 和 NSStrokeWidthAttributeName(邊線(xiàn)寬度)
該值改變筆畫(huà)寬度(相對(duì)于字體 size 的百分比),負(fù)值填充效果,正值中空效果,默認(rèn)為 0,即不改變。正數(shù)只改變描邊寬度。負(fù)數(shù)同時(shí)改變文字的描邊和填充寬度。例如,對(duì)于常見(jiàn)的空心字,這個(gè)值通常為 3.0。
同時(shí)設(shè)置了空心的兩個(gè)屬性,并且 NSStrokeWidthAttributeName 屬性設(shè)置為整數(shù),文字前景色就無(wú)效果了
NSStrokeColorAttributeName 填充部分顏色,不是字體顏色,取值為 UIColor 對(duì)象
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];
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];
NSShadowAttributeName(陰影)
該屬性所對(duì)應(yīng)的值是一個(gè) NSShadow 對(duì)象。默認(rèn)為 nil。
單獨(dú)設(shè)置不好使,和這三個(gè)任一個(gè)都好使,
NSVerticalGlyphFormAttributeName,NSObliquenessAttributeName,NSExpansionAttributeName
NSVerticalGlyphFormAttributeName(橫豎排版)
該屬性所對(duì)應(yīng)的值是一個(gè) NSNumber 對(duì)象(整數(shù))。0 表示橫排文本。1 表示豎排文本。在 iOS 中,總是使用橫排文本,0 以外的值都未定義。
NSObliquenessAttributeName(字體傾斜)
NSExpansionAttributeName (文本扁平化)
- 后續(xù)還會(huì)繼續(xù)補(bǔ)充更新,大家也可以在評(píng)論中提出來(lái),我及時(shí)更新!