富文本,顧名思義就是豐富的文本格式,本文demo使用NSMutableAttributedString
//獲取富文本 NSMutableAttributedString*attributeString_atts=[[NSMutableAttributedString alloc]initWithString:string]; //背景色 UIColor *backgroundColor=[UIColor whiteColor]; //字體 UIFont *font=[UIFont fontWithName:@"STHeitiSC-Light" size:18.0]; //偏移量 NSNumber *baseLineOffset=[NSNumber numberWithFloat:10.0]; //文本顏色 UIColor *foregroundColor=[UIColor blackColor]; //字間距 NSNumber *kern=[NSNumber numberWithFloat:1.5]; //連體字 NSNumber *ligature=[NSNumber numberWithFloat:0]; //超鏈接文本 NSURL *linkURL=[NSURL URLWithString:@"www.baidu.com"]; //下划線格式 NSNumber *underLine=[NSNumber numberWithInt:NSUnderlineStyleSingle]; //段落格式 段落中有更多段落的格式 NSMutableParagraphStyle *paragraphStyle=[[NSMutableParagraphStyle alloc]init];//新起行縮進 // [paragraphStyle setHeadIndent:10.0]; //首行縮進 [paragraphStyle setFirstLineHeadIndent:21.0];
//行高倍數因子
[paragraphStyle setLineHeightMultiple:1.3];
//行間距 [paragraphStyle setLineSpacing:10.0];
//上述兩屬性說明:行間距是每行之間距離,計算高度時,可能出現偏差;行高倍數因子,行高固定。
//斷行 默認按單詞 // paragraphStyle.lineBreakMode=NSLineBreakByWordWrapping;
// // [paragraphStyle setTailIndent:375.0]; NSDictionary *attrsDic=@{ NSForegroundColorAttributeName:foregroundColor, NSBackgroundColorAttributeName:backgroundColor, // NSBaselineOffsetAttributeName:baseLineOffset, NSFontAttributeName:font, NSKernAttributeName:kern, NSLigatureAttributeName:ligature, // NSLinkAttributeName:linkURL, // NSUnderlineStyleAttributeName:underLine, NSParagraphStyleAttributeName:paragraphStyle }; //全文使用 [attributeString_atts addAttributes:attrsDic range:NSMakeRange(0, [string length])]; //計算富文本的寬度 CGRect attrsRect=[attributeString_atts boundingRectWithSize:CGSizeMake(self.view.frame.size.width-20, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil]; NSLog(@"%f",attrsRect.size.width); NSLog(@"%f",attrsRect.size.height); UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(10, 200*number, self.view.frame.size.width-20, attrsRect.size.height)]; label.attributedText=attributeString_atts; label.userInteractionEnabled=YES; label.numberOfLines=0; [self.view addSubview:label];
AttributedString的創建方式,下面研究下AttributedString究竟可以設置哪些屬性,具體來說,有以下21個:
// NSFontAttributeName 設置字體屬性,默認值:字體:Helvetica(Neue) 字號:12
// NSForegroundColorAttributeNam 設置字體顏色,取值為 UIColor對象,默認值為黑色
// NSBackgroundColorAttributeName 設置字體所在區域背景顏色,取值為 UIColor對象,默認值為nil, 透明色
// NSLigatureAttributeName 設置連體屬性,取值為NSNumber 對象(整數),0 表示沒有連體字符,1 表示使用默認的連體字符
// NSKernAttributeName 設定字符間距,取值為 NSNumber 對象(整數),正值間距加寬,負值間距變窄
// NSStrikethroughStyleAttributeName 設置刪除線,取值為 NSNumber 對象(整數)
// NSStrikethroughColorAttributeName 設置刪除線顏色,取值為 UIColor 對象,默認值為黑色
// NSUnderlineStyleAttributeName 設置下划線,取值為 NSNumber 對象(整數),枚舉常量 NSUnderlineStyle中的值,與刪除線類似
// NSUnderlineColorAttributeName 設置下划線顏色,取值為 UIColor 對象,默認值為黑色
// NSStrokeWidthAttributeName 設置筆畫寬度,取值為 NSNumber 對象(整數),負值填充效果,正值中空效果
// NSStrokeColorAttributeName 填充部分顏色,不是字體顏色,取值為 UIColor 對象
// NSShadowAttributeName 設置陰影屬性,取值為 NSShadow 對象
// NSTextEffectAttributeName 設置文本特殊效果,取值為 NSString 對象,目前只有圖版印刷效果可用:
// NSBaselineOffsetAttributeName 設置基線偏移值,取值為 NSNumber (float),正值上偏,負值下偏
// NSObliquenessAttributeName 設置字形傾斜度,取值為 NSNumber (float),正值右傾,負值左傾
// NSExpansionAttributeName 設置文本橫向拉伸屬性,取值為 NSNumber (float),正值橫向拉伸文本,負值橫向壓縮文本
// NSWritingDirectionAttributeName 設置文字書寫方向,從左向右書寫或者從右向左書寫
// NSVerticalGlyphFormAttributeName 設置文字排版方向,取值為 NSNumber 對象(整數),0 表示橫排文本,1 表示豎排文本
// NSLinkAttributeName 設置鏈接屬性,點擊后調用打開指定URL地址
// NSAttachmentAttributeName 設置文本附件,取值為NSTextAttachment對象,常用於文字圖片混排
// NSParagraphStyleAttributeName 設置文本段落排版格式,取值為 NSParagraphStyle 對象
paragraph
alignment //對齊方式
firstLineHeadIndent //首行縮進
headIndent //縮進
tailIndent //尾部縮進
lineBreakMode //斷行方式
maximumLineHeight //最大行高
minimumLineHeight //最低行高
lineSpacing //行距
paragraphSpacing //段距
paragraphSpacingBefore //段首空間
baseWritingDirection //句子方向
lineHeightMultiple //可變行高,乘因數。
hyphenationFactor //連字符屬性