(轉載)iOS自己總結的超級詳細分解富文本大全(AttributedString)议泵,圖文混排很輕松

原文地址:http://blog.csdn.net/deft_mkjing/article/details/52141936

    NSFontAttributeName               設置字體大小和字體的類型 默認12 Helvetica(Neue)
    NSForegroundColorAttributeName    設置字體顏色克饶,默認黑色 UIColor對象
    NSBackgroundColorAttributeName    設置字體所在區(qū)域的背景顏色抬纸,默認為nil,透明色
    NSLigatureAttributeName           設置連體屬性弦悉,NSNumber對象 默認0 沒有連體
    NSKernAttributeName               設置字符間距窒典, NSNumber浮點型屬性 正數(shù)間距加大,負數(shù)間距縮小
    NSStrikethroughStyleAttributeName 設置刪除線稽莉,NSNumber對象
    NSStrikethroughColorAttributeName 設置刪除線顏色瀑志,UIColor對象,默認是黑色
    NSUnderlineStyleAttributeName     設置下劃線污秆,NSNumber對象 NSUnderlineStyle枚舉值
    NSUnderlineColorAttributeName     設置下劃線顏色劈猪,UIColor對象,默認是黑色
    NSStrokeWidthAttributeName        設置筆畫寬度良拼,NSNumber對象 正數(shù)中空 負數(shù)填充
    NSStrokeColorAttributeName        設置填充部分顏色战得,不是指字體顏色,UIColor對象
    NSShadowAttributeName             設置陰影屬性庸推,取值為NSShadow對象
    NSTextEffectAttributeName         設置文本特殊效果 NSString對象 只有圖版印刷效果可用
    NSBaselineOffsetAttributeName     設置基線偏移量常侦,NSNumber float對象 正數(shù)向上偏移浇冰,負數(shù)向下偏移
    NSObliquenessAttributeName        設置字體傾斜度,NSNumber float對象聋亡,正數(shù)右傾斜,負數(shù)左傾斜
    NSExpansionAttributeName          設置文本橫向拉伸屬性,NSNumber float對象溜歪,正數(shù)橫向拉伸文本庄岖,負數(shù)壓縮
    NSWritingDirectionAttributeName   設置文字書寫方向,從左向右或者右向左
    NSVerticalGlyphFormAttributeName  設置文本排版方向罪塔,NSNumber對象仅仆。0 橫向排版,1 豎向排版
    NSLinkAttributeName               設置文本超鏈接垢袱,點擊可以打開指定URL地址
    NSAttachmentAttributeName         設置文本附件墓拜,取值為NSTextAttachment對象,一般為圖文混排
    NSParagraphStyleAttributeName     設置文本段落排版请契,為NSParagraphStyle對象

11.18新增NSParagraphStyleAttributeName的屬性介紹


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



我去,大兄弟爽锥,有點多啊涌韩,各位大爺不要慌,聽小弟給你們一一道來氯夷,每個屬性一行臣樱,分分鐘教大家簡單學習富文本

OK,咱們現(xiàn)在開始一個一個絮叨


1.NSFontAttributeName
說明:該屬性用于改變一段文本的字體腮考。如果不指定該屬性雇毫,則默認為12-point Helvetica(Neue)


2.NSForegroundColorAttributeName
說明:該屬性用于指定一段文本的字體顏色。如果不指定該屬性踩蔚,則默認為黑色


3.NSBackgroundColorAttributeName
說明:設置文字背景顏色

[objc] view plain copy

NSString *string = @"落霞與孤鶩齊飛,秋水共長天一色";
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
連體字符是指某些連在一起的字符棚放,它們采用單個的圖元符號。0 表示沒有連體字符馅闽。1
表示使用默認的連體字符飘蚯。2表示使用所有連體符號。默認值為 1(注意福也,iOS 不支持值
為 2)


[objc] view plain copy

// 連體藝術字局骤,不是每個都能連起的,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
字符間距正值間距加寬峦甩,負值間距變窄

[objc] view plain copy

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 設置刪除線,取值為 NSNumber 對象(整數(shù))搬设,
NSStrikethroughColorAttributeName 設置刪除線顏色
枚舉常量 NSUnderlineStyle中的值:

NSUnderlineStyleNone =0x00, 不設置
NSUnderlineStyleSingle =0x01, 設置單細刪除線
NSUnderlineStyleThickNS_ENUM_AVAILABLE(10_0,7_0) = 0x02, 設置粗單刪除線
NSUnderlineStyleDoubleNS_ENUM_AVAILABLE(10_0,7_0) = 0x09,雙細刪除線


[objc] view plain copy

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


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



[objc] view plain copy

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

設置文字描邊顏色撕捍,需要和NSStrokeWidthAttributeName設置描邊寬度,這樣就能使文字空心.
NSStrokeWidthAttributeName 這個屬性所對應的值是一個 NSNumber 對象(小數(shù))泣洞。該值改變筆畫寬度(相對于字體 size 的百分比)忧风,負值填充效果,正值中空效果球凰,默認為 0狮腿,即不改變。正數(shù)只改變描邊寬度呕诉。負數(shù)同時改變文字的描邊和填充寬度缘厢。例如,對于常見的空心字甩挫,這個值通常為 3.0贴硫。同時設置了空心的兩個屬性,并且 NSStrokeWidthAttributeName 屬性設置為整數(shù)伊者,文字前景色就無效果了


[objc] view plain copy

NSMutableAttributedString *mutableAttributeStr5 = [[NSMutableAttributedString alloc] init];
// 如果這個stroke的width正數(shù)的時候英遭,字體就中空了,那么前景色就設置無效了
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設置文字陰影亦渗,取值為NSShadow對象

[objc] view plain copy

// 設置陰影
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 //設置文本特殊效果挖诸,取值為NSString類型,目前只有一個可用效果 NSTextEffectLetterpressStyle(凸版印刷效果)


[objc] view plain copy

// 文本印刷法精,我也不知道是什么鬼
// 設置陰影
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
文字基線偏移,想要達到以下圖片的效果多律,就要設置需要的文字偏移,正數(shù)上移搂蜓,負數(shù)下

[objc] view plain copy

// 設置文本的基線 負數(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 設置字體傾斜度狼荞,取值為 NSNumber(float)洛勉,正值右傾,負值左傾****13.******NSExpansionAttributeName****
NSExpansionAttributeName 設置字體的橫向拉伸,取值為NSNumber (float)殷勘,正值拉伸 此再,負值壓縮
14.NSVerticalGlyphFormAttributeName
NSVerticalGlyphFormAttributeName 設置文字排版方向玲销,取值為NSNumber對象(整數(shù)),0表示橫排文本贤斜,1表示豎排文本 在ios中只支持0

[objc] view plain copy

// 說明:NSVerticalGlyphFormAttributeName 設置文字排版方向策吠,取值為NSNumber對象(整數(shù)),0表示橫排文本带族,1表示豎排文本 在iOS中只支持0
//說明:NSObliquenessAttributeName 設置字體傾斜度蟀给,取值為 NSNumber(float)跋理,正值右傾,負值左傾
// NSExpansionAttributeName
// 說明:NSExpansionAttributeName 設置字體的橫向拉伸肚邢,取值為NSNumber (float)拭卿,正值拉伸 记劈,負值壓縮
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ù)組包含NSNumber 一般書寫就是L ---R
那么我們多個需求也就是從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

[objc] view plain copy

// 文字書寫方向  

// 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
這貨有點奇葩换途,所以很多人用第三方例如YYLabel來做军拟,這東西不能在UILabel和UITextField使用誓禁,只能用UITextView來進行,實現(xiàn)他的代理辫继,在代理方法里面進行URL跳轉

  • (BOOL)textView:(UITextView )textView shouldInteractWithURL:(NSURL)URL inRange:(NSRange)characterRange
    該方法返回YES就能打開URL姑宽,NO不做任何事情
    注:
    1.一定要實現(xiàn)UITextView的代理才能進行URL跳轉
    2.textView的editable屬性修改為NO,在編輯時不可點擊

[objc] view plain copy

// 把 NSLinkAttributeName 屬性單獨列出來炮车,是因為在 UILabel 和 UITextField 中是無法使用該屬性的瘦穆。更準確點說是在UILabel 和 UITextField 中無法實現(xiàn)點擊鏈接啟動瀏覽器打開一個URL地址扛或,因為在此過程中用到了一個代理函數(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 = @" 跳轉到宓珂璟的博客";
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****設置文本附件麸拄,取值為NSTextAttachment對象 常用于圖文混排

[objc] view plain copy

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)足夠用了淮椰,而且可以任意組合出想要的圖文混排纳寂,
如果遇到特殊的需求或者不能滿足的毙芜,那么就需要各位去試試往上的第三方富文本了
給個好用的大家可以試試點擊打開鏈接



本次Demo地址:Demo傳送門

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末晦雨,一起剝皮案震驚了整個濱河市隘冲,隨后出現(xiàn)的幾起案子展辞,更是在濱河造成了極大的恐慌,老刑警劉巖洽腺,帶你破解...
    沈念sama閱讀 216,496評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件已脓,死亡現(xiàn)場離奇詭異通殃,居然都是意外死亡画舌,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來齐疙,“玉大人旭咽,你說我怎么就攤上這事〗嗡” “怎么了勾缭?”我有些...
    開封第一講書人閱讀 162,632評論 0 353
  • 文/不壞的土叔 我叫張陵俩由,是天一觀的道長癌蚁。 經(jīng)常有香客問我匈勋,道長,這世上最難降的妖魔是什么痘系? 我笑而不...
    開封第一講書人閱讀 58,180評論 1 292
  • 正文 為了忘掉前任汰翠,我火速辦了婚禮昭雌,結果婚禮上烛卧,老公的妹妹穿的比我還像新娘妓局。我一直安慰自己好爬,他們只是感情好甥啄,可當我...
    茶點故事閱讀 67,198評論 6 388
  • 文/花漫 我一把揭開白布蜈漓。 她就那樣靜靜地躺著融虽,像睡著了一般。 火紅的嫁衣襯著肌膚如雪驼侠。 梳的紋絲不亂的頭發(fā)上倒源,一...
    開封第一講書人閱讀 51,165評論 1 299
  • 那天笋熬,我揣著相機與錄音腻菇,去河邊找鬼筹吐。 笑死,一個胖子當著我的面吹牛嘉竟,可吹牛的內容都是我干的洋侨。 我是一名探鬼主播希坚,決...
    沈念sama閱讀 40,052評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼裁僧,長吁一口氣:“原來是場噩夢啊……” “哼慕购!你這毒婦竟也來了脓钾?” 一聲冷哼從身側響起桩警,我...
    開封第一講書人閱讀 38,910評論 0 274
  • 序言:老撾萬榮一對情侶失蹤捶枢,失蹤者是張志新(化名)和其女友劉穎烂叔,沒想到半個月后固歪,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體牢裳,經(jīng)...
    沈念sama閱讀 45,324評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡蒲讯,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,542評論 2 332
  • 正文 我和宋清朗相戀三年判帮,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片悦昵。...
    茶點故事閱讀 39,711評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡但指,死狀恐怖枚赡,靈堂內的尸體忽然破棺而出谓谦,到底是詐尸還是另有隱情反粥,我是刑警寧澤疲迂,帶...
    沈念sama閱讀 35,424評論 5 343
  • 正文 年R本政府宣布尤蒿,位于F島的核電站腰池,受9級特大地震影響忙芒,放射性物質發(fā)生泄漏呵萨。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,017評論 3 326
  • 文/蒙蒙 一囱皿、第九天 我趴在偏房一處隱蔽的房頂上張望嘱腥。 院中可真熱鬧德谅,春花似錦窄做、人聲如沸椭盏。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽盆偿。三九已至准浴,卻和暖如春乐横,著一層夾襖步出監(jiān)牢的瞬間今野,已是汗流浹背条霜。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評論 1 269
  • 我被黑心中介騙來泰國打工宰睡, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留溯乒,地道東北人裆悄。 一個月前我還...
    沈念sama閱讀 47,722評論 2 368
  • 正文 我出身青樓光稼,卻偏偏與公主長得像艾君,于是被迫代替她去往敵國和親肄方。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,611評論 2 353

推薦閱讀更多精彩內容