初始化:
NSMutableAttributedString*attStr = [[NSMutableAttributedStringalloc] initWithString:@"富文本"];
?其他幾種創(chuàng)建方法
- (instancetype)initWithString:(NSString *)str;
- (instancetype)initWithString:(NSString *)str attributes:(nullable NSDictionary *)attrs;
- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;
添加富文本:
- (void)addAttribute:(NSAttributedStringKey)name value:(id)value range:(NSRange)range;
- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;
刪除富文本
- (void)removeAttribute:(NSAttributedStringKey)name range:(NSRange)range;
富文本樣式大全:
字體大小 ?NSFontAttributeName
[@“富文本” addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:10.0f] range:NSMakeRange(0,2)];
字體顏色?NSForegroundColorAttributeName
背景顏色NSBackgroundColorAttributeName
空心字和描邊顏色
[@“富文本” addAttribute:NSStrokeColorAttributeNamevalue:[UIColorredColor] range:NSMakeRange(4,3)];
[@“富文本”addAttribute:NSStrokeWidthAttributeNamevalue:@1.5range:NSMakeRange(4,3)];
字體間距NSKernAttributeName
斜體NSObliquenessAttributeName
拉伸NSExpansionAttributeName
兩個字連在一起NSLigatureAttributeName
字體下沉或者上浮NSBaselineOffsetAttributeName
字體陰影
NSShadow*shadow = [[NSShadowalloc] init];
shadow.shadowOffset =CGSizeMake(3,3);
shadow.shadowBlurRadius =1.5;
shadow.shadowColor = [UIColorredColor];
[@“富文本” addAttribute:NSShadowAttributeNamevalue:shadow range:NSMakeRange(0,2)];
文字方向[@“富文本”? addAttribute:NSWritingDirectionAttributeNamevalue:@[@(NSWritingDirectionRightToLeft|NSWritingDirectionOverride)] range:NSMakeRange(0, attrStr.length)];
橫豎排版NSVerticalGlyphFormAttributeName
下劃線NSUnderlineStyleAttributeName
中劃線NSStrikethroughStyleAttributeName
網(wǎng)址鏈接NSLinkAttributeName
圖文混排
NSMutableAttributedString* attrStr = [[NSMutableAttributedStringalloc] initWithString:@"這是一個富文本字符串!"];// 創(chuàng)建一個文字附件對象NSTextAttachment*textAttachment = [[NSTextAttachmentalloc] init];textAttachment.image = [UIImageimageNamed:@"11.png"];//設(shè)置圖片源textAttachment.bounds =CGRectMake(0,-6,30,30);//設(shè)置圖片位置和大小// 將文字附件轉(zhuǎn)換成屬性字符串NSAttributedString*attachmentAttrStr = [NSAttributedStringattributedStringWithAttachment:textAttachment];// 將轉(zhuǎn)換成屬性字符串插入到目標(biāo)字符串[attrStr insertAttributedString:attachmentAttrStr atIndex:8];
段落
NSMutableParagraphStyle*paragraphStyle = [[NSMutableParagraphStylealloc] init];// 行間距paragraphStyle.lineSpacing =15.f;// 段落間距paragraphStyle.paragraphSpacing =30.f;// 段落縮進(jìn)像素paragraphStyle.firstLineHeadIndent =40.f;// 整體縮進(jìn)像素paragraphStyle.headIndent =15.f;// 對齊方式paragraphStyle.alignment =NSTextAlignmentLeft;
NSMutableAttributedString* attrStr = [[NSMutableAttributedStringalloc] initWithString:@"這是一個富文本字符串。\n這是一個富文本字符串?\n這是一個富文本字符串!"];[attrStr addAttribute:NSParagraphStyleAttributeNamevalue:paragraphStyle range:NSMakeRange(0, attrStr.length)];
//?NSFontAttributeName????????????????設(shè)置字體屬性,默認(rèn)值:字體:Helvetica(Neue)?字號:12??
//?NSForegroundColorAttributeNam??????設(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?????????填充部分顏色,不是字體顏色,取值為?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?對象??