與NSString類似蛇更,在iOS中AttributedString也分為NSAttributedString和
NSMutableAttributedString谁鳍,不同的是针炉,AttributedString對(duì)象多了一個(gè)Attribute的概念,一個(gè)
AttributedString的對(duì)象包含很多的屬性女阀,每一個(gè)屬性都有其對(duì)應(yīng)的字符區(qū)域契吉,在這里是使用NSRange來進(jìn)行描述的跳仿。
使用AttributedString的方式通常有兩種:
方式一:
首先初始化一個(gè)NSMutableAttributedString,然后向里面添加文字樣式捐晶,最后將它賦給控件的AttributedText菲语,該方法適合于文本較少而又需要分段精細(xì)控制的情況。
NSString *originStr = @"Hello,中秋節(jié)惑灵!";
//方式一
//創(chuàng)建 NSMutableAttributedString
NSMutableAttributedString *attributedStr01 = [[NSMutableAttributedString alloc] initWithString: originStr];
//添加屬性
//給所有字符設(shè)置字體為Zapfino山上,字體高度為15像素
[attributedStr01 addAttribute: NSFontAttributeName value: [UIFont fontWithName: @"Zapfino" size: 15]
range: NSMakeRange(0, originStr.length)];
//分段控制,最開始4個(gè)字符顏色設(shè)置成藍(lán)色
[attributedStr01 addAttribute: NSForegroundColorAttributeName value: [UIColor blueColor] range: NSMakeRange(0, 4)];
//分段控制英支,第5個(gè)字符開始的3個(gè)字符佩憾,即第5、6干花、7字符設(shè)置為紅色
[attributedStr01 addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(4, 3)];
//賦值給顯示控件label01的 attributedText
_label01.attributedText = attributedStr01;
方式二:
首先創(chuàng)建屬性字典妄帘,初始化各種屬性,然后和需要控制的文本一起創(chuàng)建并賦值給控件的AttributedText池凄,該方法適合于需要控制的文本較多整體控制的情況抡驼,通常是從文件中讀取的大段文本控制。
//方式二
//創(chuàng)建屬性字典
NSDictionary *attrDict = @{ NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 15],
NSForegroundColorAttributeName: [UIColor blueColor] };
//創(chuàng)建 NSAttributedString 并賦值
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict];
通過對(duì)比兩個(gè)例子可以看出肿仑,方式一比較容易處理復(fù)雜的格式致盟,但是屬性設(shè)置比較繁多復(fù)雜,而方式二的屬性設(shè)置比較簡單明了尤慰,卻不善于處理復(fù)雜多樣的格式控制勾邦,但是不善于并不等于不能,可以通過屬性字符串分段的方式來達(dá)到方式一的效果割择,如下:
//方式二的分段處理
//第一段
NSDictionary *attrDict1 = @{ NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 15],
NSForegroundColorAttributeName: [UIColor blueColor] };
NSAttributedString *attrStr1 = [[NSAttributedString alloc]
initWithString: [originStr substringWithRange: NSMakeRange(0, 4)]
attributes: attrDict1];
//第二段
NSDictionary *attrDict2 = @{ NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 15],
NSForegroundColorAttributeName: [UIColor redColor] };
NSAttributedString *attrStr2 = [[NSAttributedString alloc]
initWithString: [originStr substringWithRange: NSMakeRange(4, 3)]
attributes: attrDict2];
//第三段
NSDictionary *attrDict3 = @{ NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 15],
NSForegroundColorAttributeName: [UIColor blackColor] };
NSAttributedString *attrStr3 = [[NSAttributedString alloc] initWithString: [originStr substringWithRange:
NSMakeRange(7, originStr.length - 4 - 3)] attributes:
attrDict3];
//合并
NSMutableAttributedString *attributedStr03 = [[NSMutableAttributedString alloc] initWithAttributedString: attrStr1];
[attributedStr03 appendAttributedString: attrStr2];
[attributedStr03 appendAttributedString: attrStr3];
_label03.attributedText = attributedStr03;
好了,講完AttributedString的創(chuàng)建方式萎河,下面研究下AttributedString究竟可以設(shè)置哪些屬性荔泳,具體來說蕉饼,有以下21個(gè):
// NSFontAttributeName ? ? ? ? ? ? ? ?設(shè)置字體屬性,默認(rèn)值:字體:Helvetica(Neue) 字號(hào):12
// NSForegroundColorAttributeNam ? ? ?設(shè)置字體顏色玛歌,取值為 UIColor對(duì)象昧港,默認(rèn)值為黑色
// NSBackgroundColorAttributeName ? ? 設(shè)置字體所在區(qū)域背景顏色,取值為 UIColor對(duì)象支子,默認(rèn)值為nil, 透明色
// NSLigatureAttributeName ? ? ? ? ? ?設(shè)置連體屬性创肥,取值為NSNumber 對(duì)象(整數(shù)),0 表示沒有連體字符值朋,1 表示使用默認(rèn)的連體字符
// NSKernAttributeName ? ? ? ? ? ? ? ?設(shè)定字符間距叹侄,取值為 NSNumber 對(duì)象(整數(shù)),正值間距加寬昨登,負(fù)值間距變窄
// NSStrikethroughStyleAttributeName ?設(shè)置刪除線趾代,取值為 NSNumber 對(duì)象(整數(shù))
// NSStrikethroughColorAttributeName ?設(shè)置刪除線顏色,取值為 UIColor 對(duì)象丰辣,默認(rèn)值為黑色
// NSUnderlineStyleAttributeName ? ? ?設(shè)置下劃線撒强,取值為 NSNumber 對(duì)象(整數(shù)),枚舉常量 NSUnderlineStyle中的值笙什,與刪除線類似
// NSUnderlineColorAttributeName ? ? ?設(shè)置下劃線顏色飘哨,取值為 UIColor 對(duì)象,默認(rèn)值為黑色
// NSStrokeWidthAttributeName ? ? ? ? 設(shè)置筆畫寬度琐凭,取值為 NSNumber 對(duì)象(整數(shù))芽隆,負(fù)值填充效果,正值中空效果
// NSStrokeColorAttributeName ? ? ? ? 填充部分顏色淘正,不是字體顏色摆马,取值為 UIColor 對(duì)象
// NSShadowAttributeName ? ? ? ? ? ? ?設(shè)置陰影屬性,取值為 NSShadow 對(duì)象
// NSTextEffectAttributeName ? ? ? ? ?設(shè)置文本特殊效果鸿吆,取值為 NSString 對(duì)象囤采,目前只有圖版印刷效果可用:
// NSBaselineOffsetAttributeName ? ? ?設(shè)置基線偏移值,取值為 NSNumber (float),正值上偏惩淳,負(fù)值下偏
// NSObliquenessAttributeName ? ? ? ? 設(shè)置字形傾斜度蕉毯,取值為 NSNumber (float),正值右傾,負(fù)值左傾
// NSExpansionAttributeName ? ? ? ? ? 設(shè)置文本橫向拉伸屬性思犁,取值為 NSNumber (float),正值橫向拉伸文本代虾,負(fù)值橫向壓縮文本
// NSWritingDirectionAttributeName ? ?設(shè)置文字書寫方向,從左向右書寫或者從右向左書寫
// NSVerticalGlyphFormAttributeName ? 設(shè)置文字排版方向激蹲,取值為 NSNumber 對(duì)象(整數(shù))棉磨,0 表示橫排文本,1 表示豎排文本
// NSLinkAttributeName ? ? ? ? ? ? ? ?設(shè)置鏈接屬性学辱,點(diǎn)擊后調(diào)用瀏覽器打開指定URL地址
// NSAttachmentAttributeName ? ? ? ? ?設(shè)置文本附件,取值為NSTextAttachment對(duì)象,常用于文字圖片混排
// NSParagraphStyleAttributeName ? ? ?設(shè)置文本段落排版格式乘瓤,取值為 NSParagraphStyle 對(duì)象
下面就一一舉例說明:
1. NSFontAttributeName
//NSForegroundColorAttributeName 設(shè)置字體顏色环形,取值為 UIColor,默認(rèn)為黑色
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è)置的顏色在地位上是相等的衙傀,誰最后賦值抬吟,最終顯示的就是誰的顏色。
2. NSBackgroundColorAttributeName
//NSForegroundColorAttributeName 設(shè)置字體顏色统抬,取值為 UIColor火本,默認(rèn)為黑色
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 設(shè)置字體所在區(qū)域背景的顏色,取值為UIColor聪建,默認(rèn)值為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];
仔細(xì)觀察會(huì)發(fā)現(xiàn)個(gè)問題钙畔,我并沒有關(guān)閉 NSForegroundColorAttributeName
屬性,但是在運(yùn)行結(jié)果中妆偏,所有字體的顏色都變成了默認(rèn)色——黑色刃鳄,這說明 NSForegroundColorAttributeName 和
NSBackgroundColorAttributeName 的低位是相等的,跟前面介紹的 textColor
一樣钱骂,哪個(gè)屬性最后一次賦值叔锐,就會(huì)沖掉前面的效果,若是我們把屬性代碼順序交換一下
//NSBackgroundColorAttributeName 設(shè)置字體所在區(qū)域背景的顏色见秽,取值為UIColor愉烙,默認(rèn)值為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 設(shè)置字體顏色,取值為 UIColor解取,默認(rèn)為黑色
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 設(shè)置字體顏色步责,取值為 UIColor,默認(rèn)為黑色
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 設(shè)置字體所在區(qū)域背景的顏色禀苦,取值為UIColor蔓肯,默認(rèn)值為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 設(shè)置連體屬性,取值為NSNumber 對(duì)象(整數(shù))慧邮,0 表示沒有連體字符调限,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ì)象的方法有很多裆装,或者可以簡寫成 @(int)
注意觀察字母f和l之間的變化。
感覺連寫就是一個(gè)藝術(shù)字功能,當(dāng)字符f和l組合使用組合符號(hào)(所謂的字形(glyph))繪制時(shí)哨免,看起來確實(shí)更加美觀勾扭。但是并非所有的字符之間都有組合符號(hào),事實(shí)上铁瞒,只有某些字體中得某些字符的組合(如字符f和l,字符f和i等)才具有美觀的組合符號(hào)桅滋。
4. 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];
5. NSStrikethroughStyleAttributeName
//NSStrikethroughStyleAttributeName 設(shè)置刪除線芍碧,取值為 NSNumber 對(duì)象(整數(shù)),枚舉常量 NSUnderlineStyle中的值
// NSUnderlineStyleNone ? 不設(shè)置刪除線
// NSUnderlineStyleSingle 設(shè)置刪除線為細(xì)單實(shí)線
// NSUnderlineStyleThick ?設(shè)置刪除線為粗單實(shí)線
// NSUnderlineStyleDouble 設(shè)置刪除線為細(xì)雙實(shí)線
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 才能使用
刪除線和下劃線使用相同的枚舉常量作為其屬性值
目前iOS中只有上面列出的4中效果,雖然我們能夠在頭文件中發(fā)現(xiàn)其他更多的取值吏饿,但是使用后沒有任何效果
可以看出踪危,中文和英文刪除線的位置有所不同
另外,刪除線屬性取值除了上面的4種外猪落,其實(shí)還可以取其他整數(shù)值贞远,有興趣的可以自行試驗(yàn),取值為 0 - 7時(shí)笨忌,效果為單實(shí)線蓝仲,隨著值得增加,單實(shí)線逐漸變粗官疲,取值為 9 - 15時(shí)袱结,效果為雙實(shí)線,取值越大途凫,雙實(shí)線越粗垢夹。
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 設(shè)置刪除線顏色,取值為 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];
7. NSUnderlineStyleAttributeName
下劃線除了線條位置和刪除線不同外棚饵,其他的都可以完全參照刪除線設(shè)置。
//NSUnderlineStyleAttributeName 設(shè)置下劃線掩完,取值為 NSNumber 對(duì)象(整數(shù))噪漾,枚舉常量 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
可以完全參照下劃線顏色設(shè)置
//NSUnderlineColorAttributeName 設(shè)置下劃線顏色且蓬,取值為 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];
9. NSStrokeWidthAttributeName
//NSStrokeWidthAttributeName 設(shè)置筆畫寬度,取值為 NSNumber 對(duì)象(整數(shù))恶阴,負(fù)值填充效果诈胜,正值中空效果
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 對(duì)象
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];