富文本

與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];

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末焦匈,一起剝皮案震驚了整個(gè)濱河市血公,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌缓熟,老刑警劉巖累魔,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異够滑,居然都是意外死亡垦写,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門彰触,熙熙樓的掌柜王于貴愁眉苦臉地迎上來梯投,“玉大人,你說我怎么就攤上這事况毅》直停” “怎么了?”我有些...
    開封第一講書人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵俭茧,是天一觀的道長咆疗。 經(jīng)常有香客問我,道長母债,這世上最難降的妖魔是什么午磁? 我笑而不...
    開封第一講書人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮毡们,結(jié)果婚禮上迅皇,老公的妹妹穿的比我還像新娘。我一直安慰自己衙熔,他們只是感情好登颓,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著红氯,像睡著了一般框咙。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上痢甘,一...
    開封第一講書人閱讀 51,125評(píng)論 1 297
  • 那天喇嘱,我揣著相機(jī)與錄音,去河邊找鬼塞栅。 笑死者铜,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播作烟,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼愉粤,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了拿撩?” 一聲冷哼從身側(cè)響起衣厘,我...
    開封第一講書人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎压恒,沒想到半個(gè)月后头滔,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡涎显,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了兴猩。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片期吓。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖倾芝,靈堂內(nèi)的尸體忽然破棺而出讨勤,到底是詐尸還是另有隱情,我是刑警寧澤晨另,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布潭千,位于F島的核電站,受9級(jí)特大地震影響借尿,放射性物質(zhì)發(fā)生泄漏刨晴。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一路翻、第九天 我趴在偏房一處隱蔽的房頂上張望狈癞。 院中可真熱鬧,春花似錦茂契、人聲如沸蝶桶。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽真竖。三九已至,卻和暖如春厌小,著一層夾襖步出監(jiān)牢的瞬間恢共,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來泰國打工召锈, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留旁振,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像拐袜,于是被迫代替她去往敵國和親吉嚣。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容