iOS之富文本

  

以前作項目時遇到一個問題:html

       使用UITextView顯示一段電影的簡介,因爲字數比較多,因此字體設置的很小,行間距和段間距也很小,一大段文字擠在一塊兒看起來很彆扭,想要把行間距調大,結果在XCode中查遍其全部屬性才發現,UITextView竟然沒有調整行間距的接口,因而忍住不內心抱怨了一下下。瀏覽器

       可是問題仍是要解決的,上網一查才發現,iOS不只有富文本處理的功能,並且對於文字排版的處理能力那是至關的強大,看來我是孤陋寡聞了。app


正題開始以前插播一點基礎知識:框架


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


       簡單來講,是經過控件來完成的,而這些控件都封裝在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,該方法適合於文本較少而又須要分段精細控制的狀況。

    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個字符,即第五、六、7字符設置爲紅色
    [attributedStr01 addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(4, 3)];
    
    //賦值給顯示控件label01的 attributedText
    _label01.attributedText = attributedStr01;

 運行結果:

方式二:

      首先建立屬性字典,初始化各類屬性,而後和須要控制的文本一塊兒建立並賦值給控件的AttributedText,該方法適合於須要控制的文本較多總體控制的狀況,一般是從文件中讀取的大段文本控制。

    //方式二
    
    //建立屬性字典
    NSDictionary *attrDict = @{ NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 15],
                                NSForegroundColorAttributeName: [UIColor blueColor] };

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

運行結果:

   

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

    //方式二的分段處理
    //第一段
    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;

運行結果:

 

 

好了,講完AttributedString的建立方式,下面研究下AttributedString究竟能夠設置哪些屬性,具體來講,有如下21個:

// 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

    //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設置的顏色與UILabel的textColor屬性設置的顏色在地位上是相等的,誰最後賦值,最終顯示的就是誰的顏色。

 

2. NSBackgroundColorAttributeName

    //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 屬性,可是在運行結果中,全部字體的顏色都變成了默認色——黑色,這說明 NSForegroundColorAttributeName 和 NSBackgroundColorAttributeName 的低位是相等的,跟前面介紹的 textColor 同樣,哪一個屬性最後一次賦值,就會沖掉前面的效果,如果咱們把屬性代碼順序交換一下

    //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];

運行結果:

可是textColor屬性能夠與 NSBackgroundColorAttributeName 屬性疊加

    _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];

運行結果:

       雖然 textColor 在 NSFontAttributeName 以前賦值,可是因爲 NSFontAttributeName 的屬性效果被NSBackgroundColorAttributeName 屬性沖掉了,因此最終顯示了 textColor 的顏色。

 

3. NSLigatureAttributeName

    //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)

運行結果:

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

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

 

4. NSKernAttributeName

  //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];

運行結果:

 

5. NSStrikethroughStyleAttributeName

    //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中效果,雖然咱們可以在頭文件中發現其餘更多的取值,可是使用後沒有任何效果

 

運行結果:

能夠看出,中文和英文刪除線的位置有所不一樣

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

    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];

運行結果:

 

6. NSStrikethroughColorAttributeName

  //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];

運行結果:

 

7. NSUnderlineStyleAttributeName

    下劃線除了線條位置和刪除線不一樣外,其餘的均可以徹底參照刪除線設置。

    //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];

運行結果:

 

8. NSUnderlineColorAttributeName

    能夠徹底參照下劃線顏色設置

 //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];

運行結果:

 

9. NSStrokeWidthAttributeName

  //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];

運行結果:

 

10. NSStrokeColorAttributeName

 //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];

運行結果:



咱們並無設置字體的顏色,因此全部字體顏色應該是黑色,上圖清晰的代表了 StrokeColor 的做用範圍。

 

11. NSShadowAttributeName

 //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];

運行結果:

 

12. NSTextEffectAttributeName

 //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];

運行結果:

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

 

13. NSBaselineOffsetAttributeName

//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];

運行結果:

 

14. NSObliquenessAttributeName 

//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];

運行結果:

 

15. NSExpansionAttributeName

   //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];

運行結果:

 

16. NSWritingDirectionAttributeName

    //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];

運行結果:

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

 

17. NSVerticalGlyphFormAttributeName

    //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];

運行結果:



18. NSLinkAttributeName

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

 //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];

代理函數:

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
    NSLog(@"textView is clicked...");
    return YES;
}

運行結果:

點擊UITextView中得「Hello,中秋節!」,便可打開瀏覽器

 

19. NSAttachmentAttributeName

   //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;

運行結果:

 

20. NSParagraphStyleAttributeName

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

// 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

// 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];

運行結果:

  

 

<2>. firstLineHeadIndent

 //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];

運行結果:

 

<3>. headIndent

   //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];

運行結果:

 

<4>. tailIndent

   //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];

運行結果:

 

<5>. lineHeightMultiple

 //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];

運行結果:

 

<6>. maximumLineHeight

   //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];

運行結果:

 

<7>. minimumLineHeight

    //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];

運行結果:

 

<8>. lineSpacing

    //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];

運行結果:

 

<9>. paragraphSpacing

   //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];

運行結果:

 

<10>. paragraphSpacingBefore

    //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];

運行結果:

 

<11>. baseWritingDirection

    //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];

運行結果:

 

<12>. lineBreakMode

    //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];

運行結果:

  

 

<13>. hyphenationFactor

   //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];

運行結果:

 

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

 

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

 

Roman Ligatures

 

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

 

 

 

 

 

 

 

 

 

 

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

 

大功終於告成!

相關文章
相關標籤/搜索