圖文混排之NSAttributedString(超級(jí)詳細(xì)的富文本大全)

前言

最近項(xiàng)目中對(duì)圖文混排有一定的需求,例如價(jià)格,文字鏈接,文字顏色變化等要求,翻了很多資料,咱們對(duì)這些屬性做了如下的總結(jié),希望能在方便自己查閱!

    NSFontAttributeName               設(shè)置字體大小和字體的類型 默認(rèn)12 Helvetica(Neue)
    NSForegroundColorAttributeName    設(shè)置字體顏色,默認(rèn)黑色 UIColor對(duì)象
    NSBackgroundColorAttributeName    設(shè)置字體所在區(qū)域的背景顏色,默認(rèn)為nil,透明色
    NSLigatureAttributeName           設(shè)置連體屬性,NSNumber對(duì)象 默認(rèn)0 沒(méi)有連體
    NSKernAttributeName               設(shè)置字符間距, NSNumber浮點(diǎn)型屬性 正數(shù)間距加大,負(fù)數(shù)間距縮小
    NSStrikethroughStyleAttributeName 設(shè)置刪除線,NSNumber對(duì)象
    NSStrikethroughColorAttributeName 設(shè)置刪除線顏色,UIColor對(duì)象,默認(rèn)是黑色
    NSUnderlineStyleAttributeName     設(shè)置下劃線,NSNumber對(duì)象 NSUnderlineStyle枚舉值
    NSUnderlineColorAttributeName     設(shè)置下劃線顏色,UIColor對(duì)象,默認(rèn)是黑色
    NSStrokeWidthAttributeName        設(shè)置筆畫(huà)寬度,NSNumber對(duì)象 正數(shù)中空 負(fù)數(shù)填充
    NSStrokeColorAttributeName        設(shè)置填充部分顏色,不是指字體顏色,UIColor對(duì)象
    NSShadowAttributeName             設(shè)置陰影屬性,取值為NSShadow對(duì)象
    NSTextEffectAttributeName         設(shè)置文本特殊效果 NSString對(duì)象 只有圖版印刷效果可用
    NSBaselineOffsetAttributeName     設(shè)置基線偏移量,NSNumber float對(duì)象 正數(shù)向上偏移,負(fù)數(shù)向下偏移
    NSObliquenessAttributeName        設(shè)置字體傾斜度,NSNumber float對(duì)象,正數(shù)右傾斜,負(fù)數(shù)左傾斜
    NSExpansionAttributeName          設(shè)置文本橫向拉伸屬性,NSNumber float對(duì)象,正數(shù)橫向拉伸文本,負(fù)數(shù)壓縮
    NSWritingDirectionAttributeName   設(shè)置文字書(shū)寫(xiě)方向,從左向右或者右向左
    NSVerticalGlyphFormAttributeName  設(shè)置文本排版方向,NSNumber對(duì)象。0 橫向排版,1 豎向排版
    NSLinkAttributeName               設(shè)置文本超鏈接,點(diǎn)擊可以打開(kāi)指定URL地址
    NSAttachmentAttributeName         設(shè)置文本附件,取值為NSTextAttachment對(duì)象,一般為圖文混排
    NSParagraphStyleAttributeName     設(shè)置文本段落排版,為NSParagraphStyle對(duì)象

11.18新增NSParagraphStyleAttributeName的屬性介紹

paragraphStyle.lineSpacing = 0.0;//增加行高
paragraphStyle.headIndent = 0;//頭部縮進(jìn),相當(dāng)于左padding
paragraphStyle.tailIndent = 0;//相當(dāng)于右padding
paragraphStyle.lineHeightMultiple = 0;//行間距是多少倍
paragraphStyle.alignment = NSTextAlignmentLeft;//對(duì)齊方式
paragraphStyle.firstLineHeadIndent = 0;//首行頭縮進(jìn)
paragraphStyle.paragraphSpacing = 0;//段落后面的間距
paragraphStyle.paragraphSpacingBefore = 0;//段落之前的間距

效果圖:


1.NSFontAttributeName

說(shuō)明:該屬性用于改變一段文本的字體。如果不指定該屬性,則默認(rèn)為12-point Helvetica(Neue)

2.NSForegroundColorAttributeName

說(shuō)明:該屬性用于指定一段文本的字體顏色。如果不指定該屬性,則默認(rèn)為黑色

3.NSBackgroundColorAttributeName

說(shuō)明:設(shè)置文字背景顏色

NSString *string = @"落霞與孤鶩齊飛,秋水共長(zhǎng)天一色";
    NSMutableAttributedString *mutableAttriteStr = [[NSMutableAttributedString alloc] init];
    NSAttributedString *attributeStr1 = [[NSAttributedString alloc] initWithString:[string substringWithRange:NSMakeRange(0, 3)] attributes:@{NSFontAttributeName :[UIFont fontWithName:@"futura" size:12],NSForegroundColorAttributeName : [UIColor redColor],NSBackgroundColorAttributeName : [UIColor yellowColor]}];
    NSAttributedString *attributeStr4 = [[NSAttributedString alloc] initWithString:[string substringWithRange:NSMakeRange(3, 4)] attributes:@{NSFontAttributeName :[UIFont fontWithName:@"futura" size:22],NSForegroundColorAttributeName : [UIColor redColor],NSBackgroundColorAttributeName : [UIColor yellowColor]}];
    NSAttributedString *attributeStr2 = [[NSAttributedString alloc] initWithString:[string substringWithRange:NSMakeRange(8, 4)] attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:22],NSForegroundColorAttributeName : [UIColor blackColor],NSBackgroundColorAttributeName : [UIColor lightGrayColor]}];
     NSAttributedString *attributeStr5 = [[NSAttributedString alloc] initWithString:[string substringWithRange:NSMakeRange(12, 3)] attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:12],NSForegroundColorAttributeName : [UIColor blackColor],NSBackgroundColorAttributeName : [UIColor lightGrayColor]}];
    NSAttributedString *attriteStr3 = [[NSAttributedString alloc] initWithString:[string substringWithRange:NSMakeRange(7, 1)] attributes:@{NSBackgroundColorAttributeName : [UIColor greenColor]}];
    [mutableAttriteStr appendAttributedString:attributeStr1];
    [mutableAttriteStr appendAttributedString:attributeStr4];
    [mutableAttriteStr appendAttributedString:attriteStr3];
   
    [mutableAttriteStr appendAttributedString:attributeStr2];
     [mutableAttriteStr appendAttributedString:attributeStr5];
    self.label1.attributedText = mutableAttriteStr;

4.NSLigatureAttributeName

連體字符是指某些連在一起的字符,它們采用單個(gè)的圖元符號(hào)。0 表示沒(méi)有連體字符。1

表示使用默認(rèn)的連體字符。2表示使用所有連體符號(hào)。默認(rèn)值為 1(注意,iOS 不支持值

為 2)

// 連體藝術(shù)字,不是每個(gè)都能連起的,f和l  f和i就可以,其他各位可以自己去試試
    self.label2.attributedText = [[NSAttributedString alloc] initWithString:@"flush and fily" attributes:@{NSLigatureAttributeName : [NSNumber numberWithInt:1],NSFontAttributeName : [UIFont fontWithName:@"futura" size:30]}];
    
    NSMutableAttributedString *mutableAttributeStr2 = [[NSMutableAttributedString alloc] init];
    NSAttributedString *string6 = [[NSAttributedString alloc] initWithString:@"ABCDE " attributes:@{NSKernAttributeName : [NSNumber numberWithInt:-3],NSForegroundColorAttributeName : [UIColor redColor]}];
    NSAttributedString *string7 = [[NSAttributedString alloc] initWithString:@"FGHIJ " attributes:@{NSKernAttributeName : [NSNumber numberWithInt:0],NSForegroundColorAttributeName : [UIColor yellowColor]}];
    NSAttributedString *string8 = [[NSAttributedString alloc] initWithString:@"KLMNO " attributes:@{NSKernAttributeName : @(15),NSForegroundColorAttributeName : [UIColor blueColor]}];
    [mutableAttributeStr2 appendAttributedString:string6];
    [mutableAttributeStr2 appendAttributedString:string7];
    [mutableAttributeStr2 appendAttributedString:string8];
    self.label3.attributedText = mutableAttributeStr2;

5.NSKernAttributeName

字符間距正值間距加寬,負(fù)值間距變窄

NSMutableAttributedString *mutableAttributeStr2 = [[NSMutableAttributedString alloc] init];
    NSAttributedString *string6 = [[NSAttributedString alloc] initWithString:@"ABCDE " attributes:@{NSKernAttributeName : [NSNumber numberWithInt:-3],NSForegroundColorAttributeName : [UIColor redColor]}];
    NSAttributedString *string7 = [[NSAttributedString alloc] initWithString:@"FGHIJ " attributes:@{NSKernAttributeName : [NSNumber numberWithInt:0],NSForegroundColorAttributeName : [UIColor yellowColor]}];
    NSAttributedString *string8 = [[NSAttributedString alloc] initWithString:@"KLMNO " attributes:@{NSKernAttributeName : @(15),NSForegroundColorAttributeName : [UIColor blueColor]}];
    [mutableAttributeStr2 appendAttributedString:string6];
    [mutableAttributeStr2 appendAttributedString:string7];
    [mutableAttributeStr2 appendAttributedString:string8];
    self.label3.attributedText = mutableAttributeStr2;

6.NSStrikethroughStyleAttributeName 和 NSStrikethroughColorAttributeName

NSStrikethroughStyleAttributeName 設(shè)置刪除線,取值為 NSNumber 對(duì)象(整數(shù)),

NSStrikethroughColorAttributeName 設(shè)置刪除線顏色

枚舉常量 NSUnderlineStyle中的值:

NSUnderlineStyleNone =0x00, 不設(shè)置

NSUnderlineStyleSingle =0x01, 設(shè)置單細(xì)刪除線

NSUnderlineStyleThickNS_ENUM_AVAILABLE(10_0,7_0) = 0x02, 設(shè)置粗單刪除線

NSUnderlineStyleDoubleNS_ENUM_AVAILABLE(10_0,7_0) = 0x09,雙細(xì)刪除線

20160807121301280.png
NSMutableAttributedString *mutableAttributeStr3 = [[NSMutableAttributedString alloc] init];
    NSAttributedString *string9 = [[NSAttributedString alloc] initWithString:@"只要998  " attributes:@{NSStrikethroughStyleAttributeName : @(NSUnderlineStyleSingle),NSStrikethroughColorAttributeName : [UIColor redColor]}];
    NSAttributedString *string10 = [[NSAttributedString alloc] initWithString:@"真的998  " attributes:@{NSStrikethroughStyleAttributeName : @(NSUnderlineStyleThick),NSStrikethroughColorAttributeName : [UIColor blueColor]}];
    NSAttributedString *string11 = [[NSAttributedString alloc] initWithString:@"好吧9塊拿走" attributes:@{NSStrikethroughStyleAttributeName : @(NSUnderlineStyleDouble),NSStrikethroughColorAttributeName : [UIColor yellowColor]}];
    
    [mutableAttributeStr3 appendAttributedString:string9];
    [mutableAttributeStr3 appendAttributedString:string10];
    [mutableAttributeStr3 appendAttributedString:string11];
    self.label4.attributedText = mutableAttributeStr3;

7.NSUnderlineStyleAttributeName 和 NSUnderlineColorAttributeName

給文字加下劃線和更換下劃線顏色,屬性和上面的刪除線都是一樣用的

20160807122021815.png
NSMutableAttributedString *mutableAttributeStr4 = [[NSMutableAttributedString alloc] init];
    NSAttributedString *string12 = [[NSAttributedString alloc] initWithString:@"只要888  " attributes:@{NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle),NSUnderlineColorAttributeName : [UIColor redColor]}];
    NSAttributedString *string13 = [[NSAttributedString alloc] initWithString:@"真的88  " attributes:@{NSUnderlineStyleAttributeName : @(NSUnderlineStyleThick),NSUnderlineColorAttributeName : [UIColor purpleColor]}];
    NSAttributedString *string14 = [[NSAttributedString alloc] initWithString:@"好吧8塊拿走" attributes:@{NSUnderlineStyleAttributeName : @(NSUnderlineStyleDouble),NSUnderlineColorAttributeName : [UIColor yellowColor]}];
    
    [mutableAttributeStr4 appendAttributedString:string12];
    [mutableAttributeStr4 appendAttributedString:string13];
    [mutableAttributeStr4 appendAttributedString:string14];
    self.label5.attributedText = mutableAttributeStr4;

8.NSStrokeWidthAttributeName 和 NSStrokeColorAttributeName

設(shè)置文字描邊顏色,需要和NSStrokeWidthAttributeName設(shè)置描邊寬度,這樣就能使文字空心.

NSStrokeWidthAttributeName 這個(gè)屬性所對(duì)應(yīng)的值是一個(gè) NSNumber 對(duì)象(小數(shù))。該值改變筆畫(huà)寬度(相對(duì)于字體 size 的百分比),負(fù)值填充效果,正值中空效果,默認(rèn)為 0,即不改變。正數(shù)只改變描邊寬度。負(fù)數(shù)同時(shí)改變文字的描邊和填充寬度。例如,對(duì)于常見(jiàn)的空心字,這個(gè)值通常為 3.0。
同時(shí)設(shè)置了空心的兩個(gè)屬性,并且 NSStrokeWidthAttributeName 屬性設(shè)置為整數(shù),文字前景色就無(wú)效果了

NSMutableAttributedString *mutableAttributeStr5 = [[NSMutableAttributedString alloc] init];
    // 如果這個(gè)stroke的width正數(shù)的時(shí)候,字體就中空了,那么前景色就設(shè)置無(wú)效了
    NSAttributedString *string15 = [[NSAttributedString alloc] initWithString:@"HELLO " attributes:@{NSStrokeWidthAttributeName : @(5),NSStrokeColorAttributeName : [UIColor yellowColor],NSFontAttributeName : [UIFont boldSystemFontOfSize:30],NSForegroundColorAttributeName : [UIColor redColor]}];
    NSAttributedString *string16 = [[NSAttributedString alloc] initWithString:@"YUKD  " attributes:@{NSStrokeWidthAttributeName : @(0),NSStrokeColorAttributeName : [UIColor redColor],NSFontAttributeName : [UIFont boldSystemFontOfSize:30]}];
    NSAttributedString *string17 = [[NSAttributedString alloc] initWithString:@"GOOGLE" attributes:@{NSStrokeWidthAttributeName : @(-5),NSStrokeColorAttributeName : [UIColor greenColor],NSFontAttributeName : [UIFont boldSystemFontOfSize:30],NSForegroundColorAttributeName : [UIColor lightGrayColor]}];
    
    [mutableAttributeStr5 appendAttributedString:string15];
    [mutableAttributeStr5 appendAttributedString:string16];
    [mutableAttributeStr5 appendAttributedString:string17];
    self.label6.attributedText = mutableAttributeStr5;

9.NSShadowAttributeName

設(shè)置文字陰影,取值為NSShadow對(duì)象

20160807123121931.png
// 設(shè)置陰影
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowBlurRadius = 5.0f; // 模糊度
    shadow.shadowColor = [UIColor blueColor];
    shadow.shadowOffset = CGSizeMake(1, 5);
    NSAttributedString *string20 = [[NSAttributedString alloc] initWithString:@"HELLO_HALY_璟" attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:30],NSForegroundColorAttributeName : [UIColor redColor],NSShadowAttributeName : shadow}];
    
    self.label7.attributedText = string20;

10.NSTextEffectAttributeNam

NSTextEffectAttributeName //設(shè)置文本特殊效果,取值為NSString類型,目前只有一個(gè)可用效果 NSTextEffectLetterpressStyle(凸版印刷效果)

20160807123246574.png
// 文本印刷,我也不知道是什么鬼
    // 設(shè)置陰影
    NSAttributedString *string21 = [[NSAttributedString alloc] initWithString:@"HELLO_HALY_璟" attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:30],NSForegroundColorAttributeName : [UIColor redColor],NSShadowAttributeName : shadow,NSTextEffectAttributeName : NSTextEffectLetterpressStyle}];
    
    self.label8.attributedText = string21;

11.NSBaselineOffsetAttributeName

文字基線偏移,想要達(dá)到以下圖片的效果,就要設(shè)置需要的文字偏移,正數(shù)上移,負(fù)數(shù)下移

20160807123730298.png
// 設(shè)置文本的基線 負(fù)數(shù)向下  正數(shù)向上  0不變
    UIFont *bigFont = [UIFont boldSystemFontOfSize:36];
    UIFont *smallFont = [UIFont boldSystemFontOfSize:bigFont.pointSize / 2];
    CGFloat capHeight = bigFont.pointSize - smallFont.pointSize;
    NSMutableAttributedString *attributeString6 = [[NSMutableAttributedString alloc] init];
    NSAttributedString *string22 = [[NSAttributedString alloc] initWithString:@"¥" attributes:@{NSFontAttributeName : smallFont,NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(0)}];
    NSAttributedString *string23 = [[NSAttributedString alloc] initWithString:@"9" attributes:@{NSFontAttributeName : bigFont,NSForegroundColorAttributeName : [UIColor blueColor],NSBaselineOffsetAttributeName : @(0)}];
    NSAttributedString *string24 = [[NSAttributedString alloc] initWithString:@".99" attributes:@{NSFontAttributeName : smallFont,NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(0)}];
    
    
    NSAttributedString *string28 = [[NSAttributedString alloc] initWithString:@"    七夕大促銷    " attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:24],NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(0),NSShadowAttributeName : shadow}];
    
    NSAttributedString *string25 = [[NSAttributedString alloc] initWithString:@"¥" attributes:@{NSFontAttributeName : smallFont,NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(capHeight - 5)}];
    NSAttributedString *string26 = [[NSAttributedString alloc] initWithString:@"0" attributes:@{NSFontAttributeName : bigFont,NSForegroundColorAttributeName : [UIColor redColor],NSBaselineOffsetAttributeName : @(0)}];
    NSAttributedString *string27 = [[NSAttributedString alloc] initWithString:@".09" attributes:@{NSFontAttributeName : smallFont,NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(capHeight - 5)}];
    [attributeString6 appendAttributedString:string22];
    [attributeString6 appendAttributedString:string23];
    [attributeString6 appendAttributedString:string24];
    [attributeString6 appendAttributedString:string28];
    [attributeString6 appendAttributedString:string25];
    [attributeString6 appendAttributedString:string26];
    [attributeString6 appendAttributedString:string27];
    self.label9.attributedText = attributeString6;
    
    NSMutableAttributedString *attributeString7 = [[NSMutableAttributedString alloc] init];
    
    NSAttributedString *string29 = [[NSAttributedString alloc] initWithString:@"Hello  " attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:40],NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(0)}];
    NSAttributedString *string30 = [[NSAttributedString alloc] initWithString:@"  World    " attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:20],NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(0)}];
    
    NSAttributedString *string31 = [[NSAttributedString alloc] initWithString:@"Hello  " attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:40],NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(0)}];
    NSAttributedString *string32 = [[NSAttributedString alloc] initWithString:@"  World" attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:20],NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(6)}];
    
    
    [attributeString7 appendAttributedString:string29];
    [attributeString7 appendAttributedString:string30];
    [attributeString7 appendAttributedString:string31];
    [attributeString7 appendAttributedString:string32];
    self.label10.attributedText = attributeString7;

12.NSObliquenessAttributeName

NSObliquenessAttributeName 設(shè)置字體傾斜度,取值為 NSNumber(float),正值右傾,負(fù)值左傾

13.NSExpansionAttributeName

NSExpansionAttributeName 設(shè)置字體的橫向拉伸,取值為NSNumber (float),正值拉伸 ,負(fù)值壓縮

14.NSVerticalGlyphFormAttributeName

NSVerticalGlyphFormAttributeName 設(shè)置文字排版方向,取值為NSNumber對(duì)象(整數(shù)),0表示橫排文本,1表示豎排文本 在iOS中只支持0

20160807124536918.png
//    說(shuō)明:NSVerticalGlyphFormAttributeName 設(shè)置文字排版方向,取值為NSNumber對(duì)象(整數(shù)),0表示橫排文本,1表示豎排文本 在iOS中只支持0
//說(shuō)明:NSObliquenessAttributeName 設(shè)置字體傾斜度,取值為 NSNumber(float),正值右傾,負(fù)值左傾
//    NSExpansionAttributeName
//    說(shuō)明:NSExpansionAttributeName 設(shè)置字體的橫向拉伸,取值為NSNumber (float),正值拉伸 ,負(fù)值壓縮
    NSMutableAttributedString *mutableAttriteStr = [[NSMutableAttributedString alloc] init];
    NSAttributedString *attributeStr1 = [[NSAttributedString alloc] initWithString:@"HeHe_XiXi_mm" attributes:@{NSFontAttributeName :[UIFont boldSystemFontOfSize:30],NSForegroundColorAttributeName : [UIColor redColor],NSShadowAttributeName : shadow,NSObliquenessAttributeName : @(1),NSExpansionAttributeName : @(1),NSVerticalGlyphFormAttributeName : @(1)}];
    [mutableAttriteStr appendAttributedString:attributeStr1];
    self.label1.attributedText = mutableAttriteStr;

15.NSWritingDirectionAttributeName

文字書(shū)寫(xiě)方向取值為以下組合為例 他是一個(gè)數(shù)組包含NSNumber 一般書(shū)寫(xiě)就是L ---R

那么我們多個(gè)需求也就是從R --- L

The values of the NSNumber objects should be 0, 1, 2, or 3, for LRE, RLE, LRO, or RLO respectively, and combinations of leftToRight and rightToLeft withNSTextWritingDirectionEmbedding or NSTextWritingDirectionOverride

20160807124955094.png
     // 文字書(shū)寫(xiě)方向
//    The values of the NSNumber objects should be 0, 1, 2, or 3, for LRE, RLE, LRO, or RLO respectively, and combinations of leftToRight and rightToLeft with NSTextWritingDirectionEmbedding or NSTextWritingDirectionOverride, as shown in Table 1.
//    NSWritingDirectionLeftToRight | NSTextWritingDirectionEmbedding
//    NSWritingDirectionRightToLeft | NSTextWritingDirectionEmbedding
//    NSWritingDirectionLeftToRight | NSTextWritingDirectionOverride
//    NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride
    NSMutableAttributedString *attributuStr2 = [[NSMutableAttributedString alloc] init];
    NSAttributedString *string1 = [[NSAttributedString alloc] initWithString:@"一切皆有可能" attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:24],NSForegroundColorAttributeName : [UIColor redColor],NSWritingDirectionAttributeName : @[@(3)]}];
    [attributuStr2 appendAttributedString:string1];
    self.label.attributedText = attributuStr2;

16.NSLinkAttributeName

這貨有點(diǎn)奇葩,所以很多人用第三方例如YYLabel來(lái)做,這東西不能在UILabel和UITextField使用,只能用UITextView來(lái)進(jìn)行,實(shí)現(xiàn)他的代理,在代理方法里面進(jìn)行URL跳轉(zhuǎn)

該方法返回YES就能打開(kāi)URL,NO不做任何事情

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange

注:

1.一定要實(shí)現(xiàn)UITextView的代理才能進(jìn)行URL跳轉(zhuǎn)

2.textView的editable屬性修改為NO,在編輯時(shí)不可點(diǎn)擊

20160807130600290.png
//    把 NSLinkAttributeName 屬性單獨(dú)列出來(lái),是因?yàn)樵?UILabel 和 UITextField 中是無(wú)法使用該屬性的。更準(zhǔn)確點(diǎn)說(shuō)是在UILabel 和 UITextField 中無(wú)法實(shí)現(xiàn)點(diǎn)擊鏈接啟動(dòng)瀏覽器打開(kāi)一個(gè)URL地址,因?yàn)樵诖诉^(guò)程中用到了一個(gè)代理函數(shù)。只能用在 UITextView 中
    self.textView.delegate = self;
    self.textView.scrollEnabled = NO;
    self.textView.editable = NO;
    self.textView.textContainer.lineFragmentPadding = 0;
    self.textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
    NSString *str = @"  跳轉(zhuǎn)到宓珂璟的博客";
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:str];
    [attrStr addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"http://blog.csdn.net/deft_mkjing"] range:[str rangeOfString:@"宓珂璟的博客"]];
    [attrStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:30] range:NSMakeRange(0, str.length)];
    self.textView.attributedText = attrStr;

17.NSTextAttachment

設(shè)置文本附件,取值為NSTextAttachment對(duì)象 常用于圖文混排

20160807131107937.png
 NSString *words = @"天才";
    NSMutableAttributedString *strAtt = [[NSMutableAttributedString alloc] initWithString:words attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:25]}];
    NSTextAttachment *attatch = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    attatch.bounds = CGRectMake(0, 0, 40, 30);
    attatch.image = [UIImage imageNamed:@"$DCZ2WLN9RWI6JF(Q`P_(NH.jpg"];
    NSTextAttachment *attatch1 = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    attatch1.bounds = CGRectMake(0, 0, 50, 30);
    attatch1.image = [UIImage imageNamed:@"%5T@J(4WKWSOX2~~PY0{4M0.jpg"];
    NSAttributedString *string8 = [NSAttributedString attributedStringWithAttachment:attatch];
    NSAttributedString *string9 = [NSAttributedString attributedStringWithAttachment:attatch1];
    [strAtt insertAttributedString:string8 atIndex:1];
    [strAtt insertAttributedString:string9 atIndex:0];
    self.label3.attributedText = strAtt;

基本的就介紹到這里了,上面那些已經(jīng)足夠用了,而且可以任意組合出想要的圖文混排,

如果遇到特殊的需求或者不能滿足的,那么就需要各位去試試往上的第三方富文本了

給個(gè)好用的大家可以試試點(diǎn)擊打開(kāi)鏈接

本次Demo地址:Demo傳送門(mén)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容