NSAttributedString
An NSAttributedString object manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string. An association of characters and their attributes is called an attributed string;
這句話就是對(duì)這個(gè)類的一個(gè)最簡(jiǎn)明扼要的概括纽竣。NSAttributedString管理一個(gè)字符串,以及與該字符串中的單個(gè)字符或某些范圍的字符串相關(guān)的屬性。它有一個(gè)子類NSMutableAttributedString。具體實(shí)現(xiàn)時(shí)坯癣,NSAttributedString維護(hù)了一個(gè)NSString蛙酪,用來(lái)保存最原始的字符串已亥,另有一個(gè)NSDictionary用來(lái)保存各個(gè)子串/字符的屬性炼团。
NSAttributedString 的21種屬性詳細(xì)介紹
* API: Character Attributes , NSAttributedString 共有21個(gè)屬性*
* 1. NSFontAttributeName ->設(shè)置字體屬性咪惠,默認(rèn)值:字體:Helvetica(Neue) 字號(hào):12
* 2. NSParagraphStyleAttributeName ->設(shè)置文本段落排版格式击吱,取值為 NSParagraphStyle 對(duì)象(詳情見(jiàn)下面的API說(shuō)明)
* 3. NSForegroundColorAttributeName ->設(shè)置字體顏色,取值為 UIColor對(duì)象遥昧,默認(rèn)值為黑色
* 4. NSBackgroundColorAttributeName ->設(shè)置字體所在區(qū)域背景顏色覆醇,取值為 UIColor對(duì)象,默認(rèn)值為nil, 透明色
* 5. NSLigatureAttributeName ->設(shè)置連體屬性炭臭,取值為NSNumber 對(duì)象(整數(shù))永脓,0 表示沒(méi)有連體字符,1 表示使用默認(rèn)的連體字符
* 6. NSKernAttributeName ->設(shè)置字符間距鞋仍,取值為 NSNumber 對(duì)象(整數(shù))常摧,正值間距加寬,負(fù)值間距變窄
* 7. NSStrikethroughStyleAttributeName ->設(shè)置刪除線威创,取值為 NSNumber 對(duì)象(整數(shù))
* 8. NSStrikethroughColorAttributeName ->設(shè)置刪除線顏色排宰,取值為 UIColor 對(duì)象,默認(rèn)值為黑色
* 9. NSUnderlineStyleAttributeName ->設(shè)置下劃線那婉,取值為 NSNumber 對(duì)象(整數(shù))板甘,枚舉常量 NSUnderlineStyle中的值,與刪除線類似
* 10. NSUnderlineColorAttributeName ->設(shè)置下劃線顏色详炬,取值為 UIColor 對(duì)象盐类,默認(rèn)值為黑色
* 11. NSStrokeWidthAttributeName ->設(shè)置筆畫(huà)寬度(粗細(xì)),取值為 NSNumber 對(duì)象(整數(shù))呛谜,負(fù)值填充效果在跳,正值中空效果
* 12. NSStrokeColorAttributeName ->填充部分顏色,不是字體顏色隐岛,取值為 UIColor 對(duì)象
* 13. NSShadowAttributeName ->設(shè)置陰影屬性猫妙,取值為 NSShadow 對(duì)象
* 14. NSTextEffectAttributeName ->設(shè)置文本特殊效果,取值為 NSString 對(duì)象聚凹,目前只有圖版印刷效果可用
* 15. NSBaselineOffsetAttributeName ->設(shè)置基線偏移值割坠,取值為 NSNumber (float),正值上偏,負(fù)值下偏
* 16. NSObliquenessAttributeName ->設(shè)置字形傾斜度妒牙,取值為 NSNumber (float),正值右傾彼哼,負(fù)值左傾
* 17. NSExpansionAttributeName ->設(shè)置文本橫向拉伸屬性,取值為 NSNumber (float),正值橫向拉伸文本湘今,負(fù)值橫向壓縮文本
* 18. NSWritingDirectionAttributeName ->設(shè)置文字書(shū)寫(xiě)方向敢朱,從左向右書(shū)寫(xiě)或者從右向左書(shū)寫(xiě)
* 19. NSVerticalGlyphFormAttributeName ->設(shè)置文字排版方向,取值為 NSNumber 對(duì)象(整數(shù)),0 表示橫排文本拴签,1 表示豎排文本
* 20. NSLinkAttributeName ->設(shè)置鏈接屬性孝常,點(diǎn)擊后調(diào)用瀏覽器打開(kāi)指定URL地址
* 21. NSAttachmentAttributeName ->設(shè)置文本附件,取值為NSTextAttachment對(duì)象,常用于文字圖片混排
NSParagraphStyleAttributeName 段落的風(fēng)格
NSParagraphStyleAttributeName 段落的風(fēng)格(設(shè)置首行,行間距蚓哩,對(duì)齊方式什么的)看自己需要什么屬性茫因,寫(xiě)什么
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 10;// 字體的行間距
paragraphStyle.firstLineHeadIndent = 20.0f;//首行縮進(jìn)
paragraphStyle.alignment = NSTextAlignmentJustified;//(兩端對(duì)齊的)文本對(duì)齊方式:(左,中,右驰贷,兩端對(duì)齊盛嘿,自然)
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;//結(jié)尾部分的內(nèi)容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz")
paragraphStyle.headIndent = 20;//整體縮進(jìn)(首行除外)
paragraphStyle.tailIndent = 20;//
paragraphStyle.minimumLineHeight = 10;//最低行高
paragraphStyle.maximumLineHeight = 20;//最大行高
paragraphStyle.paragraphSpacing = 15;//段與段之間的間距
paragraphStyle.paragraphSpacingBefore = 22.0f;//段首行空白空間/* Distance between the bottom of the previous paragraph (or the end of its paragraphSpacing, if any) and the top of this paragraph. */
paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;//從左到右的書(shū)寫(xiě)方向(一共??三種)
paragraphStyle.lineHeightMultiple = 15;/* Natural line height is multiplied by this factor (if positive) before being constrained by minimum and maximum line height. */
paragraphStyle.hyphenationFactor = 1;//連字屬性 在iOS,唯一支持的值分別為0和1
計(jì)算文本寬高等
有了NSAttributed的屬性后次兆,現(xiàn)在就可以很輕松的計(jì)算某一段落高度
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:1]; //行間距
NSDictionary *attributes = @{NSFontAttributeName: font,
NSParagraphStyleAttributeName: paragraphStyle};
NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin;
CGRect textRect = [self boundingRectWithSize:maxSize
options:options
attributes:attributes
context:nil];
CGSize size = textRect.size;
return CGSizeMake(ceil(size.width), ceil(size.height));
YYLabel的富文本
#pragma mark - 注意:對(duì)于YYLabel的textAlignment的設(shè)置必須在attributedText之后恃慧,否則無(wú)效痢士,即否則會(huì)變成默認(rèn)的NSTextAlignmentLeft
//self.yyLabel.textAlignment = NSTextAlignmentCenter; //錯(cuò)誤怠蹂,寫(xiě)在attributedText設(shè)置之前是無(wú)效的
self.yyLabel.attributedText = allAttributedString;
self.yyLabel.textAlignment = NSTextAlignmentCenter; //注意:對(duì)于YYLabel的textAlignment的設(shè)置必須在attributedText之后善延,否則無(wú)效,即否則會(huì)變成默認(rèn)的NSTextAlignmentLeft
self.yyLabel.textVerticalAlignment = YYTextVerticalAlignmentCenter;
self.yyLabel.numberOfLines = 0;