先寫一寫AttributeString的基本屬性缆蝉,熟悉了他們才能更好的使用
NSFontAttributeName 設(shè)置字體屬性嗡官,默認(rèn)值:字體:Helvetica(Neue) 字號:12
NSForegroundColorAttributeName 設(shè)置字體顏色利虫,取值為 UIColor對象捏卓,默認(rèn)值為黑色
NSBackgroundColorAttributeName 設(shè)置字體所在區(qū)域背景顏色,取值為 UIColor對象卧晓,默認(rèn)值為nil, 透明色
NSLigatureAttributeName 設(shè)置連體屬性虱黄,取值為NSNumber 對象(整數(shù))悦即,0 表示沒有連體字符,1 表示使用默認(rèn)的連體字符
NSKernAttributeName 設(shè)定字符間距,取值為 NSNumber 對象(整數(shù))盐欺,正值間距加寬赁豆,負(fù)值間距變窄
NSStrikethroughStyleAttributeName 設(shè)置刪除線,取值為 NSNumber 對象(整數(shù))
NSStrikethroughColorAttributeName 設(shè)置刪除線顏色冗美,取值為 UIColor 對象魔种,默認(rèn)值為黑色
NSUnderlineStyleAttributeName 設(shè)置下劃線,取值為 NSNumber 對象(整數(shù))粉洼,枚舉常量
NSUnderlineStyle中的值节预,與刪除線類似
NSUnderlineColorAttributeName 設(shè)置下劃線顏色,取值為 UIColor 對象属韧,默認(rèn)值為黑色
NSStrokeWidthAttributeName 設(shè)置邊線寬度安拟,取值為 NSNumber 對象(整數(shù)),負(fù)值填充效果宵喂,正值中空效果
NSStrokeColorAttributeName 設(shè)置邊線顏色糠赦,取值為 UIColor 對象
NSShadowAttributeName 設(shè)置陰影屬性,取值為 NSShadow 對象
NSTextEffectAttributeName 設(shè)置文本特殊效果锅棕,取值為 NSString 對象拙泽,目前只有圖版印刷效果可用
NSBaselineOffsetAttributeName 設(shè)置基線偏移值,取值為 NSNumber (float),正值上偏裸燎,負(fù)值下偏
NSObliquenessAttributeName 設(shè)置字形傾斜度顾瞻,取值為 NSNumber (float),正值右傾,負(fù)值左傾
NSExpansionAttributeName 設(shè)置文本橫向拉伸屬性德绿,取值為 NSNumber (float),正值橫向拉伸文本荷荤,負(fù)值橫向壓縮文本
NSWritingDirectionAttributeName 設(shè)置文字書寫方向,從左向右書寫或者從右向左書寫
NSVerticalGlyphFormAttributeName 設(shè)置文字排版方向移稳,取值為 NSNumber 對象(整數(shù))蕴纳,0 表示橫排文本,1 表示豎排文本
NSLinkAttributeName 設(shè)置鏈接屬性秒裕,點(diǎn)擊后調(diào)用瀏覽器打開指定URL地址
NSAttachmentAttributeName 設(shè)置文本附件,取值為NSTextAttachment對象,常用于文字圖片混排
NSParagraphStyleAttributeName 設(shè)置文本段落排版格式袱蚓,取值為 NSParagraphStyle 對象
NSKernAttributeName 設(shè)置文字間距
場景1
要實(shí)現(xiàn)效果1
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
[attributedString addAttributes:@{
NSForegroundColorAttributeName : [UIColor getColor:@"#ff1d1d"]
}range:NSMakeRange(8, 5)];
label.attributedText = attributedString;
場景2
要實(shí)現(xiàn)效果2
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
[attributedString addAttributes:@{
NSStrikethroughStyleAttributeName : @1
}range:NSMakeRange(0, text.length)];
label.attributedText = attributedString;
場景3
要實(shí)現(xiàn)效果3
這種方式的實(shí)現(xiàn)有兩種方法,第一種是加 NSUnderlineStyleAttributeName 下劃線并且更改文字和下劃線的顏色几蜻,還有一種方法就是直接用 NSLinkAttributeName 這種超鏈接的方式,只需要改一下顏色就可以体斩,不同場景做不同的選擇吧梭稚。
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
[attributedString addAttributes:@{
NSLinkAttributeName : @"0123456789",
NSForegroundColorAttributeName : [UIColor getColor:@"#2c92da"]
}range:NSMakeRange(text.length -11, 11)];
label.attributedText = attributedString;
場景4
要實(shí)現(xiàn)效果4
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
[attributedString addAttributes:@{
NSFontAttributeName : Font_20,
NSForegroundColorAttributeName : [UIColor getColor:@"#ff1d1d"]
}range:NSMakeRange(8, 5)];
label.attributedText = attributedString;