NSAttributedString那點事

原文鏈接

之前做項目時遇到一個問題: 

    使用UITextView顯示一段電影的簡介,由於字數比較多,所以字體設置的很小,行間距和段間距也很小,一大段文字擠在一起看起來很彆扭,想要把行間距調大,結果在XCode中查遍其所有屬性才發現,UITextView居然沒有調整行間距的接口,於是忍住不心裏抱怨了一下下。 

    但是問題還是要解決的,上網一查才發現,iOS不僅有富文本處理的功能,而且對於文字排版的處理能力那是相當的強大,看來我是孤陋寡聞了。


正題開始之前插播一點基礎知識: 


在iOS中或者Mac OS X中怎樣才能將一個字符串繪製到屏幕上呢? 


    簡單來說,是通過控件來完成的,而這些控件都封裝在UIKit框架中(對於Mac OS X是AppKit框架),在UIKit中常用來在屏幕上顯示字符串的控件有3個: 

        UILabel 

        UITextField 

        UITextView 

    然而這些控件本身對文本的展現方式很單一,通常僅僅能夠控制字體樣式、大小、顏色、加粗、斜體等等,而對於行距控制,字距控制,段落控制等高級功能卻無能爲力。 

    此時不免要提起一個非常強大的文本排版框架CoreText.framework。
    CoreText框架是基於 iOS 3.2+ 和 OSX 10.5+ 的一種能夠對文本格式和文本佈局進行精細控制的文本引擎。它良好的結合了 UIKit 和 Core Graphics/Quartz: 


    UIKit 的 UILabel 允許你通過在 IB 中簡單的拖曳添加文本,但你不能改變文本的顏色和其中的單詞。
    Core Graphics/Quartz幾乎允許你做任何系統允許的事情,但你需要爲每個字形計算位置,並畫在屏幕上。 


    CoreText正結合了這兩者!你自己可以完全控制位置、佈局、類似文本大小和顏色這樣的屬性,CoreText將幫你完善其它的東西??類似文本換行、字體呈現等等。 


    然而,CoreText.framework本身非常龐大,學習成本較高,使用起來也不是很方便,所以一般不是特殊需要,很少會有人去使用它。 


    隨着iOS6 API的發佈,文字顯示的API越來越完善,其中一個重要的更新是在UITextField,UITextView和UILabel中加入了對AttributedString的支持,實現行距控制,字距控制,段落控制等高級功能也不必再去使用深奧的CoreText框架。 


    而iOS7的發佈,蘋果又引入了TextKit,TextKit是一個快速而又現代化的文字排版和渲染引擎。 

    TextKit並沒有新增類,只是在原有的文本顯示控件上進行了封裝,可以在平時我們最喜歡使用的UILabel,UITextField,UITextView等控件裏面使用,其最主要的作用就是爲程序提供文字排版和渲染的功能。
    蘋果引入TextKit的目的並非要取代已有的CoreText框架,雖然CoreText的主要作用也是用於文字的排版和渲染,但它是一種先進而又處於底層技術,如果我們需要將文本內容直接渲染到圖形上下文(Graphics context)時,從性能和易用性來考慮,最佳方案就是使用CoreText。而如果我們需要直接利用蘋果提供的一些控件(如UITextView、UILabel和UITextField等)對文字進行排版,那麼藉助於UIKit中TextKit提供的API無疑更爲方便快捷。
    TextKit在文字處理方面具有非常強大的功能,並且開發者可以對TextKit進行定製和擴展。據悉,蘋果利用了2年的時間來開發TextKit,相信這對許多開發者來說都是福音。

    然而,無論CoreText還是TextKit都不在本文討論的範疇,因爲它們都是非常龐大的體系,而我們的需求通過一個簡單小巧的AttributedString就可以輕鬆搞定,所以本文的關注點只有一個,那就是AttributedString,至於CoreText和TextKit,在真正需要的時候再進行深入研究和總結。 


OK,?嗦完畢,進入正題。

    與NSString類似,在iOS中AttributedString也分爲NSAttributedString和NSMutableAttributedString,不同的是,AttributedString對象多了一個Attribute的概念,一個AttributedString的對象包含很多的屬性,每一個屬性都有其對應的字符區域,在這裏是使用NSRange來進行描述的。 

    使用AttributedString的方式通常有兩種: 

方式一: 

    首先初始化一個NSMutableAttributedString,然後向裏面添加文字樣式,最後將它賦給控件的AttributedText,該方法適合於文本較少而又需要分段精細控制的情況。


[objc] view plain copy print ? 
NSString *originStr = @"Hello,中秋節!";      //方式一      //創建 NSMutableAttributedString   NSMutableAttributedString *attributedStr01 = [[NSMutableAttributedString alloc] initWithString: originStr];      //添加屬性      //給所有字符設置字體爲Zapfino,字體高度爲15像素   [attributedStr01 addAttribute: NSFontAttributeName value: [UIFont fontWithName: @"Zapfino" size: 15]                                                      range: NSMakeRange(0, originStr.length)];   //分段控制,最開始4個字符顏色設置成藍色   [attributedStr01 addAttribute: NSForegroundColorAttributeName value: [UIColor blueColor] range: NSMakeRange(0, 4)];   //分段控制,第5個字符開始的3個字符,即第5、6、7字符設置爲紅色   [attributedStr01 addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(4, 3)];      //賦值給顯示控件label01的 attributedText   _label01.attributedText = attributedStr01;   
    NSString *originStr = @"Hello,中秋節!";
    
    //方式一
    
    //創建 NSMutableAttributedString
    NSMutableAttributedString *attributedStr01 = [[NSMutableAttributedString alloc] initWithString: originStr];
    
    //添加屬性
    
    //給所有字符設置字體爲Zapfino,字體高度爲15像素
    [attributedStr01 addAttribute: NSFontAttributeName value: [UIFont fontWithName: @"Zapfino" size: 15]
                                                       range: NSMakeRange(0, originStr.length)];
    //分段控制,最開始4個字符顏色設置成藍色
    [attributedStr01 addAttribute: NSForegroundColorAttributeName value: [UIColor blueColor] range: NSMakeRange(0, 4)];
    //分段控制,第5個字符開始的3個字符,即第5、6、7字符設置爲紅色
    [attributedStr01 addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(4, 3)];
    
    //賦值給顯示控件label01的 attributedText
    _label01.attributedText = attributedStr01;



運行結果: 


iOS之富文本0


方式二: 

   首先創建屬性字典,初始化各種屬性,然後和需要控制的文本一起創建並賦值給控件的AttributedText,該方法適合於需要控制的文本較多整體控制的情況,通常是從文件中讀取的大段文本控制。 


[objc] view plain copy print ? 
//方式二      //創建屬性字典   NSDictionary *attrDict = @{ NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 15],                               NSForegroundColorAttributeName: [UIColor blueColor] };      //創建 NSAttributedString 並賦值   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict];   
    //方式二
    
    //創建屬性字典
    NSDictionary *attrDict = @{ NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 15],
                                NSForegroundColorAttributeName: [UIColor blueColor] };

    //創建 NSAttributedString 並賦值
    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict];

運行結果: 


iOS之富文本1    

    通過對比兩個例子可以看出,方式一比較容易處理複雜的格式,但是屬性設置比較繁多複雜,而方式二的屬性設置比較簡單明瞭,卻不善於處理複雜多樣的格式控制,但是不善於並不等於不能,可以通過屬性字符串分段的方式來達到方式一的效果,如下: 


[objc] view plain copy print ? 
//方式二的分段處理   //第一段   NSDictionary *attrDict1 = @{ NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 15],                                NSForegroundColorAttributeName: [UIColor blueColor] };   NSAttributedString *attrStr1 = [[NSAttributedString alloc] initWithString: [originStr substringWithRange: NSMakeRange(0, 4)] attributes: attrDict1];      //第二段   NSDictionary *attrDict2 = @{ NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 15],                                NSForegroundColorAttributeName: [UIColor redColor] };   NSAttributedString *attrStr2 = [[NSAttributedString alloc] initWithString: [originStr substringWithRange: NSMakeRange(4, 3)] attributes: attrDict2];      //第三段   NSDictionary *attrDict3 = @{ NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 15],                                NSForegroundColorAttributeName: [UIColor blackColor] };   NSAttributedString *attrStr3 = [[NSAttributedString alloc] initWithString: [originStr substringWithRange:                                                                               NSMakeRange(7, originStr.length - 4 - 3)] attributes: attrDict3];   //合併   NSMutableAttributedString *attributedStr03 = [[NSMutableAttributedString alloc] initWithAttributedString: attrStr1];   [attributedStr03 appendAttributedString: attrStr2];   [attributedStr03 appendAttributedString: attrStr3];      _label03.attributedText = attributedStr03;   
    //方式二的分段處理
    //第一段
    NSDictionary *attrDict1 = @{ NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 15],
                                 NSForegroundColorAttributeName: [UIColor blueColor] };
    NSAttributedString *attrStr1 = [[NSAttributedString alloc] initWithString: [originStr substringWithRange: NSMakeRange(0, 4)] attributes: attrDict1];
    
    //第二段
    NSDictionary *attrDict2 = @{ NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 15],
                                 NSForegroundColorAttributeName: [UIColor redColor] };
    NSAttributedString *attrStr2 = [[NSAttributedString alloc] initWithString: [originStr substringWithRange: NSMakeRange(4, 3)] attributes: attrDict2];
    
    //第三段
    NSDictionary *attrDict3 = @{ NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 15],
                                 NSForegroundColorAttributeName: [UIColor blackColor] };
    NSAttributedString *attrStr3 = [[NSAttributedString alloc] initWithString: [originStr substringWithRange:
                                                                                NSMakeRange(7, originStr.length - 4 - 3)] attributes: attrDict3];
    //合併
    NSMutableAttributedString *attributedStr03 = [[NSMutableAttributedString alloc] initWithAttributedString: attrStr1];
    [attributedStr03 appendAttributedString: attrStr2];
    [attributedStr03 appendAttributedString: attrStr3];
    
    _label03.attributedText = attributedStr03;

運行結果: 


iOS之富文本2


    好了,講完AttributedString的創建方式,下面研究下AttributedString究竟可以設置哪些屬性,具體來說,有以下21個: 


[objc] view plain copy print ? 
// 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 對象   
// 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 對象


下面就一一舉例說明: 


1. NSFontAttributeName 


[objc] view plain copy print ? 
//NSForegroundColorAttributeName 設置字體顏色,取值爲 UIColor,默認爲黑色      NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };   NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };   NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };      _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];   
    //NSForegroundColorAttributeName 設置字體顏色,取值爲 UIColor,默認爲黑色
    
    NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };
    NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };
    NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };
    
    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];

運行結果: 


iOS之富文本3


注意: 

    NSForegroundColorAttributeName設置的顏色與UILabel的textColor屬性設置的顏色在地位上是相等的,誰最後賦值,最終顯示的就是誰的顏色。


2. NSBackgroundColorAttributeName 


[objc] view plain copy print ? 
//NSForegroundColorAttributeName 設置字體顏色,取值爲 UIColor,默認爲黑色      NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };   NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };   NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };      _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];         //NSBackgroundColorAttributeName 設置字體所在區域背景的顏色,取值爲UIColor,默認值爲nil      NSDictionary *attrDict4 = @{ NSBackgroundColorAttributeName: [UIColor orangeColor] };   NSDictionary *attrDict5 = @{ NSBackgroundColorAttributeName: [UIColor redColor] };   NSDictionary *attrDict6 = @{ NSBackgroundColorAttributeName: [UIColor cyanColor] };      _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict4];   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict5];   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict6];   
    //NSForegroundColorAttributeName 設置字體顏色,取值爲 UIColor,默認爲黑色
    
    NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };
    NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };
    NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };
    
    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
  
    
    //NSBackgroundColorAttributeName 設置字體所在區域背景的顏色,取值爲UIColor,默認值爲nil
    
    NSDictionary *attrDict4 = @{ NSBackgroundColorAttributeName: [UIColor orangeColor] };
    NSDictionary *attrDict5 = @{ NSBackgroundColorAttributeName: [UIColor redColor] };
    NSDictionary *attrDict6 = @{ NSBackgroundColorAttributeName: [UIColor cyanColor] };
    
    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict4];
    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict5];
    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict6];

運行結果: 


iOS之富文本4


    仔細觀察會發現個問題,我並沒有關閉 NSForegroundColorAttributeName 屬性,但是在運行結果中,所有字體的顏色都變成了默認色??黑色,這說明 NSForegroundColorAttributeName 和 NSBackgroundColorAttributeName 的低位是相等的,跟前面介紹的 textColor 一樣,哪個屬性最後一次賦值,就會沖掉前面的效果,若是我們把屬性代碼順序交換以下 


[objc] view plain copy print ? 
//NSBackgroundColorAttributeName 設置字體所在區域背景的顏色,取值爲UIColor,默認值爲nil      NSDictionary *attrDict4 = @{ NSBackgroundColorAttributeName: [UIColor orangeColor] };   NSDictionary *attrDict5 = @{ NSBackgroundColorAttributeName: [UIColor redColor] };   NSDictionary *attrDict6 = @{ NSBackgroundColorAttributeName: [UIColor cyanColor] };      _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict4];   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict5];   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict6];      //NSForegroundColorAttributeName 設置字體顏色,取值爲 UIColor,默認爲黑色      NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };   NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };   NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };      _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];   
    //NSBackgroundColorAttributeName 設置字體所在區域背景的顏色,取值爲UIColor,默認值爲nil
    
    NSDictionary *attrDict4 = @{ NSBackgroundColorAttributeName: [UIColor orangeColor] };
    NSDictionary *attrDict5 = @{ NSBackgroundColorAttributeName: [UIColor redColor] };
    NSDictionary *attrDict6 = @{ NSBackgroundColorAttributeName: [UIColor cyanColor] };
    
    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict4];
    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict5];
    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict6];
    
    //NSForegroundColorAttributeName 設置字體顏色,取值爲 UIColor,默認爲黑色
    
    NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };
    NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };
    NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };
    
    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];

運行結果: 


iOS之富文本5


但是textColor屬性可以與 NSBackgroundColorAttributeName 屬性疊加 


[objc] view plain copy print ? 
_label01.textColor = [UIColor greenColor];   _label02.textColor = [UIColor yellowColor];   _label03.textColor = [UIColor blueColor];      //NSForegroundColorAttributeName 設置字體顏色,取值爲 UIColor,默認爲黑色      NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };   NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };   NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };      _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];         //NSBackgroundColorAttributeName 設置字體所在區域背景的顏色,取值爲UIColor,默認值爲nil      NSDictionary *attrDict4 = @{ NSBackgroundColorAttributeName: [UIColor orangeColor] };   NSDictionary *attrDict5 = @{ NSBackgroundColorAttributeName: [UIColor redColor] };   NSDictionary *attrDict6 = @{ NSBackgroundColorAttributeName: [UIColor cyanColor] };      _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict4];   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict5];   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict6];   
    _label01.textColor = [UIColor greenColor];
    _label02.textColor = [UIColor yellowColor];
    _label03.textColor = [UIColor blueColor];
    
    //NSForegroundColorAttributeName 設置字體顏色,取值爲 UIColor,默認爲黑色
    
    NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };
    NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };
    NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };
    
    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
  
    
    //NSBackgroundColorAttributeName 設置字體所在區域背景的顏色,取值爲UIColor,默認值爲nil
    
    NSDictionary *attrDict4 = @{ NSBackgroundColorAttributeName: [UIColor orangeColor] };
    NSDictionary *attrDict5 = @{ NSBackgroundColorAttributeName: [UIColor redColor] };
    NSDictionary *attrDict6 = @{ NSBackgroundColorAttributeName: [UIColor cyanColor] };
    
    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict4];
    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict5];
    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict6];

運行結果: 


iOS之富文本6


    雖然 textColor 在 NSFontAttributeName 之前賦值,但是由於 NSFontAttributeName 的屬性效果被NSBackgroundColorAttributeName 屬性沖掉了,所以最終顯示了 textColor 的顏色。 


3. NSLigatureAttributeName 


[objc] view plain copy print ? 
//NSLigatureAttributeName 設置連體屬性,取值爲NSNumber 對象(整數),0 表示沒有連體字符,1 表示使用默認的連體字符,   //                        2 表示使用所有連體符號,默認值爲 1(iOS 不支持 2)      NSString *ligatureStr = @"flush";      NSDictionary *attrDict1 = @{ NSLigatureAttributeName: [NSNumber numberWithInt: 0],                                NSFontAttributeName: [UIFont fontWithName: @"futura" size: 30] };   _label01.attributedText = [[NSAttributedString alloc] initWithString: ligatureStr attributes: attrDict1];      NSDictionary *attrDict2 = @{ NSLigatureAttributeName: @(1),                                NSFontAttributeName: [UIFont fontWithName: @"futura" size: 30]                                 };   _label02.attributedText = [[NSAttributedString alloc] initWithString: ligatureStr attributes: attrDict2];   
    //NSLigatureAttributeName 設置連體屬性,取值爲NSNumber 對象(整數),0 表示沒有連體字符,1 表示使用默認的連體字符,
    //                        2 表示使用所有連體符號,默認值爲 1(iOS 不支持 2)
    
    NSString *ligatureStr = @"flush";
    
    NSDictionary *attrDict1 = @{ NSLigatureAttributeName: [NSNumber numberWithInt: 0],
                                 NSFontAttributeName: [UIFont fontWithName: @"futura" size: 30] };
    _label01.attributedText = [[NSAttributedString alloc] initWithString: ligatureStr attributes: attrDict1];
    
    NSDictionary *attrDict2 = @{ NSLigatureAttributeName: @(1),
                                 NSFontAttributeName: [UIFont fontWithName: @"futura" size: 30] 
                                 };
    _label02.attributedText = [[NSAttributedString alloc] initWithString: ligatureStr attributes: attrDict2];

    由於要展示連體字符,所以將前面使用的帶有中文的字符串換成 flush 


    NSLigatureAttributeName的取值爲NSNumber對象,所以不能直接將一個整數值賦給它,創建 NSNumber 對象的方法有很多,或者可以簡寫成 @(int) 


運行結果: 


iOS之富文本7


    注意觀察字母f和l之間的變化。 

    感覺連寫就是一個藝術字功能,當字符f和l組合使用組合符號(所謂的字形(glyph))繪製時,看起來確實更加美觀。但是並非所有的字符之間都有組合符號,事實上,只有某些字體中得某些字符的組合(如字符f和l,字符f和i等)才具有美觀的組合符號。 


4. NSKernAttributeName 


[objc] view plain copy print ? 
//NSKernAttributeName 設定字符間距,取值爲 NSNumber 對象(整數),正值間距加寬,負值間距變窄         NSDictionary *attrDict1 = @{ NSKernAttributeName: @(-3),                                NSFontAttributeName: [UIFont systemFontOfSize: 20]                                };   _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];         NSDictionary *attrDict2 = @{ NSKernAttributeName: @(0),                                NSFontAttributeName: [UIFont systemFontOfSize: 20]                                };   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];         NSDictionary *attrDict3 = @{ NSKernAttributeName: @(10),                                NSFontAttributeName: [UIFont systemFontOfSize: 20]                                };   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];   
    //NSKernAttributeName 設定字符間距,取值爲 NSNumber 對象(整數),正值間距加寬,負值間距變窄
    
    
    NSDictionary *attrDict1 = @{ NSKernAttributeName: @(-3),
                                 NSFontAttributeName: [UIFont systemFontOfSize: 20]
                                 };
    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
    
    
    NSDictionary *attrDict2 = @{ NSKernAttributeName: @(0),
                                 NSFontAttributeName: [UIFont systemFontOfSize: 20]
                                 };
    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
    
    
    NSDictionary *attrDict3 = @{ NSKernAttributeName: @(10),
                                 NSFontAttributeName: [UIFont systemFontOfSize: 20]
                                 };
    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];

運行結果: 


iOS之富文本8


5. NSStrikethroughStyleAttributeName 


[objc] view plain copy print ? 
//NSStrikethroughStyleAttributeName 設置刪除線,取值爲 NSNumber 對象(整數),枚舉常量 NSUnderlineStyle中的值   // NSUnderlineStyleNone   不設置刪除線   // NSUnderlineStyleSingle 設置刪除線爲細單實線   // NSUnderlineStyleThick  設置刪除線爲粗單實線   // NSUnderlineStyleDouble 設置刪除線爲細雙實線         NSDictionary *attrDict1 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle),                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];         NSDictionary *attrDict2 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleThick),                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];         NSDictionary *attrDict3 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleDouble),                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];   
    //NSStrikethroughStyleAttributeName 設置刪除線,取值爲 NSNumber 對象(整數),枚舉常量 NSUnderlineStyle中的值
    // NSUnderlineStyleNone   不設置刪除線
    // NSUnderlineStyleSingle 設置刪除線爲細單實線
    // NSUnderlineStyleThick  設置刪除線爲粗單實線
    // NSUnderlineStyleDouble 設置刪除線爲細雙實線
    
    
    NSDictionary *attrDict1 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle),
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
    
    
    NSDictionary *attrDict2 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleThick),
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
    
    
    NSDictionary *attrDict3 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleDouble),
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];

注意: 


    雖然使用了枚舉常量,但是枚舉常量的本質仍爲整數,所以同樣必須先轉化爲 NSNumber 才能使用 

    刪除線和下劃線使用相同的枚舉常量作爲其屬性值 

    目前iOS中只有上面列出的4中效果,雖然我們能夠在頭文件中發現其他更多的取值,但是使用後沒有任何效果 


運行結果: 


iOS之富文本9


可以看出,中文和英文刪除線的位置有所不同 


    另外,刪除線屬性取值除了上面的4種外,其實還可以取其他整數值,有興趣的可以自行試驗,取值爲 0 - 7時,效果爲單實線,隨着值得增加,單實線逐漸變粗,取值爲 9 - 15時,效果爲雙實線,取值越大,雙實線越粗。 


[objc] view plain copy print ? 
NSDictionary *attrDict1 = @{ NSStrikethroughStyleAttributeName: @(1),                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];         NSDictionary *attrDict2 = @{ NSStrikethroughStyleAttributeName: @(3),                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];         NSDictionary *attrDict3 = @{ NSStrikethroughStyleAttributeName: @(7),                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];   
    NSDictionary *attrDict1 = @{ NSStrikethroughStyleAttributeName: @(1),
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
    
    
    NSDictionary *attrDict2 = @{ NSStrikethroughStyleAttributeName: @(3),
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
    
    
    NSDictionary *attrDict3 = @{ NSStrikethroughStyleAttributeName: @(7),
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];

運行結果: 



iOS之富文本10


6. NSStrikethroughColorAttributeName 


[objc] view plain copy print ? 
//NSStrikethroughColorAttributeName 設置刪除線顏色,取值爲 UIColor 對象,默認值爲黑色      NSDictionary *attrDict1 = @{ NSStrikethroughColorAttributeName: [UIColor blueColor],                                NSStrikethroughStyleAttributeName: @(1),                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];         NSDictionary *attrDict2 = @{ NSStrikethroughColorAttributeName: [UIColor orangeColor],                                NSStrikethroughStyleAttributeName: @(3),                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];         NSDictionary *attrDict3 = @{ NSStrikethroughColorAttributeName: [UIColor greenColor],                                NSStrikethroughStyleAttributeName: @(7),                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];   
    //NSStrikethroughColorAttributeName 設置刪除線顏色,取值爲 UIColor 對象,默認值爲黑色
    
    NSDictionary *attrDict1 = @{ NSStrikethroughColorAttributeName: [UIColor blueColor],
                                 NSStrikethroughStyleAttributeName: @(1),
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
    
    
    NSDictionary *attrDict2 = @{ NSStrikethroughColorAttributeName: [UIColor orangeColor],
                                 NSStrikethroughStyleAttributeName: @(3),
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
    
    
    NSDictionary *attrDict3 = @{ NSStrikethroughColorAttributeName: [UIColor greenColor],
                                 NSStrikethroughStyleAttributeName: @(7),
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];


運行結果: 


iOS之富文本11


7. NSUnderlineStyleAttributeName 

     

下劃線除了線條位置和刪除線不同外,其他的都可以完全參照刪除線設置。 


[objc] view plain copy print ? 
//NSUnderlineStyleAttributeName 設置下劃線,取值爲 NSNumber 對象(整數),枚舉常量 NSUnderlineStyle中的值,與刪除線類似      NSDictionary *attrDict1 = @{ NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];         NSDictionary *attrDict2 = @{ NSUnderlineStyleAttributeName: @(NSUnderlineStyleThick),                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];         NSDictionary *attrDict3 = @{ NSUnderlineStyleAttributeName: @(NSUnderlineStyleDouble),                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];   
    //NSUnderlineStyleAttributeName 設置下劃線,取值爲 NSNumber 對象(整數),枚舉常量 NSUnderlineStyle中的值,與刪除線類似
    
    NSDictionary *attrDict1 = @{ NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
    
    
    NSDictionary *attrDict2 = @{ NSUnderlineStyleAttributeName: @(NSUnderlineStyleThick),
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
    
    
    NSDictionary *attrDict3 = @{ NSUnderlineStyleAttributeName: @(NSUnderlineStyleDouble),
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];


運行結果: 


iOS之富文本12


8. NSUnderlineColorAttributeName 


可以完全參照下劃線顏色設置 


[objc] view plain copy print ? 
//NSUnderlineColorAttributeName 設置下劃線顏色,取值爲 UIColor 對象,默認值爲黑色      NSDictionary *attrDict1 = @{ NSUnderlineColorAttributeName: [UIColor blueColor],                                NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];         NSDictionary *attrDict2 = @{ NSUnderlineColorAttributeName: [UIColor orangeColor],                                NSUnderlineStyleAttributeName: @(NSUnderlineStyleThick),                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];         NSDictionary *attrDict3 = @{ NSUnderlineColorAttributeName: [UIColor greenColor],                                NSUnderlineStyleAttributeName: @(NSUnderlineStyleDouble),                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];   
    //NSUnderlineColorAttributeName 設置下劃線顏色,取值爲 UIColor 對象,默認值爲黑色
    
    NSDictionary *attrDict1 = @{ NSUnderlineColorAttributeName: [UIColor blueColor],
                                 NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
    
    
    NSDictionary *attrDict2 = @{ NSUnderlineColorAttributeName: [UIColor orangeColor],
                                 NSUnderlineStyleAttributeName: @(NSUnderlineStyleThick),
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
    
    
    NSDictionary *attrDict3 = @{ NSUnderlineColorAttributeName: [UIColor greenColor],
                                 NSUnderlineStyleAttributeName: @(NSUnderlineStyleDouble),
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];


運行結果: 


iOS之富文本13


9. NSStrokeWidthAttributeName 


[objc] view plain copy print ? 
//NSStrokeWidthAttributeName 設置筆畫寬度,取值爲 NSNumber 對象(整數),負值填充效果,正值中空效果      NSDictionary *attrDict1 = @{ NSStrokeWidthAttributeName: @(-3),                                NSFontAttributeName: [UIFont systemFontOfSize:30] };   _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];         NSDictionary *attrDict2 = @{ NSStrokeWidthAttributeName: @(0),                                NSFontAttributeName: [UIFont systemFontOfSize:30] };   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];         NSDictionary *attrDict3 = @{ NSStrokeWidthAttributeName: @(3),                                NSFontAttributeName: [UIFont systemFontOfSize:30] };   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];   
    //NSStrokeWidthAttributeName 設置筆畫寬度,取值爲 NSNumber 對象(整數),負值填充效果,正值中空效果
    
    NSDictionary *attrDict1 = @{ NSStrokeWidthAttributeName: @(-3),
                                 NSFontAttributeName: [UIFont systemFontOfSize:30] };
    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
    
    
    NSDictionary *attrDict2 = @{ NSStrokeWidthAttributeName: @(0),
                                 NSFontAttributeName: [UIFont systemFontOfSize:30] };
    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
    
    
    NSDictionary *attrDict3 = @{ NSStrokeWidthAttributeName: @(3),
                                 NSFontAttributeName: [UIFont systemFontOfSize:30] };
    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];


運行結果: 


iOS之富文本14


10. NSStrokeColorAttributeName 


[objc] view plain copy print ? 
//NSStrokeColorAttributeName 填充部分顏色,不是字體顏色,取值爲 UIColor 對象      NSDictionary *attrDict1 = @{ NSStrokeWidthAttributeName: @(-3),                                NSStrokeColorAttributeName: [UIColor orangeColor],                                NSFontAttributeName: [UIFont systemFontOfSize:30] };   _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];         NSDictionary *attrDict2 = @{ NSStrokeWidthAttributeName: @(0),                                NSStrokeColorAttributeName: [UIColor blueColor],                                NSFontAttributeName: [UIFont systemFontOfSize:30] };   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];         NSDictionary *attrDict3 = @{ NSStrokeWidthAttributeName: @(3),                                NSStrokeColorAttributeName: [UIColor greenColor],                                NSFontAttributeName: [UIFont systemFontOfSize:30] };   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];   
    //NSStrokeColorAttributeName 填充部分顏色,不是字體顏色,取值爲 UIColor 對象
    
    NSDictionary *attrDict1 = @{ NSStrokeWidthAttributeName: @(-3),
                                 NSStrokeColorAttributeName: [UIColor orangeColor],
                                 NSFontAttributeName: [UIFont systemFontOfSize:30] };
    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
    
    
    NSDictionary *attrDict2 = @{ NSStrokeWidthAttributeName: @(0),
                                 NSStrokeColorAttributeName: [UIColor blueColor],
                                 NSFontAttributeName: [UIFont systemFontOfSize:30] };
    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
    
    
    NSDictionary *attrDict3 = @{ NSStrokeWidthAttributeName: @(3),
                                 NSStrokeColorAttributeName: [UIColor greenColor],
                                 NSFontAttributeName: [UIFont systemFontOfSize:30] };
    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];


運行結果: 


iOS之富文本15

我們並沒有設置字體的顏色,所以所有字體顏色應該是黑色,上圖清晰的表明了 StrokeColor 的作用範圍。 


11. NSShadowAttributeName 


[objc] view plain copy print ? 
//NSShadowAttributeName 設置陰影屬性,取值爲 NSShadow 對象      NSShadow *shadow1 = [[NSShadow alloc] init];  //NSShadow 對象比較簡單,只有3個屬性:陰影顏色,模糊半徑和偏移   shadow1.shadowOffset = CGSizeMake(3, 3);      //陰影偏移(X方向偏移和Y方向偏移)   shadow1.shadowBlurRadius = 0.5;               //模糊半徑   shadow1.shadowColor = [UIColor orangeColor];  //陰影顏色      NSShadow *shadow2 = [[NSShadow alloc] init];   shadow2.shadowOffset = CGSizeMake(3, 16);   shadow2.shadowBlurRadius = 2.5;   shadow2.shadowColor = [UIColor purpleColor];      NSShadow *shadow3 = [[NSShadow alloc] init];   shadow3.shadowOffset = CGSizeMake(16, 3);   shadow3.shadowBlurRadius = 4.0;   shadow3.shadowColor = [UIColor blueColor];      NSDictionary *attrDict1 = @{ NSShadowAttributeName: shadow1,                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];         NSDictionary *attrDict2 = @{ NSShadowAttributeName: shadow2,                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];      NSDictionary *attrDict3 = @{ NSShadowAttributeName: shadow3,                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];   
    //NSShadowAttributeName 設置陰影屬性,取值爲 NSShadow 對象
    
    NSShadow *shadow1 = [[NSShadow alloc] init];  //NSShadow 對象比較簡單,只有3個屬性:陰影顏色,模糊半徑和偏移
    shadow1.shadowOffset = CGSizeMake(3, 3);      //陰影偏移(X方向偏移和Y方向偏移)
    shadow1.shadowBlurRadius = 0.5;               //模糊半徑
    shadow1.shadowColor = [UIColor orangeColor];  //陰影顏色
    
    NSShadow *shadow2 = [[NSShadow alloc] init];
    shadow2.shadowOffset = CGSizeMake(3, 16);
    shadow2.shadowBlurRadius = 2.5;
    shadow2.shadowColor = [UIColor purpleColor];
    
    NSShadow *shadow3 = [[NSShadow alloc] init];
    shadow3.shadowOffset = CGSizeMake(16, 3);
    shadow3.shadowBlurRadius = 4.0;
    shadow3.shadowColor = [UIColor blueColor];
    
    NSDictionary *attrDict1 = @{ NSShadowAttributeName: shadow1,
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
    

    NSDictionary *attrDict2 = @{ NSShadowAttributeName: shadow2,
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
    
    NSDictionary *attrDict3 = @{ NSShadowAttributeName: shadow3,
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];

運行結果: 



iOS之富文本16


12. NSTextEffectAttributeName 


[objc] view plain copy print ? 
//NSTextEffectAttributeName 設置文本特殊效果,取值爲 NSString 對象,目前只有一個可用的特效:   //                          NSTextEffectLetterpressStyle(凸版印刷效果),適用於iOS 7.0及以上         NSDictionary *attrDict1 = @{ NSTextEffectAttributeName: NSTextEffectLetterpressStyle,                                NSForegroundColorAttributeName: [UIColor grayColor],                                NSFontAttributeName: [UIFont systemFontOfSize:30] };   _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];         NSDictionary *attrDict2 = @{ //NSTextEffectAttributeName: NSTextEffectLetterpressStyle,                                NSForegroundColorAttributeName: [UIColor grayColor],                                NSFontAttributeName: [UIFont systemFontOfSize:30] };   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];      NSDictionary *attrDict3 = @{ NSTextEffectAttributeName: NSTextEffectLetterpressStyle,                                NSForegroundColorAttributeName: [UIColor blueColor],                                NSFontAttributeName: [UIFont systemFontOfSize:30] };   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];   
    //NSTextEffectAttributeName 設置文本特殊效果,取值爲 NSString 對象,目前只有一個可用的特效:
    //                          NSTextEffectLetterpressStyle(凸版印刷效果),適用於iOS 7.0及以上
 
    
    NSDictionary *attrDict1 = @{ NSTextEffectAttributeName: NSTextEffectLetterpressStyle,
                                 NSForegroundColorAttributeName: [UIColor grayColor],
                                 NSFontAttributeName: [UIFont systemFontOfSize:30] };
    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
    
    
    NSDictionary *attrDict2 = @{ //NSTextEffectAttributeName: NSTextEffectLetterpressStyle,
                                 NSForegroundColorAttributeName: [UIColor grayColor],
                                 NSFontAttributeName: [UIFont systemFontOfSize:30] };
    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
    
    NSDictionary *attrDict3 = @{ NSTextEffectAttributeName: NSTextEffectLetterpressStyle,
                                 NSForegroundColorAttributeName: [UIColor blueColor],
                                 NSFontAttributeName: [UIFont systemFontOfSize:30] };
    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];


運行結果: 


iOS之富文本17

仔細對比label01和label02的文字顯示效果 


13. NSBaselineOffsetAttributeName 


[objc] view plain copy print ? 
//NSBaselineOffsetAttributeName 設置基線偏移值,取值爲 NSNumber (float),正值上偏,負值下偏      NSDictionary *attrDict1 = @{ NSBaselineOffsetAttributeName: @(-10),                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];         NSDictionary *attrDict2 = @{ NSBaselineOffsetAttributeName: @(0),                               NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];      NSDictionary *attrDict3 = @{ NSBaselineOffsetAttributeName: @(10),                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];   
    //NSBaselineOffsetAttributeName 設置基線偏移值,取值爲 NSNumber (float),正值上偏,負值下偏
    
    NSDictionary *attrDict1 = @{ NSBaselineOffsetAttributeName: @(-10),
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
    
    
    NSDictionary *attrDict2 = @{ NSBaselineOffsetAttributeName: @(0),
                                NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
    
    NSDictionary *attrDict3 = @{ NSBaselineOffsetAttributeName: @(10),
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];


運行結果: 


iOS之富文本18



14. NSObliquenessAttributeName 


[objc] view plain copy print ? 
//NSObliquenessAttributeName 設置字形傾斜度,取值爲 NSNumber (float),正值右傾,負值左傾      NSDictionary *attrDict1 = @{ NSObliquenessAttributeName: @(-0.5),                                NSFontAttributeName: [UIFont systemFontOfSize:30] };   _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];         NSDictionary *attrDict2 = @{ NSObliquenessAttributeName: @(0),                                NSFontAttributeName: [UIFont systemFontOfSize:30] };   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];      NSDictionary *attrDict3 = @{ NSObliquenessAttributeName: @(0.8),                                NSFontAttributeName: [UIFont systemFontOfSize:30] };   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];   
    //NSObliquenessAttributeName 設置字形傾斜度,取值爲 NSNumber (float),正值右傾,負值左傾
    
    NSDictionary *attrDict1 = @{ NSObliquenessAttributeName: @(-0.5),
                                 NSFontAttributeName: [UIFont systemFontOfSize:30] };
    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
    
    
    NSDictionary *attrDict2 = @{ NSObliquenessAttributeName: @(0),
                                 NSFontAttributeName: [UIFont systemFontOfSize:30] };
    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
    
    NSDictionary *attrDict3 = @{ NSObliquenessAttributeName: @(0.8),
                                 NSFontAttributeName: [UIFont systemFontOfSize:30] };
    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];


運行結果: 


iOS之富文本19


15. NSExpansionAttributeName 



[objc] view plain copy print ? 
//NSExpansionAttributeName 設置文本橫向拉伸屬性,取值爲 NSNumber (float),正值橫向拉伸文本,負值橫向壓縮文本      NSDictionary *attrDict1 = @{ NSExpansionAttributeName: @(-1),                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];         NSDictionary *attrDict2 = @{ NSExpansionAttributeName: @(0),                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];      NSDictionary *attrDict3 = @{ NSExpansionAttributeName: @(0.6),                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];   
    //NSExpansionAttributeName 設置文本橫向拉伸屬性,取值爲 NSNumber (float),正值橫向拉伸文本,負值橫向壓縮文本
    
    NSDictionary *attrDict1 = @{ NSExpansionAttributeName: @(-1),
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
    
    
    NSDictionary *attrDict2 = @{ NSExpansionAttributeName: @(0),
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
    
    NSDictionary *attrDict3 = @{ NSExpansionAttributeName: @(0.6),
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];



運行結果: 


iOS之富文本20


16. NSWritingDirectionAttributeName 


[objc] view plain copy print ? 
//NSWritingDirectionAttributeName 設置文字書寫方向,取值爲以下組合      //@[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionEmbedding)]   //@[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionOverride)]   //@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionEmbedding)]   //@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride)]      NSDictionary *attrDict1 = @{ NSWritingDirectionAttributeName: @[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionEmbedding)],                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];      NSDictionary *attrDict2 = @{ NSWritingDirectionAttributeName: @[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride)],                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];      NSDictionary *attrDict3 = @{ NSWritingDirectionAttributeName: @[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionOverride)],                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];   
    //NSWritingDirectionAttributeName 設置文字書寫方向,取值爲以下組合
    
    //@[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionEmbedding)]
    //@[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionOverride)]
    //@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionEmbedding)]
    //@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride)]
    
    NSDictionary *attrDict1 = @{ NSWritingDirectionAttributeName: @[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionEmbedding)],
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
    
    NSDictionary *attrDict2 = @{ NSWritingDirectionAttributeName: @[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride)],
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
    
    NSDictionary *attrDict3 = @{ NSWritingDirectionAttributeName: @[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionOverride)],
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];

運行結果: 



iOS之富文本21



一直沒搞明白  NSTextWritingDirectionEmbedding 和  NSTextWritingDirectionOverride 有什麼不同的效果。 

17. NSVerticalGlyphFormAttributeName 


[objc] view plain copy print ? 
//NSVerticalGlyphFormAttributeName 設置文字排版防線,取值爲 NSNumber 對象(整數),0 表示橫排文本,1 表示豎排文本。   //                                 在 iOS 中,總是使用橫排文本,0 以外的值都未定義      NSDictionary *attrDict1 = @{ NSVerticalGlyphFormAttributeName: @(-10),                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];         NSDictionary *attrDict2 = @{ NSVerticalGlyphFormAttributeName: @(0),                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];      NSDictionary *attrDict3 = @{ NSVerticalGlyphFormAttributeName: @(10),                                NSFontAttributeName: [UIFont systemFontOfSize:20] };   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];   
    //NSVerticalGlyphFormAttributeName 設置文字排版防線,取值爲 NSNumber 對象(整數),0 表示橫排文本,1 表示豎排文本。
    //                                 在 iOS 中,總是使用橫排文本,0 以外的值都未定義
    
    NSDictionary *attrDict1 = @{ NSVerticalGlyphFormAttributeName: @(-10),
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
    
    
    NSDictionary *attrDict2 = @{ NSVerticalGlyphFormAttributeName: @(0),
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
    
    NSDictionary *attrDict3 = @{ NSVerticalGlyphFormAttributeName: @(10),
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];



運行結果: 


iOS之富文本22

18. NSLinkAttributeName 


    鏈接屬性點擊將啓動瀏覽器打開一個URL地址,中間用到一個代理函數,UILabel 和 UITextField 無法使用該屬性,所以只能用UITextView來做示例。 



[objc] view plain copy print ? 
//NSLinkAttributeName 設置鏈接屬性,點擊後調用瀏覽器打開指定URL地址      NSDictionary *attrDict1 = @{ NSLinkAttributeName: [NSURL URLWithString: @"http://www.baidu.com"],                                NSFontAttributeName: [UIFont systemFontOfSize:20] };      _textview01.editable = NO;        //必須禁止輸入,否則點擊將彈出輸入鍵盤   _textview01.scrollEnabled = NO;   //可選   _textview01.delegate = self;      //必須設置,否則代理函數不會被回調      _textview01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];   
    //NSLinkAttributeName 設置鏈接屬性,點擊後調用瀏覽器打開指定URL地址
    
    NSDictionary *attrDict1 = @{ NSLinkAttributeName: [NSURL URLWithString: @"http://www.baidu.com"],
                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };

    _textview01.editable = NO;        //必須禁止輸入,否則點擊將彈出輸入鍵盤
    _textview01.scrollEnabled = NO;   //可選
    _textview01.delegate = self;      //必須設置,否則代理函數不會被回調

    _textview01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];

代理函數: 



[objc] view plain copy print ? 
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{       NSLog(@"textView is clicked...");       return YES;   }   
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
    NSLog(@"textView is clicked...");
    return YES;
}

運行結果: 



iOS之富文本23


點擊UITextView中得“Hello,中秋節!”,即可打開瀏覽器 


iOS之富文本24


19. NSAttachmentAttributeName 


[objc] view plain copy print ? 
//NSAttachmentAttributeName 設置文本附件,取值爲NSTextAttachment對象,常用於文字圖片混排      NSTextAttachment *textAttachment01 = [[NSTextAttachment alloc] init];   textAttachment01.image = [UIImage imageNamed: @"10000.jpeg"];  //設置圖片源   textAttachment01.bounds = CGRectMake(0, 0, 30, 30);          //設置圖片位置和大小   NSMutableAttributedString *attrStr01 = [[NSMutableAttributedString alloc] initWithString: originStr];      [attrStr01 addAttribute: NSFontAttributeName value: [UIFont systemFontOfSize: 25] range: NSMakeRange(0, originStr.length)];   NSAttributedString *attrStr11 = [NSAttributedString attributedStringWithAttachment: textAttachment01];      [attrStr01 insertAttributedString: attrStr11 atIndex: 2];  //NSTextAttachment佔用一個字符長度,插入後原字符串長度增加1      _label01.attributedText = attrStr01;         NSTextAttachment *textAttachment02 = [[NSTextAttachment alloc] init];   textAttachment02.image = [UIImage imageNamed: @"10000.jpeg"];  //設置圖片源   textAttachment02.bounds = CGRectMake(0, -10, 30, 40);          //設置圖片位置和大小   NSMutableAttributedString *attrStr02 = [[NSMutableAttributedString alloc] initWithString: originStr];      [attrStr02 addAttribute: NSFontAttributeName value: [UIFont systemFontOfSize: 25] range: NSMakeRange(0, originStr.length)];   NSAttributedString *attrStr12 = [NSAttributedString attributedStringWithAttachment: textAttachment02];      [attrStr02 insertAttributedString: attrStr12 atIndex: 6];      _label02.attributedText = attrStr02;      NSTextAttachment *textAttachment03 = [[NSTextAttachment alloc] init];   textAttachment03.image = [UIImage imageNamed: @"10000.jpeg"];  //設置圖片源   textAttachment03.bounds = CGRectMake(0, -6, 50, 30);          //設置圖片位置和大小   NSMutableAttributedString *attrStr03 = [[NSMutableAttributedString alloc] initWithString: originStr];      [attrStr03 addAttribute: NSFontAttributeName value: [UIFont systemFontOfSize: 25] range: NSMakeRange(0, originStr.length)];   NSAttributedString *attrStr13 = [NSAttributedString attributedStringWithAttachment: textAttachment03];      [attrStr03 insertAttributedString: attrStr13 atIndex: 8];      _label03.attributedText = attrStr03;   
    //NSAttachmentAttributeName 設置文本附件,取值爲NSTextAttachment對象,常用於文字圖片混排
    
    NSTextAttachment *textAttachment01 = [[NSTextAttachment alloc] init];
    textAttachment01.image = [UIImage imageNamed: @"10000.jpeg"];  //設置圖片源
    textAttachment01.bounds = CGRectMake(0, 0, 30, 30);          //設置圖片位置和大小
    NSMutableAttributedString *attrStr01 = [[NSMutableAttributedString alloc] initWithString: originStr];

    [attrStr01 addAttribute: NSFontAttributeName value: [UIFont systemFontOfSize: 25] range: NSMakeRange(0, originStr.length)];
    NSAttributedString *attrStr11 = [NSAttributedString attributedStringWithAttachment: textAttachment01];
    
    [attrStr01 insertAttributedString: attrStr11 atIndex: 2];  //NSTextAttachment佔用一個字符長度,插入後原字符串長度增加1
    
    _label01.attributedText = attrStr01;

    
    NSTextAttachment *textAttachment02 = [[NSTextAttachment alloc] init];
    textAttachment02.image = [UIImage imageNamed: @"10000.jpeg"];  //設置圖片源
    textAttachment02.bounds = CGRectMake(0, -10, 30, 40);          //設置圖片位置和大小
    NSMutableAttributedString *attrStr02 = [[NSMutableAttributedString alloc] initWithString: originStr];
    
    [attrStr02 addAttribute: NSFontAttributeName value: [UIFont systemFontOfSize: 25] range: NSMakeRange(0, originStr.length)];
    NSAttributedString *attrStr12 = [NSAttributedString attributedStringWithAttachment: textAttachment02];
    
    [attrStr02 insertAttributedString: attrStr12 atIndex: 6];
    
    _label02.attributedText = attrStr02;
    
    NSTextAttachment *textAttachment03 = [[NSTextAttachment alloc] init];
    textAttachment03.image = [UIImage imageNamed: @"10000.jpeg"];  //設置圖片源
    textAttachment03.bounds = CGRectMake(0, -6, 50, 30);          //設置圖片位置和大小
    NSMutableAttributedString *attrStr03 = [[NSMutableAttributedString alloc] initWithString: originStr];
    
    [attrStr03 addAttribute: NSFontAttributeName value: [UIFont systemFontOfSize: 25] range: NSMakeRange(0, originStr.length)];
    NSAttributedString *attrStr13 = [NSAttributedString attributedStringWithAttachment: textAttachment03];
    
    [attrStr03 insertAttributedString: attrStr13 atIndex: 8];
    
    _label03.attributedText = attrStr03;

運行結果: 


iOS之富文本25


20. NSParagraphStyleAttributeName 

設置文本段落排版格式,取值爲 NSParagraphStyle/NSMutableParagraphStyle 對象,可以設置如下屬性: 


[objc] view plain copy print ? 
// alignment               對齊方式,取值枚舉常量 NSTextAlignment   // firstLineHeadIndent     首行縮進,取值 float   // headIndent              縮進,取值 float   // tailIndent              尾部縮進,取值 float   // ineHeightMultiple       可變行高,乘因數,取值 float   // maximumLineHeight       最大行高,取值 float   // minimumLineHeight       最小行高,取值 float   // lineSpacing             行距,取值 float   // paragraphSpacing        段距,取值 float   // paragraphSpacingBefore  段首空間,取值 float   //       // baseWritingDirection    句子方向,取值枚舉常量 NSWritingDirection   // lineBreakMode           斷行方式,取值枚舉常量 NSLineBreakMode   // hyphenationFactor       連字符屬性,取值 0 - 1   
// alignment               對齊方式,取值枚舉常量 NSTextAlignment
// firstLineHeadIndent     首行縮進,取值 float
// headIndent              縮進,取值 float
// tailIndent              尾部縮進,取值 float
// ineHeightMultiple       可變行高,乘因數,取值 float
// maximumLineHeight       最大行高,取值 float
// minimumLineHeight       最小行高,取值 float
// lineSpacing             行距,取值 float
// paragraphSpacing        段距,取值 float
// paragraphSpacingBefore  段首空間,取值 float
//    
// baseWritingDirection    句子方向,取值枚舉常量 NSWritingDirection
// lineBreakMode           斷行方式,取值枚舉常量 NSLineBreakMode
// hyphenationFactor       連字符屬性,取值 0 - 1


下面逐一說明: 


<1>. alignment 



[objc] view plain copy print ? 
// alignment 對齊方式,取值枚舉常量 NSTextAlignment          //    enum {   //        NSTextAlignmentLeft      = 0,   //        NSTextAlignmentCenter    = 1,   //        NSTextAlignmentRight     = 2,   //        NSTextAlignmentJustified = 3,   //        NSTextAlignmentNatural   = 4,   //    };   //    typedef NSInteger NSTextAlignment;              _textview01.editable = NO;       _textview02.editable = NO;              _label11.text = @"alignment : NSTextAlignmentCenter";       _label12.text = @"alignment : NSTextAlignmentJustified";              NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;         // NSParagraphStyle       NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];       paraStyle01.alignment = NSTextAlignmentNatural;              NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                                     NSFontAttributeName: [UIFont systemFontOfSize: 12] };              _textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];                     NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];       paraStyle02.alignment = NSTextAlignmentJustified;              NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                                     NSFontAttributeName: [UIFont systemFontOfSize: 12] };       _textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];   
// alignment 對齊方式,取值枚舉常量 NSTextAlignment
    
//    enum {
//        NSTextAlignmentLeft      = 0,
//        NSTextAlignmentCenter    = 1,
//        NSTextAlignmentRight     = 2,
//        NSTextAlignmentJustified = 3,
//        NSTextAlignmentNatural   = 4,
//    };
//    typedef NSInteger NSTextAlignment;
    
    _textview01.editable = NO;
    _textview02.editable = NO;
    
    _label11.text = @"alignment : NSTextAlignmentCenter";
    _label12.text = @"alignment : NSTextAlignmentJustified";
    
    NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;

   // NSParagraphStyle
    NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];
    paraStyle01.alignment = NSTextAlignmentNatural;
    
    NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 12] };
    
    _textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];
    
    
    NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];
    paraStyle02.alignment = NSTextAlignmentJustified;
    
    NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 12] };
    _textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];



運行結果: 


iOS之富文本26  iOS之富文本27


<2>. firstLineHeadIndent 



[objc] view plain copy print ? 
//firstLineHeadIndent 首行縮進,取值 float      _textview01.editable = NO;   _textview02.editable = NO;      _label11.text = @"firstLineHeadIndent: 24";   _label12.text = @"firstLineHeadIndent: 48";      NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;      // NSParagraphStyle   NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];   paraStyle01.firstLineHeadIndent = 24;      NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                                 NSFontAttributeName: [UIFont systemFontOfSize: 12] };      _textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];         NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];   paraStyle02.firstLineHeadIndent = 48;      NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                                 NSFontAttributeName: [UIFont systemFontOfSize: 12] };   _textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];   
    //firstLineHeadIndent 首行縮進,取值 float
    
    _textview01.editable = NO;
    _textview02.editable = NO;
    
    _label11.text = @"firstLineHeadIndent: 24";
    _label12.text = @"firstLineHeadIndent: 48";
    
    NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;
    
    // NSParagraphStyle
    NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];
    paraStyle01.firstLineHeadIndent = 24;
    
    NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 12] };
    
    _textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];
    
    
    NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];
    paraStyle02.firstLineHeadIndent = 48;
    
    NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 12] };
    _textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];

運行結果: 


iOS之富文本28


<3>. headIndent 



[objc] view plain copy print ? 
//headIndent 除了首行之外的行縮進,取值 float      _textview01.editable = NO;   _textview02.editable = NO;      _label11.text = @"headIndent: 24";   _label12.text = @"headIndent: 48";      NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;      // NSParagraphStyle   NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];   paraStyle01.headIndent = 24;      NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                                 NSFontAttributeName: [UIFont systemFontOfSize: 12] };      _textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];         NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];   paraStyle02.headIndent = 48;      NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                                 NSFontAttributeName: [UIFont systemFontOfSize: 12] };   _textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];   
    //headIndent 除了首行之外的行縮進,取值 float
    
    _textview01.editable = NO;
    _textview02.editable = NO;

    _label11.text = @"headIndent: 24";
    _label12.text = @"headIndent: 48";
    
    NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;
    
    // NSParagraphStyle
    NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];
    paraStyle01.headIndent = 24;
    
    NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 12] };
    
    _textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];
    
    
    NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];
    paraStyle02.headIndent = 48;
    
    NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 12] };
    _textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];

運行結果: 


iOS之富文本29


<4>. tailIndent 



[objc] view plain copy print ? 
//tailIndent 行尾縮進,注意距離是從行首算起      _textview01.editable = NO;   _textview02.editable = NO;         _label11.text = @"tailIndent: 48";   _label12.text = @"tailIndent: 252";      NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;      // NSParagraphStyle   NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];   paraStyle01.tailIndent = 48;      NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                                 NSFontAttributeName: [UIFont systemFontOfSize: 12] };      _textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];         NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];   paraStyle02.tailIndent = 252;      NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                                 NSFontAttributeName: [UIFont systemFontOfSize: 12] };   _textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];   
    //tailIndent 行尾縮進,注意距離是從行首算起
    
    _textview01.editable = NO;
    _textview02.editable = NO;
    
    
    _label11.text = @"tailIndent: 48";
    _label12.text = @"tailIndent: 252";
    
    NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;
    
    // NSParagraphStyle
    NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];
    paraStyle01.tailIndent = 48;
    
    NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 12] };
    
    _textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];
    
    
    NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];
    paraStyle02.tailIndent = 252;
    
    NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 12] };
    _textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];

運行結果: 


iOS之富文本30


<5>. lineHeightMultiple 



[objc] view plain copy print ? 
//lineHeightMultiple 行高倍數因子,大於1行高變小,小於1行高變小,實際上字體大小不會改變,改變的時行間距      _textview01.editable = NO;   _textview02.editable = NO;      _label11.text = @"lineHeightMultiple: 0.6";   _label12.text = @"lineHeightMultiple: 2.5";      NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;      // NSParagraphStyle   NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];   paraStyle01.lineHeightMultiple = 0.6;      NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                                 NSFontAttributeName: [UIFont systemFontOfSize: 12] };      _textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];         NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];   paraStyle02.lineHeightMultiple = 2.5;      NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                                 NSFontAttributeName: [UIFont systemFontOfSize: 12] };   _textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];   
    //lineHeightMultiple 行高倍數因子,大於1行高變小,小於1行高變小,實際上字體大小不會改變,改變的時行間距
    
    _textview01.editable = NO;
    _textview02.editable = NO;
    
    _label11.text = @"lineHeightMultiple: 0.6";
    _label12.text = @"lineHeightMultiple: 2.5";
    
    NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;
    
    // NSParagraphStyle
    NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];
    paraStyle01.lineHeightMultiple = 0.6;
    
    NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 12] };
    
    _textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];
    
    
    NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];
    paraStyle02.lineHeightMultiple = 2.5;
    
    NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 12] };
    _textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];

運行結果: 


iOS之富文本31


<6>. maximumLineHeight 



[objc] view plain copy print ? 
//maximumLineHeight 最大行高,若其值小於默認行高,則行間距變小,若其值大於默認行高,則不會引起任何變化      _textview01.editable = NO;   _textview02.editable = NO;      _label11.text = @"maximumLineHeight: 7";   _label12.text = @"maximumLineHeight: 25";      NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;      // NSParagraphStyle   NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];   paraStyle01.maximumLineHeight = 7;      NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                                 NSFontAttributeName: [UIFont systemFontOfSize: 12] };      _textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];         NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];   paraStyle02.maximumLineHeight = 25;      NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                                 NSFontAttributeName: [UIFont systemFontOfSize: 12] };   _textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];   
    //maximumLineHeight 最大行高,若其值小於默認行高,則行間距變小,若其值大於默認行高,則不會引起任何變化
    
    _textview01.editable = NO;
    _textview02.editable = NO;
    
    _label11.text = @"maximumLineHeight: 7";
    _label12.text = @"maximumLineHeight: 25";
    
    NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;
    
    // NSParagraphStyle
    NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];
    paraStyle01.maximumLineHeight = 7;
    
    NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 12] };
    
    _textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];
    
    
    NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];
    paraStyle02.maximumLineHeight = 25;
    
    NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 12] };
    _textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];

運行結果: 


iOS之富文本32


<7>. minimumLineHeight 



[objc] view plain copy print ? 
//minimumLineHeight 最小行高,若其值大於默認行高,則行間距變大,若其值小於默認行高,則不會引起任何變化      _textview01.editable = NO;   _textview02.editable = NO;         _label11.text = @"minimumLineHeight: 0.6";   _label12.text = @"minimumLineHeight: 25";      NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;      // NSParagraphStyle   NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];   paraStyle01.minimumLineHeight = 0.6;      NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                                 NSFontAttributeName: [UIFont systemFontOfSize: 12] };      _textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];         NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];   paraStyle02.minimumLineHeight = 25;      NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                                 NSFontAttributeName: [UIFont systemFontOfSize: 12] };   _textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];   
    //minimumLineHeight 最小行高,若其值大於默認行高,則行間距變大,若其值小於默認行高,則不會引起任何變化
    
    _textview01.editable = NO;
    _textview02.editable = NO;
    
    
    _label11.text = @"minimumLineHeight: 0.6";
    _label12.text = @"minimumLineHeight: 25";
    
    NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;
    
    // NSParagraphStyle
    NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];
    paraStyle01.minimumLineHeight = 0.6;
    
    NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 12] };
    
    _textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];
    
    
    NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];
    paraStyle02.minimumLineHeight = 25;
    
    NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 12] };
    _textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];

運行結果: 


iOS之富文本33


<8>. lineSpacing 



[objc] view plain copy print ? 
//lineSpacing 行距,取值爲 float,可正可負,正值增加行距,負值減小行距      _textview01.editable = NO;   _textview02.editable = NO;      _label11.text = @"lineSpacing: -7";   _label12.text = @"lineSpacing: 25";      NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;      // NSParagraphStyle   NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];   paraStyle01.lineSpacing = -7;      NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                                 NSFontAttributeName: [UIFont systemFontOfSize: 12] };      _textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];         NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];   paraStyle02.lineSpacing = 25;      NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                                 NSFontAttributeName: [UIFont systemFontOfSize: 12] };   _textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];   
    //lineSpacing 行距,取值爲 float,可正可負,正值增加行距,負值減小行距
    
    _textview01.editable = NO;
    _textview02.editable = NO;
    
    _label11.text = @"lineSpacing: -7";
    _label12.text = @"lineSpacing: 25";
    
    NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;
    
    // NSParagraphStyle
    NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];
    paraStyle01.lineSpacing = -7;
    
    NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 12] };
    
    _textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];
    
    
    NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];
    paraStyle02.lineSpacing = 25;
    
    NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 12] };
    _textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];

運行結果: 


iOS之富文本34


<9>. paragraphSpacing 



[objc] view plain copy print ? 
//paragraphSpacing 段距,取值 float, 負值無效,取0值      _textview01.editable = NO;   _textview02.editable = NO;      _label11.text = @"paragraphSpacing: -7";   _label12.text = @"paragraphSpacing: 25";      NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;      // NSParagraphStyle   NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];   paraStyle01.paragraphSpacing = -7;      NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                                 NSFontAttributeName: [UIFont systemFontOfSize: 12] };      _textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];         NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];   paraStyle02.paragraphSpacing = 25;      NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                                 NSFontAttributeName: [UIFont systemFontOfSize: 12] };   _textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];   
    //paragraphSpacing 段距,取值 float, 負值無效,取0值
    
    _textview01.editable = NO;
    _textview02.editable = NO;
    
    _label11.text = @"paragraphSpacing: -7";
    _label12.text = @"paragraphSpacing: 25";
    
    NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;
    
    // NSParagraphStyle
    NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];
    paraStyle01.paragraphSpacing = -7;
    
    NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 12] };
    
    _textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];
    
    
    NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];
    paraStyle02.paragraphSpacing = 25;
    
    NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 12] };
    _textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];

運行結果: 


iOS之富文本35


<10>. paragraphSpacingBefore 



[objc] view plain copy print ? 
//paragraphSpacingBefore 段首距離,取值 float , 最小取值爲0      _textview01.editable = NO;   _textview02.editable = NO;      _label11.text = @"paragraphSpacingBefore: -7";   _label12.text = @"paragraphSpacingBefore: 25";      NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;      // NSParagraphStyle   NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];   paraStyle01.paragraphSpacingBefore = -7;      NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                                 NSFontAttributeName: [UIFont systemFontOfSize: 12] };      _textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];         NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];   paraStyle02.paragraphSpacingBefore = 25;      NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                                 NSFontAttributeName: [UIFont systemFontOfSize: 12] };   _textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];   
    //paragraphSpacingBefore 段首距離,取值 float , 最小取值爲0
    
    _textview01.editable = NO;
    _textview02.editable = NO;
    
    _label11.text = @"paragraphSpacingBefore: -7";
    _label12.text = @"paragraphSpacingBefore: 25";
    
    NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;
    
    // NSParagraphStyle
    NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];
    paraStyle01.paragraphSpacingBefore = -7;
    
    NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 12] };
    
    _textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];
    
    
    NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];
    paraStyle02.paragraphSpacingBefore = 25;
    
    NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 12] };
    _textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];

運行結果: 


iOS之富文本36


<11>. baseWritingDirection 



[objc] view plain copy print ? 
//baseWritingDirection  //句子排版方向,取值爲枚舉常量 NSWritingDirection       //    enum {   //        NSWritingDirectionNatural = -1,   //        NSWritingDirectionLeftToRight =  0,   //        NSWritingDirectionRightToLeft =  1   //    };   //    typedef NSInteger NSWritingDirection;      _textview01.editable = NO;   _textview02.editable = NO;      _label11.text = @"baseWritingDirection: NSWritingDirectionLeftToRight";   _label12.text = @"baseWritingDirection: NSWritingDirectionRightToLeft";      NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;      // NSParagraphStyle   NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];   paraStyle01.baseWritingDirection = NSWritingDirectionLeftToRight;      NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                                 NSFontAttributeName: [UIFont systemFontOfSize: 12] };      _textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];         NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];   paraStyle02.baseWritingDirection = NSWritingDirectionRightToLeft;      NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                                 NSFontAttributeName: [UIFont systemFontOfSize: 12] };   _textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];   
    //baseWritingDirection  //句子排版方向,取值爲枚舉常量 NSWritingDirection    
    //    enum {
    //        NSWritingDirectionNatural = -1,
    //        NSWritingDirectionLeftToRight =  0,
    //        NSWritingDirectionRightToLeft =  1
    //    };
    //    typedef NSInteger NSWritingDirection;
 
    _textview01.editable = NO;
    _textview02.editable = NO;
    
    _label11.text = @"baseWritingDirection: NSWritingDirectionLeftToRight";
    _label12.text = @"baseWritingDirection: NSWritingDirectionRightToLeft";
    
    NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;
    
    // NSParagraphStyle
    NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];
    paraStyle01.baseWritingDirection = NSWritingDirectionLeftToRight;
    
    NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 12] };
    
    _textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];
    
    
    NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];
    paraStyle02.baseWritingDirection = NSWritingDirectionRightToLeft;
    
    NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 12] };
    _textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];

運行結果: 


iOS之富文本37


<12>. lineBreakMode 


[objc] view plain copy print ? 
//lineBreakMode 斷行方式,取值枚舉常量 NSLineBreakMode      //    enum {   //        NSLineBreakByWordWrapping = 0, //自動換行,單詞切斷   //        NSLineBreakByCharWrapping,     //自動換行,字母切斷   //        NSLineBreakByClipping,         //非自動換行,不切斷   //        NSLineBreakByTruncatingHead,   //非自動換行,行首切斷   //        NSLineBreakByTruncatingTail,   //非自動換行,行尾切斷   //        NSLineBreakByTruncatingMiddle  //非自動換行,中間切斷   //    };   //    typedef NSUInteger NSLineBreakMode;      _textview01.editable = NO;   _textview02.editable = NO;      _label11.text = @"lineBreakMode: NSLineBreakByTruncatingHead";   _label12.text = @"lineBreakMode: NSLineBreakByTruncatingTail";         NSString *strstr = @"These two documents provide the perfect starting point for iOS and Mac app development. Follow either road map to learn how to get and use Xcode to create your first app. You will learn how to use Xcode to test and debug your source code, analyze to improve your app’s performance, perform source control operations, archive your app, and submit your app to the App Store.";      NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;      // NSParagraphStyle   NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];   paraStyle01.lineBreakMode = NSLineBreakByTruncatingHead;      NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                                 NSFontAttributeName: [UIFont systemFontOfSize: 12] };      _textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];         NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];   paraStyle02.lineBreakMode = NSLineBreakByTruncatingTail;      NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                                 NSFontAttributeName: [UIFont systemFontOfSize: 12] };   _textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];   
    //lineBreakMode 斷行方式,取值枚舉常量 NSLineBreakMode
    
    //    enum {
    //        NSLineBreakByWordWrapping = 0, //自動換行,單詞切斷
    //        NSLineBreakByCharWrapping,     //自動換行,字母切斷
    //        NSLineBreakByClipping,         //非自動換行,不切斷
    //        NSLineBreakByTruncatingHead,   //非自動換行,行首切斷
    //        NSLineBreakByTruncatingTail,   //非自動換行,行尾切斷
    //        NSLineBreakByTruncatingMiddle  //非自動換行,中間切斷
    //    };
    //    typedef NSUInteger NSLineBreakMode;
    
    _textview01.editable = NO;
    _textview02.editable = NO;
    
    _label11.text = @"lineBreakMode: NSLineBreakByTruncatingHead";
    _label12.text = @"lineBreakMode: NSLineBreakByTruncatingTail";
    
    
    NSString *strstr = @"These two documents provide the perfect starting point for iOS and Mac app development. Follow either road map to learn how to get and use Xcode to create your first app. You will learn how to use Xcode to test and debug your source code, analyze to improve your app’s performance, perform source control operations, archive your app, and submit your app to the App Store.";
    
    NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;
    
    // NSParagraphStyle
    NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];
    paraStyle01.lineBreakMode = NSLineBreakByTruncatingHead;
    
    NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 12] };
    
    _textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];
    
    
    NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];
    paraStyle02.lineBreakMode = NSLineBreakByTruncatingTail;
    
    NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 12] };
    _textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];

運行結果: 


iOS之富文本38 iOS之富文本39 iOS之富文本40



<13>. hyphenationFactor 




[objc] view plain copy print ? 
//hyphenationFactor 連字符屬性,取值 0 到 1 之間,開啓斷詞功能      NSString *strstr = @"These two documents provide the perfect starting point for iOS and Mac app development. Follow either road map to learn how to get and use Xcode to create your first app. You will learn how to use Xcode to test and debug your source code, analyze to improve your app’s performance, perform source control operations, archive your app, and submit your app to the App Store.";      _textview01.editable = NO;   _textview02.editable = NO;      _label11.text = @"hyphenationFactor: 0.3";   _label12.text = @"hyphenationFactor: 0.9";      // NSParagraphStyle   NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];   paraStyle01.hyphenationFactor = 0.3;      NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                                 NSFontAttributeName: [UIFont systemFontOfSize: 15] };      _textview01.attributedText = [[NSAttributedString alloc] initWithString: strstr  attributes: attrDict01];         NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];   paraStyle02.hyphenationFactor = 0.9;      NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                                 NSFontAttributeName: [UIFont systemFontOfSize: 15] };   _textview02.attributedText = [[NSAttributedString alloc] initWithString: strstr  attributes: attrDict02];   
    //hyphenationFactor 連字符屬性,取值 0 到 1 之間,開啓斷詞功能
 
    NSString *strstr = @"These two documents provide the perfect starting point for iOS and Mac app development. Follow either road map to learn how to get and use Xcode to create your first app. You will learn how to use Xcode to test and debug your source code, analyze to improve your app’s performance, perform source control operations, archive your app, and submit your app to the App Store.";
    
    _textview01.editable = NO;
    _textview02.editable = NO;
    
    _label11.text = @"hyphenationFactor: 0.3";
    _label12.text = @"hyphenationFactor: 0.9";
    
    // NSParagraphStyle
    NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];
    paraStyle01.hyphenationFactor = 0.3;
    
    NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 15] };
    
    _textview01.attributedText = [[NSAttributedString alloc] initWithString: strstr  attributes: attrDict01];
    
    
    NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];
    paraStyle02.hyphenationFactor = 0.9;
    
    NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 15] };
    _textview02.attributedText = [[NSAttributedString alloc] initWithString: strstr  attributes: attrDict02];

運行結果: 


iOS之富文本41


最後,補充一點字符(Character)和字形(Glyphs)的基礎知識:



    排版系統中文本顯示的一個重要的過程就是字符到字形的轉換,字符是信息本身的元素,而字形是字符的圖形表徵,字符還會有其它表徵比如發音。 字符在計算機中其實就是一個編碼,某個字符集中的編碼,比如Unicode字符集,就囊括了大都數存在的字符。 而字形則是圖形,一般都存儲在字體文件中,字形也有它的編碼,也就是它在字體中的索引。 一個字符可以對應多個字形(不同的字體,或者同種字體的不同樣式:粗體斜體等);多個字符也可能對應一個字形,比如字符的連寫( Ligatures)。  



iOS之富文本42
Roman Ligatures 



下面就來詳情看看字形的各個參數也就是所謂的字形度量Glyph Metrics 



iOS之富文本43iOS之富文本44



bounding box(邊界框 bbox): 

    一個假想的框子,它儘可能緊密的裝入字形。 

baseline(基線):

    一條假想的線,一行上的字形都以此線作爲上下位置的參考,在這條線的左側存在一個點叫做基線的原點, 

ascent(上行高度): 

    從原點到字體中最高(這裏的高深都是以基線爲參照線的)的字形的頂部的距離,ascent是一個正值 

descent(下行高度): 

    從原點到字體中最深的字形底部的距離,descent是一個負值(比如一個字體原點到最深的字形的底部的距離爲2,那麼descent就爲-2) 

linegap(行距):

    可以稱作leading(其實準確點講應該叫做External leading),行高lineHeight則可以通過 ascent + |descent| + linegap 來計算。 

    一些Metrics專業知識還可以參考Free Type的文檔 Glyph metrics,其實iOS就是使用Free Type庫來進行字體渲染的。 

以上圖片和部分概念來自蘋果文檔 Querying Font Metrics ,Text Layout。 


大功終於告成!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章