NSMutableParagraphStyle富文本

在iOS開發(fā)中,常常會有一段文字顯示不同的顏色和字體,或者給某幾個文字加刪除線或下劃線的需求。就需要富文本來實(shí)現(xiàn)。

一、實(shí)例化方法和使用方法

    NSMutableAttributedString *detailStr = [[NSMutableAttributedString alloc] initWithString:detailString];
NSRange stringRange = NSMakeRange(0, 5);
    [detailStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#999999"] range:stringRange];// 添加單個屬性
    [detailStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSParagraphStyleAttributeName:paragraphStyle} range:NSMakeRange(0, detailString.length)];// 添加多個屬性
    [detailLabel setAttributedText:detailStr];

二、富文本屬性

屬性 作用 類型
NSFontAttributeName 字號 UIFont 默認(rèn)12
NSParagraphStyleAttributeName 段落樣式 NSParagraphStyle
NSForegroundColorAttributeName 前景色 UIColor
NSBackgroundColorAttributeName 背景色 UIColor
NSLigatureAttributeName 連筆字 1或0
NSKernAttributeName 字間距 CGFloat
NSStrikethroughStyleAttributeName 刪除線 1或0
NSUnderlineStyleAttributeName 下劃線 1或0
NSStrokeColorAttributeName same as foreground color UIColor
NSStrokeWidthAttributeName 字體描邊 CGFloat
NSShadowAttributeName 陰影 NSShawdow
NSTextEffectAttributeName 設(shè)置文本特殊效果,目前只有圖版印刷效果可用 NSString
NSAttachmentAttributeName 設(shè)置文本附件,常用插入圖片 NSTextAttachment
NSLinkAttributeName 鏈接 NSURL (preferred) or NSString
NSBaselineOffsetAttributeName 基準(zhǔn)線偏移 NSNumber
NSUnderlineColorAttributeName 下劃線顏色 UIColor
NSStrikethroughColorAttributeName 刪除線顏色 UIColor
NSObliquenessAttributeName 字體傾斜 NSNumber
NSExpansionAttributeName 字體加粗 NSNumber 比例 0就是不變 1增加一倍
NSWritingDirectionAttributeName 文字方向 分別代表不同的文字出現(xiàn)方向等等 @[@(1),@(2)]
NSVerticalGlyphFormAttributeName 水平或者豎直文本 在iOS,不支持豎版 1豎直 0水平

三、段落樣式

段落樣式主要改行距、段距、首行縮進(jìn)、最大最小行高、多倍行距等十幾個屬性

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 5;
    paragraphStyle.alignment = NSTextAlignmentLeft;
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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