利用 UILabel 的 attributedText 屬性可設(shè)置不同的文本樣式
- 首先初始化一個(gè)NSAttributedString
NSMutableAttributedString *abStr = [[NSMutableAttributedString alloc] initWithString:@"你看我是富文本"];
這里初始加給label.attributedText,看起來是平平無奇的樣子
這里要先設(shè)置label的初始textColor和font等,展示基本樣式
- 然后選擇一種樣式給label變身吧
1. 設(shè)置字體和大小
NSFontAttributeName
//設(shè)置前三個(gè)字 [UIFont systemFontOfSize:15]
[abStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, 3)];
2. 設(shè)置間距
NSParagraphStyleAttributeName
// 這里要用NSMutableParagraphStyle來設(shè)置各種屬性
// 下面只是簡單常用的處理行間距
// 還可以設(shè)置首行縮進(jìn),行高漫谷,對齊方式旷坦,段落赎瑰,連字屬性等
NSMutableParagraphStyle *pgpStyle = [[NSMutableParagraphStyle alloc] init];
pgpStyle.lineSpacing = 30; //行間距,注意:實(shí)際視覺行距要計(jì)算到實(shí)際行高
// 行間距是多少倍
// pgpStyle.lineHeightMultiple = 1.5f;
// 首行縮進(jìn)
// pgpStyle.firstLineHeadIndent = 30.0f;
// 對齊方式
// pgpStyle.alignment = NSTextAlignmentLeft;
// 內(nèi)容超出寬度時(shí),內(nèi)容有...省略的位置 ( "...abc" ,"abc..." ,"ab...c")
// pgpStyle.lineBreakMode = NSLineBreakByTruncatingTail;
// 整體縮進(jìn)
// pgpStyle.headIndent = 30;//左邊距
// pgpStyle.tailIndent = 20;//右邊距
// 行高
// 針對不同的字型與字號,可以透過指定最大與最小行距來避免過高或過窄的狀況發(fā)生
// pgpStyle.minimumLineHeight = 10;//最低行高
// pgpStyle.maximumLineHeight = 20;//最大行高
// 段落
// pgpStyle.paragraphSpacing = 15; //段落間距
// pgpStyle.paragraphSpacingBefore = 30;//段首行空白空間
// pgpStyle.paragraphSpacing = 30; //段落后面的間距
[abStr addAttribute:NSParagraphStyleAttributeName
value:pgpStyle
range:NSMakeRange(0, 5)];
3. 設(shè)置文本顏色
NSForegroundColorAttributeName
[abStr addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 3)];
4. 設(shè)置文字背景色
NSBackgroundColorAttributeName
[abStr addAttribute:NSBackgroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 3)];
5. 設(shè)置連字
NSLigatureAttributeName
說明:
1.PingFangSC_Bold 和 PingFangSC_Regular 一般不會有連字問題
2.PingFangSC-Semibold等字體或者三方字體就有默認(rèn)連字現(xiàn)象
3.也不是所有的字都會連,一般出現(xiàn)在fi和fl這種字符才會連,具體"連字"的概念問百度or谷歌
NSMutableAttributedString *abStr = [[NSMutableAttributedString alloc]initWithString:@"fiflfi"];
//0表示不連 1表示連 實(shí)際中PingFangSC-Semibold等是默認(rèn)連的
[abStr addAttribute:NSLigatureAttributeName value:@(1) range:NSMakeRange(0, abStr.length)];
6. 設(shè)置文字間距
NSKernAttributeName
[abStr addAttribute:NSKernAttributeName value:@(10) range:NSMakeRange(0, 3)];
7. 設(shè)置刪除線
NSStrikethroughColorAttributeName
NSStrikethroughStyleAttributeName
說明:
1.可設(shè)置單.雙刪除線
2.value賦值 [1, 7]是單線, [9, 15]是雙線,區(qū)間內(nèi)數(shù)值越大,線越粗,注意超出區(qū)間內(nèi)的值,刪除線就會失效
[abStr addAttribute:NSStrikethroughStyleAttributeName value:@(5) range:NSMakeRange(0, 3)];
[abStr addAttribute:NSStrikethroughStyleAttributeName value:@(13) range:NSMakeRange(3, 4)];
[abStr addAttribute:NSStrikethroughColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 3)];
[abStr addAttribute:NSStrikethroughColorAttributeName value:[UIColor greenColor] range:NSMakeRange(3, 4)];
8. 設(shè)置下劃線
NSUnderlineStyleAttributeName
NSUnderlineColorAttributeName
一般常用:value賦值
NSUnderlineStyleSingle //單根細(xì)線
NSUnderlineStyleThick //單根粗線
NSUnderlineStyleDouble //雙根細(xì)線
//樣式
[abStr addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, 3)];
//顏色
[abStr addAttribute:NSUnderlineColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 3)];
[abStr addAttribute:NSUnderlineColorAttributeName value:[UIColor greenColor] range:NSMakeRange(4, 3)];
9. 文字描邊
NSStrokeWidthAttributeName
NSStrokeColorAttributeName
NSStrokeWidthAttributeName 這個(gè)設(shè)置描邊字符的寬度,(-∞, 0)區(qū)間向內(nèi),(0, +∞)區(qū)間向外, 注意value設(shè)置過大就糊了喲
NSStrokeColorAttributeName 這個(gè)設(shè)置描邊字符的顏色
[abStr addAttribute:NSStrokeWidthAttributeName value:@(2) range:NSMakeRange(0, 3)];
[abStr addAttribute:NSStrokeColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 3)];
[abStr addAttribute:NSStrokeWidthAttributeName value:@(-2) range:NSMakeRange(3, 3)];
[abStr addAttribute:NSStrokeColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(3, 3)];
10. 設(shè)置文字陰影
NSShadowAttributeName
NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowOffset = CGSizeMake(5, 5);
shadow.shadowColor = [UIColor orangeColor];
// shadow.shadowBlurRadius = 5; 陰影模糊度
//貌似range設(shè)置無效了,都是這個(gè)文本
[abStr addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(0, abStr.length)];
11. 設(shè)置文本特殊效果(用處少)
NSTextEffectAttributeName
只有一直效果類型為NSTextEffectLetterpressStyle //凸版樣式
12. 設(shè)置文件附件(圖文混排)
NSAttachmentAttributeName
NSTextAttachment *tathment = [[NSTextAttachment alloc]init];
tathment.image = [UIImage imageNamed:@"absImg.png"];
tathment.bounds = CGRectMake(0, 0, 35, 35);
NSAttributedString *imgAbs = [NSAttributedString attributedStringWithAttachment:tathment];
[abStr insertAttributedString:imgAbs atIndex:3];
13. 設(shè)置鏈接(只有UITextView可用)
NSLinkAttributeName
value 賦值類型是 NSURL or NSString
UITextView *tv = [[UITextView alloc]initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 200)];
tv.font = [UIFont systemFontOfSize:25];
tv.textAlignment = NSTextAlignmentCenter;
tv.editable = NO;
[self.view addSubview:tv];
NSMutableAttributedString *abStr = [[NSMutableAttributedString alloc]initWithString:@"你看我是富文本"];
[abStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:25] range:NSMakeRange(0, abStr.length)];
NSMutableParagraphStyle *pgpStyle = [[NSMutableParagraphStyle alloc] init];
pgpStyle.alignment = NSTextAlignmentCenter;
[abStr addAttribute:NSParagraphStyleAttributeName value:pgpStyle range:NSMakeRange(0, abStr.length)];
NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
[abStr addAttribute:NSLinkAttributeName value:url range:NSMakeRange(abStr.length-3, 3)];
tv.attributedText = abStr;
14. 文字上下偏移
NSBaselineOffsetAttributeName
vlaue 賦值 正負(fù)對應(yīng)上下偏移
[abStr addAttribute:NSBaselineOffsetAttributeName value:@(6) range:NSMakeRange(2, 1)];
[abStr addAttribute:NSBaselineOffsetAttributeName value:@(-6) range:NSMakeRange(4, 1)];
15. 文字斜體
NSObliquenessAttributeName
vlaue 賦值 正負(fù)對應(yīng)左右斜
[abStr addAttribute:NSObliquenessAttributeName value:@(-0.5) range:NSMakeRange(0, 3)];
[abStr addAttribute:NSObliquenessAttributeName value:@(0.5) range:NSMakeRange(4, 3)];
15. 文字拉伸
NSExpansionAttributeName // 橫向拉伸
vlaue 賦值 正負(fù)對應(yīng)變胖瘦
[abStr addAttribute:NSExpansionAttributeName value:@(-0.5) range:NSMakeRange(0, 3)];
[abStr addAttribute:NSExpansionAttributeName value:@(0.5) range:NSMakeRange(4, 3)];
16. 文字書寫方向(一般用于自右向左)
NSWritingDirectionAttributeName
[abStr addAttribute:NSWritingDirectionAttributeName value:@[@(NSWritingDirectionRightToLeft|NSWritingDirectionOverride)] range:NSMakeRange(0, abStr.length)];
17. 文字排版(iOS上無效果)
NSVerticalGlyphFormAttributeName
0為水平排版的字,1為垂直排版的字显晶。iOS上效果一直為1