你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS 富文本 ,設置行間距、字間距,計算高度(轉載組合而成)

IOS 富文本 ,設置行間距、字間距,計算高度(轉載組合而成)

編輯:IOS開發綜合

一 計算高度

-(CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(UIFont*)font withWidth:(CGFloat)width {

NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];

paraStyle.lineBreakMode = NSLineBreakByCharWrapping;

paraStyle.alignment = NSTextAlignmentLeft;

/** 行高 */

paraStyle.lineSpacing = 15;

paraStyle.hyphenationFactor = 1.0;

paraStyle.firstLineHeadIndent = 0.0;

paraStyle.paragraphSpacingBefore = 0.0;

paraStyle.headIndent = 0;

paraStyle.tailIndent = 0;

NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f

};

CGSize size = [str boundingRectWithSize:CGSizeMake(width,height) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;

return size.height;

}

二 label富文本

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

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

方式一:

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

 

 

[objc] view plain copy print ?   NSString*originStr=@"Hello,中秋節!"; //方式一 //創建NSMutableAttributedString NSMutableAttributedString*attributedStr01=[[NSMutableAttributedStringalloc]initWithString:originStr]; //添加屬性 //給所有字符設置字體為Zapfino,字體高度為15像素 [attributedStr01addAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"Zapfino"size:15] range:NSMakeRange(0,originStr.length)]; //分段控制,最開始4個字符顏色設置成藍色 [attributedStr01addAttribute:NSForegroundColorAttributeNamevalue:[UIColorblueColor]range:NSMakeRange(0,4)]; //分段控制,第5個字符開始的3個字符,即第5、6、7字符設置為紅色 [attributedStr01addAttribute:NSForegroundColorAttributeNamevalue:[UIColorredColor]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:[UIFontfontWithName:@"Zapfino"size:15], NSForegroundColorAttributeName:[UIColorblueColor]}; //創建NSAttributedString並賦值 _label02.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes: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:[UIFontfontWithName:@"Zapfino"size:15], NSForegroundColorAttributeName:[UIColorblueColor]}; NSAttributedString*attrStr1=[[NSAttributedStringalloc]initWithString:[originStrsubstringWithRange:NSMakeRange(0,4)]attributes:attrDict1]; //第二段 NSDictionary*attrDict2=@{NSFontAttributeName:[UIFontfontWithName:@"Zapfino"size:15], NSForegroundColorAttributeName:[UIColorredColor]}; NSAttributedString*attrStr2=[[NSAttributedStringalloc]initWithString:[originStrsubstringWithRange:NSMakeRange(4,3)]attributes:attrDict2]; //第三段 NSDictionary*attrDict3=@{NSFontAttributeName:[UIFontfontWithName:@"Zapfino"size:15], NSForegroundColorAttributeName:[UIColorblackColor]}; NSAttributedString*attrStr3=[[NSAttributedStringalloc]initWithString:[originStrsubstringWithRange: NSMakeRange(7,originStr.length-4-3)]attributes:attrDict3]; //合並 NSMutableAttributedString*attributedStr03=[[NSMutableAttributedStringalloc]initWithAttributedString:attrStr1]; [attributedStr03appendAttributedString:attrStr2]; [attributedStr03appendAttributedString: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:[UIColorredColor]}; NSDictionary*attrDict2=@{NSForegroundColorAttributeName:[UIColorblueColor]}; NSDictionary*attrDict3=@{NSForegroundColorAttributeName:[UIColororangeColor]}; _label01.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict1]; _label02.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict2]; _label03.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes: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:[UIColorredColor]}; NSDictionary*attrDict2=@{NSForegroundColorAttributeName:[UIColorblueColor]}; NSDictionary*attrDict3=@{NSForegroundColorAttributeName:[UIColororangeColor]}; _label01.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict1]; _label02.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict2]; _label03.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict3]; //NSBackgroundColorAttributeName設置字體所在區域背景的顏色,取值為UIColor,默認值為nil NSDictionary*attrDict4=@{NSBackgroundColorAttributeName:[UIColororangeColor]}; NSDictionary*attrDict5=@{NSBackgroundColorAttributeName:[UIColorredColor]}; NSDictionary*attrDict6=@{NSBackgroundColorAttributeName:[UIColorcyanColor]}; _label01.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict4]; _label02.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict5]; _label03.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes: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:[UIColororangeColor]}; NSDictionary*attrDict5=@{NSBackgroundColorAttributeName:[UIColorredColor]}; NSDictionary*attrDict6=@{NSBackgroundColorAttributeName:[UIColorcyanColor]}; _label01.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict4]; _label02.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict5]; _label03.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict6]; //NSForegroundColorAttributeName設置字體顏色,取值為UIColor,默認為黑色 NSDictionary*attrDict1=@{NSForegroundColorAttributeName:[UIColorredColor]}; NSDictionary*attrDict2=@{NSForegroundColorAttributeName:[UIColorblueColor]}; NSDictionary*attrDict3=@{NSForegroundColorAttributeName:[UIColororangeColor]}; _label01.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict1]; _label02.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict2]; _label03.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes: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=[UIColorgreenColor]; _label02.textColor=[UIColoryellowColor]; _label03.textColor=[UIColorblueColor]; //NSForegroundColorAttributeName設置字體顏色,取值為UIColor,默認為黑色 NSDictionary*attrDict1=@{NSForegroundColorAttributeName:[UIColorredColor]}; NSDictionary*attrDict2=@{NSForegroundColorAttributeName:[UIColorblueColor]}; NSDictionary*attrDict3=@{NSForegroundColorAttributeName:[UIColororangeColor]}; _label01.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict1]; _label02.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict2]; _label03.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict3]; //NSBackgroundColorAttributeName設置字體所在區域背景的顏色,取值為UIColor,默認值為nil NSDictionary*attrDict4=@{NSBackgroundColorAttributeName:[UIColororangeColor]}; NSDictionary*attrDict5=@{NSBackgroundColorAttributeName:[UIColorredColor]}; NSDictionary*attrDict6=@{NSBackgroundColorAttributeName:[UIColorcyanColor]}; _label01.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict4]; _label02.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict5]; _label03.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes: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:[NSNumbernumberWithInt:0], NSFontAttributeName:[UIFontfontWithName:@"futura"size:30]}; _label01.attributedText=[[NSAttributedStringalloc]initWithString:ligatureStrattributes:attrDict1]; NSDictionary*attrDict2=@{NSLigatureAttributeName:@(1), NSFontAttributeName:[UIFontfontWithName:@"futura"size:30] }; _label02.attributedText=[[NSAttributedStringalloc]initWithString:ligatureStrattributes: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:[UIFontsystemFontOfSize:20] }; _label01.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict1]; NSDictionary*attrDict2=@{NSKernAttributeName:@(0), NSFontAttributeName:[UIFontsystemFontOfSize:20] }; _label02.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict2]; NSDictionary*attrDict3=@{NSKernAttributeName:@(10), NSFontAttributeName:[UIFontsystemFontOfSize:20] }; _label03.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes: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:[UIFontsystemFontOfSize:20]}; _label01.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict1]; NSDictionary*attrDict2=@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleThick), NSFontAttributeName:[UIFontsystemFontOfSize:20]}; _label02.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict2]; NSDictionary*attrDict3=@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleDouble), NSFontAttributeName:[UIFontsystemFontOfSize:20]}; _label03.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes: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:[UIFontsystemFontOfSize:20]}; _label01.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict1]; NSDictionary*attrDict2=@{NSStrikethroughStyleAttributeName:@(3), NSFontAttributeName:[UIFontsystemFontOfSize:20]}; _label02.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict2]; NSDictionary*attrDict3=@{NSStrikethroughStyleAttributeName:@(7), NSFontAttributeName:[UIFontsystemFontOfSize:20]}; _label03.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes: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:[UIColorblueColor], NSStrikethroughStyleAttributeName:@(1), NSFontAttributeName:[UIFontsystemFontOfSize:20]}; _label01.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict1]; NSDictionary*attrDict2=@{NSStrikethroughColorAttributeName:[UIColororangeColor], NSStrikethroughStyleAttributeName:@(3), NSFontAttributeName:[UIFontsystemFontOfSize:20]}; _label02.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict2]; NSDictionary*attrDict3=@{NSStrikethroughColorAttributeName:[UIColorgreenColor], NSStrikethroughStyleAttributeName:@(7), NSFontAttributeName:[UIFontsystemFontOfSize:20]}; _label03.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes: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:[UIFontsystemFontOfSize:20]}; _label01.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict1]; NSDictionary*attrDict2=@{NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick), NSFontAttributeName:[UIFontsystemFontOfSize:20]}; _label02.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict2]; NSDictionary*attrDict3=@{NSUnderlineStyleAttributeName:@(NSUnderlineStyleDouble), NSFontAttributeName:[UIFontsystemFontOfSize:20]}; _label03.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes: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:[UIColorblueColor], NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle), NSFontAttributeName:[UIFontsystemFontOfSize:20]}; _label01.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict1]; NSDictionary*attrDict2=@{NSUnderlineColorAttributeName:[UIColororangeColor], NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick), NSFontAttributeName:[UIFontsystemFontOfSize:20]}; _label02.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict2]; NSDictionary*attrDict3=@{NSUnderlineColorAttributeName:[UIColorgreenColor], NSUnderlineStyleAttributeName:@(NSUnderlineStyleDouble), NSFontAttributeName:[UIFontsystemFontOfSize:20]}; _label03.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes: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:[UIFontsystemFontOfSize:30]}; _label01.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict1]; NSDictionary*attrDict2=@{NSStrokeWidthAttributeName:@(0), NSFontAttributeName:[UIFontsystemFontOfSize:30]}; _label02.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict2]; NSDictionary*attrDict3=@{NSStrokeWidthAttributeName:@(3), NSFontAttributeName:[UIFontsystemFontOfSize:30]}; _label03.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes: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:[UIColororangeColor], NSFontAttributeName:[UIFontsystemFontOfSize:30]}; _label01.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict1]; NSDictionary*attrDict2=@{NSStrokeWidthAttributeName:@(0), NSStrokeColorAttributeName:[UIColorblueColor], NSFontAttributeName:[UIFontsystemFontOfSize:30]}; _label02.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict2]; NSDictionary*attrDict3=@{NSStrokeWidthAttributeName:@(3), NSStrokeColorAttributeName:[UIColorgreenColor], NSFontAttributeName:[UIFontsystemFontOfSize:30]}; _label03.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes: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=[[NSShadowalloc]init];//NSShadow對象比較簡單,只有3個屬性:陰影顏色,模糊半徑和偏移 shadow1.shadowOffset=CGSizeMake(3,3);//陰影偏移(X方向偏移和Y方向偏移) shadow1.shadowBlurRadius=0.5;//模糊半徑 shadow1.shadowColor=[UIColororangeColor];//陰影顏色 NSShadow*shadow2=[[NSShadowalloc]init]; shadow2.shadowOffset=CGSizeMake(3,16); shadow2.shadowBlurRadius=2.5; shadow2.shadowColor=[UIColorpurpleColor]; NSShadow*shadow3=[[NSShadowalloc]init]; shadow3.shadowOffset=CGSizeMake(16,3); shadow3.shadowBlurRadius=4.0; shadow3.shadowColor=[UIColorblueColor]; NSDictionary*attrDict1=@{NSShadowAttributeName:shadow1, NSFontAttributeName:[UIFontsystemFontOfSize:20]}; _label01.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict1]; NSDictionary*attrDict2=@{NSShadowAttributeName:shadow2, NSFontAttributeName:[UIFontsystemFontOfSize:20]}; _label02.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict2]; NSDictionary*attrDict3=@{NSShadowAttributeName:shadow3, NSFontAttributeName:[UIFontsystemFontOfSize:20]}; _label03.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes: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(凸版印刷效果),適用於iOS7.0及以上 NSDictionary*attrDict1=@{NSTextEffectAttributeName:NSTextEffectLetterpressStyle, NSForegroundColorAttributeName:[UIColorgrayColor], NSFontAttributeName:[UIFontsystemFontOfSize:30]}; _label01.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict1]; NSDictionary*attrDict2=@{//NSTextEffectAttributeName:NSTextEffectLetterpressStyle, NSForegroundColorAttributeName:[UIColorgrayColor], NSFontAttributeName:[UIFontsystemFontOfSize:30]}; _label02.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict2]; NSDictionary*attrDict3=@{NSTextEffectAttributeName:NSTextEffectLetterpressStyle, NSForegroundColorAttributeName:[UIColorblueColor], NSFontAttributeName:[UIFontsystemFontOfSize:30]}; _label03.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes: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:[UIFontsystemFontOfSize:20]}; _label01.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict1]; NSDictionary*attrDict2=@{NSBaselineOffsetAttributeName:@(0), NSFontAttributeName:[UIFontsystemFontOfSize:20]}; _label02.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict2]; NSDictionary*attrDict3=@{NSBaselineOffsetAttributeName:@(10), NSFontAttributeName:[UIFontsystemFontOfSize:20]}; _label03.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes: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:[UIFontsystemFontOfSize:30]}; _label01.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict1]; NSDictionary*attrDict2=@{NSObliquenessAttributeName:@(0), NSFontAttributeName:[UIFontsystemFontOfSize:30]}; _label02.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict2]; NSDictionary*attrDict3=@{NSObliquenessAttributeName:@(0.8), NSFontAttributeName:[UIFontsystemFontOfSize:30]}; _label03.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes: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:[UIFontsystemFontOfSize:20]}; _label01.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict1]; NSDictionary*attrDict2=@{NSExpansionAttributeName:@(0), NSFontAttributeName:[UIFontsystemFontOfSize:20]}; _label02.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict2]; NSDictionary*attrDict3=@{NSExpansionAttributeName:@(0.6), NSFontAttributeName:[UIFontsystemFontOfSize:20]}; _label03.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes: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:[UIFontsystemFontOfSize:20]}; _label01.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict1]; NSDictionary*attrDict2=@{NSWritingDirectionAttributeName:@[@(NSWritingDirectionRightToLeft|NSTextWritingDirectionOverride)], NSFontAttributeName:[UIFontsystemFontOfSize:20]}; _label02.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict2]; NSDictionary*attrDict3=@{NSWritingDirectionAttributeName:@[@(NSWritingDirectionLeftToRight|NSTextWritingDirectionOverride)], NSFontAttributeName:[UIFontsystemFontOfSize:20]}; _label03.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes: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:[UIFontsystemFontOfSize:20]}; _label01.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict1]; NSDictionary*attrDict2=@{NSVerticalGlyphFormAttributeName:@(0), NSFontAttributeName:[UIFontsystemFontOfSize:20]}; _label02.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes:attrDict2]; NSDictionary*attrDict3=@{NSVerticalGlyphFormAttributeName:@(10), NSFontAttributeName:[UIFontsystemFontOfSize:20]}; _label03.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes: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:[NSURLURLWithString:@"http://www.baidu.com"], NSFontAttributeName:[UIFontsystemFontOfSize:20]}; _textview01.editable=NO;//必須禁止輸入,否則點擊將彈出輸入鍵盤 _textview01.scrollEnabled=NO;//可選 _textview01.delegate=self;//必須設置,否則代理函數不會被回調 _textview01.attributedText=[[NSAttributedStringalloc]initWithString:originStrattributes: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*)textViewshouldInteractWithURL:(NSURL*)URLinRange:(NSRange)characterRange{ NSLog(@"textViewisclicked..."); returnYES; }
- (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=[[NSTextAttachmentalloc]init]; textAttachment01.image=[UIImageimageNamed:@"10000.jpeg"];//設置圖片源 textAttachment01.bounds=CGRectMake(0,0,30,30);//設置圖片位置和大小 NSMutableAttributedString*attrStr01=[[NSMutableAttributedStringalloc]initWithString:originStr]; [attrStr01addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:25]range:NSMakeRange(0,originStr.length)]; NSAttributedString*attrStr11=[NSAttributedStringattributedStringWithAttachment:textAttachment01]; [attrStr01insertAttributedString:attrStr11atIndex:2];//NSTextAttachment占用一個字符長度,插入後原字符串長度增加1 _label01.attributedText=attrStr01; NSTextAttachment*textAttachment02=[[NSTextAttachmentalloc]init]; textAttachment02.image=[UIImageimageNamed:@"10000.jpeg"];//設置圖片源 textAttachment02.bounds=CGRectMake(0,-10,30,40);//設置圖片位置和大小 NSMutableAttributedString*attrStr02=[[NSMutableAttributedStringalloc]initWithString:originStr]; [attrStr02addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:25]range:NSMakeRange(0,originStr.length)]; NSAttributedString*attrStr12=[NSAttributedStringattributedStringWithAttachment:textAttachment02]; [attrStr02insertAttributedString:attrStr12atIndex:6]; _label02.attributedText=attrStr02; NSTextAttachment*textAttachment03=[[NSTextAttachmentalloc]init]; textAttachment03.image=[UIImageimageNamed:@"10000.jpeg"];//設置圖片源 textAttachment03.bounds=CGRectMake(0,-6,50,30);//設置圖片位置和大小 NSMutableAttributedString*attrStr03=[[NSMutableAttributedStringalloc]initWithString:originStr]; [attrStr03addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:25]range:NSMakeRange(0,originStr.length)]; NSAttributedString*attrStr13=[NSAttributedStringattributedStringWithAttachment:textAttachment03]; [attrStr03insertAttributedString:attrStr13atIndex: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, //}; //typedefNSIntegerNSTextAlignment; _textview01.editable=NO; _textview02.editable=NO; _label11.text=@"alignment:NSTextAlignmentCenter"; _label12.text=@"alignment:NSTextAlignmentJustified"; NSString*text=[NSStringstringWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"荷塘月色"ofType:@"txt"]encoding:NSUTF8StringEncodingerror:NULL]; //NSParagraphStyle NSMutableParagraphStyle*paraStyle01=[[NSMutableParagraphStylealloc]init]; paraStyle01.alignment=NSTextAlignmentNatural; NSDictionary*attrDict01=@{NSParagraphStyleAttributeName:paraStyle01, NSFontAttributeName:[UIFontsystemFontOfSize:12]}; _textview01.attributedText=[[NSAttributedStringalloc]initWithString:textattributes:attrDict01]; NSMutableParagraphStyle*paraStyle02=[[NSMutableParagraphStylealloc]init]; paraStyle02.alignment=NSTextAlignmentJustified; NSDictionary*attrDict02=@{NSParagraphStyleAttributeName:paraStyle02, NSFontAttributeName:[UIFontsystemFontOfSize:12]}; _textview02.attributedText=[[NSAttributedStringalloc]initWithString:textattributes: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之富文本26iOS之富文本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=[NSStringstringWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"荷塘月色"ofType:@"txt"]encoding:NSUTF8StringEncodingerror:NULL]; //NSParagraphStyle NSMutableParagraphStyle*paraStyle01=[[NSMutableParagraphStylealloc]init]; paraStyle01.firstLineHeadIndent=24; NSDictionary*attrDict01=@{NSParagraphStyleAttributeName:paraStyle01, NSFontAttributeName:[UIFontsystemFontOfSize:12]}; _textview01.attributedText=[[NSAttributedStringalloc]initWithString:textattributes:attrDict01]; NSMutableParagraphStyle*paraStyle02=[[NSMutableParagraphStylealloc]init]; paraStyle02.firstLineHeadIndent=48; NSDictionary*attrDict02=@{NSParagraphStyleAttributeName:paraStyle02, NSFontAttributeName:[UIFontsystemFontOfSize:12]}; _textview02.attributedText=[[NSAttributedStringalloc]initWithString:textattributes: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=[NSStringstringWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"荷塘月色"ofType:@"txt"]encoding:NSUTF8StringEncodingerror:NULL]; //NSParagraphStyle NSMutableParagraphStyle*paraStyle01=[[NSMutableParagraphStylealloc]init]; paraStyle01.headIndent=24; NSDictionary*attrDict01=@{NSParagraphStyleAttributeName:paraStyle01, NSFontAttributeName:[UIFontsystemFontOfSize:12]}; _textview01.attributedText=[[NSAttributedStringalloc]initWithString:textattributes:attrDict01]; NSMutableParagraphStyle*paraStyle02=[[NSMutableParagraphStylealloc]init]; paraStyle02.headIndent=48; NSDictionary*attrDict02=@{NSParagraphStyleAttributeName:paraStyle02, NSFontAttributeName:[UIFontsystemFontOfSize:12]}; _textview02.attributedText=[[NSAttributedStringalloc]initWithString:textattributes: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=[NSStringstringWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"荷塘月色"ofType:@"txt"]encoding:NSUTF8StringEncodingerror:NULL]; //NSParagraphStyle NSMutableParagraphStyle*paraStyle01=[[NSMutableParagraphStylealloc]init]; paraStyle01.tailIndent=48; NSDictionary*attrDict01=@{NSParagraphStyleAttributeName:paraStyle01, NSFontAttributeName:[UIFontsystemFontOfSize:12]}; _textview01.attributedText=[[NSAttributedStringalloc]initWithString:textattributes:attrDict01]; NSMutableParagraphStyle*paraStyle02=[[NSMutableParagraphStylealloc]init]; paraStyle02.tailIndent=252; NSDictionary*attrDict02=@{NSParagraphStyleAttributeName:paraStyle02, NSFontAttributeName:[UIFontsystemFontOfSize:12]}; _textview02.attributedText=[[NSAttributedStringalloc]initWithString:textattributes: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=[NSStringstringWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"荷塘月色"ofType:@"txt"]encoding:NSUTF8StringEncodingerror:NULL]; //NSParagraphStyle NSMutableParagraphStyle*paraStyle01=[[NSMutableParagraphStylealloc]init]; paraStyle01.lineHeightMultiple=0.6; NSDictionary*attrDict01=@{NSParagraphStyleAttributeName:paraStyle01, NSFontAttributeName:[UIFontsystemFontOfSize:12]}; _textview01.attributedText=[[NSAttributedStringalloc]initWithString:textattributes:attrDict01]; NSMutableParagraphStyle*paraStyle02=[[NSMutableParagraphStylealloc]init]; paraStyle02.lineHeightMultiple=2.5; NSDictionary*attrDict02=@{NSParagraphStyleAttributeName:paraStyle02, NSFontAttributeName:[UIFontsystemFontOfSize:12]}; _textview02.attributedText=[[NSAttributedStringalloc]initWithString:textattributes: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=[NSStringstringWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"荷塘月色"ofType:@"txt"]encoding:NSUTF8StringEncodingerror:NULL]; //NSParagraphStyle NSMutableParagraphStyle*paraStyle01=[[NSMutableParagraphStylealloc]init]; paraStyle01.maximumLineHeight=7; NSDictionary*attrDict01=@{NSParagraphStyleAttributeName:paraStyle01, NSFontAttributeName:[UIFontsystemFontOfSize:12]}; _textview01.attributedText=[[NSAttributedStringalloc]initWithString:textattributes:attrDict01]; NSMutableParagraphStyle*paraStyle02=[[NSMutableParagraphStylealloc]init]; paraStyle02.maximumLineHeight=25; NSDictionary*attrDict02=@{NSParagraphStyleAttributeName:paraStyle02, NSFontAttributeName:[UIFontsystemFontOfSize:12]}; _textview02.attributedText=[[NSAttributedStringalloc]initWithString:textattributes: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=[NSStringstringWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"荷塘月色"ofType:@"txt"]encoding:NSUTF8StringEncodingerror:NULL]; //NSParagraphStyle NSMutableParagraphStyle*paraStyle01=[[NSMutableParagraphStylealloc]init]; paraStyle01.minimumLineHeight=0.6; NSDictionary*attrDict01=@{NSParagraphStyleAttributeName:paraStyle01, NSFontAttributeName:[UIFontsystemFontOfSize:12]}; _textview01.attributedText=[[NSAttributedStringalloc]initWithString:textattributes:attrDict01]; NSMutableParagraphStyle*paraStyle02=[[NSMutableParagraphStylealloc]init]; paraStyle02.minimumLineHeight=25; NSDictionary*attrDict02=@{NSParagraphStyleAttributeName:paraStyle02, NSFontAttributeName:[UIFontsystemFontOfSize:12]}; _textview02.attributedText=[[NSAttributedStringalloc]initWithString:textattributes: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=[NSStringstringWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"荷塘月色"ofType:@"txt"]encoding:NSUTF8StringEncodingerror:NULL]; //NSParagraphStyle NSMutableParagraphStyle*paraStyle01=[[NSMutableParagraphStylealloc]init]; paraStyle01.lineSpacing=-7; NSDictionary*attrDict01=@{NSParagraphStyleAttributeName:paraStyle01, NSFontAttributeName:[UIFontsystemFontOfSize:12]}; _textview01.attributedText=[[NSAttributedStringalloc]initWithString:textattributes:attrDict01]; NSMutableParagraphStyle*paraStyle02=[[NSMutableParagraphStylealloc]init]; paraStyle02.lineSpacing=25; NSDictionary*attrDict02=@{NSParagraphStyleAttributeName:paraStyle02, NSFontAttributeName:[UIFontsystemFontOfSize:12]}; _textview02.attributedText=[[NSAttributedStringalloc]initWithString:textattributes: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=[NSStringstringWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"荷塘月色"ofType:@"txt"]encoding:NSUTF8StringEncodingerror:NULL]; //NSParagraphStyle NSMutableParagraphStyle*paraStyle01=[[NSMutableParagraphStylealloc]init]; paraStyle01.paragraphSpacing=-7; NSDictionary*attrDict01=@{NSParagraphStyleAttributeName:paraStyle01, NSFontAttributeName:[UIFontsystemFontOfSize:12]}; _textview01.attributedText=[[NSAttributedStringalloc]initWithString:textattributes:attrDict01]; NSMutableParagraphStyle*paraStyle02=[[NSMutableParagraphStylealloc]init]; paraStyle02.paragraphSpacing=25; NSDictionary*attrDict02=@{NSParagraphStyleAttributeName:paraStyle02, NSFontAttributeName:[UIFontsystemFontOfSize:12]}; _textview02.attributedText=[[NSAttributedStringalloc]initWithString:textattributes: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=[NSStringstringWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"荷塘月色"ofType:@"txt"]encoding:NSUTF8StringEncodingerror:NULL]; //NSParagraphStyle NSMutableParagraphStyle*paraStyle01=[[NSMutableParagraphStylealloc]init]; paraStyle01.paragraphSpacingBefore=-7; NSDictionary*attrDict01=@{NSParagraphStyleAttributeName:paraStyle01, NSFontAttributeName:[UIFontsystemFontOfSize:12]}; _textview01.attributedText=[[NSAttributedStringalloc]initWithString:textattributes:attrDict01]; NSMutableParagraphStyle*paraStyle02=[[NSMutableParagraphStylealloc]init]; paraStyle02.paragraphSpacingBefore=25; NSDictionary*attrDict02=@{NSParagraphStyleAttributeName:paraStyle02, NSFontAttributeName:[UIFontsystemFontOfSize:12]}; _textview02.attributedText=[[NSAttributedStringalloc]initWithString:textattributes: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 //}; //typedefNSIntegerNSWritingDirection; _textview01.editable=NO; _textview02.editable=NO; _label11.text=@"baseWritingDirection:NSWritingDirectionLeftToRight"; _label12.text=@"baseWritingDirection:NSWritingDirectionRightToLeft"; NSString*text=[NSStringstringWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"荷塘月色"ofType:@"txt"]encoding:NSUTF8StringEncodingerror:NULL]; //NSParagraphStyle NSMutableParagraphStyle*paraStyle01=[[NSMutableParagraphStylealloc]init]; paraStyle01.baseWritingDirection=NSWritingDirectionLeftToRight; NSDictionary*attrDict01=@{NSParagraphStyleAttributeName:paraStyle01, NSFontAttributeName:[UIFontsystemFontOfSize:12]}; _textview01.attributedText=[[NSAttributedStringalloc]initWithString:textattributes:attrDict01]; NSMutableParagraphStyle*paraStyle02=[[NSMutableParagraphStylealloc]init]; paraStyle02.baseWritingDirection=NSWritingDirectionRightToLeft; NSDictionary*attrDict02=@{NSParagraphStyleAttributeName:paraStyle02, NSFontAttributeName:[UIFontsystemFontOfSize:12]}; _textview02.attributedText=[[NSAttributedStringalloc]initWithString:textattributes: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//非自動換行,中間切斷 //}; //typedefNSUIntegerNSLineBreakMode; _textview01.editable=NO; _textview02.editable=NO; _label11.text=@"lineBreakMode:NSLineBreakByTruncatingHead"; _label12.text=@"lineBreakMode:NSLineBreakByTruncatingTail"; NSString*strstr=@"ThesetwodocumentsprovidetheperfectstartingpointforiOSandMacappdevelopment.FolloweitherroadmaptolearnhowtogetanduseXcodetocreateyourfirstapp.YouwilllearnhowtouseXcodetotestanddebugyoursourcecode,analyzetoimproveyourapp’sperformance,performsourcecontroloperations,archiveyourapp,andsubmityourapptotheAppStore."; NSString*text=[NSStringstringWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"荷塘月色"ofType:@"txt"]encoding:NSUTF8StringEncodingerror:NULL]; //NSParagraphStyle NSMutableParagraphStyle*paraStyle01=[[NSMutableParagraphStylealloc]init]; paraStyle01.lineBreakMode=NSLineBreakByTruncatingHead; NSDictionary*attrDict01=@{NSParagraphStyleAttributeName:paraStyle01, NSFontAttributeName:[UIFontsystemFontOfSize:12]}; _textview01.attributedText=[[NSAttributedStringalloc]initWithString:textattributes:attrDict01]; NSMutableParagraphStyle*paraStyle02=[[NSMutableParagraphStylealloc]init]; paraStyle02.lineBreakMode=NSLineBreakByTruncatingTail; NSDictionary*attrDict02=@{NSParagraphStyleAttributeName:paraStyle02, NSFontAttributeName:[UIFontsystemFontOfSize:12]}; _textview02.attributedText=[[NSAttributedStringalloc]initWithString:textattributes: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之富文本38iOS之富文本39iOS之富文本40

 

 

<13>. hyphenationFactor

 

 

 

[objc] view plain copy print ?   //hyphenationFactor連字符屬性,取值0到1之間,開啟斷詞功能 NSString*strstr=@"ThesetwodocumentsprovidetheperfectstartingpointforiOSandMacappdevelopment.FolloweitherroadmaptolearnhowtogetanduseXcodetocreateyourfirstapp.YouwilllearnhowtouseXcodetotestanddebugyoursourcecode,analyzetoimproveyourapp’sperformance,performsourcecontroloperations,archiveyourapp,andsubmityourapptotheAppStore."; _textview01.editable=NO; _textview02.editable=NO; _label11.text=@"hyphenationFactor:0.3"; _label12.text=@"hyphenationFactor:0.9"; //NSParagraphStyle NSMutableParagraphStyle*paraStyle01=[[NSMutableParagraphStylealloc]init]; paraStyle01.hyphenationFactor=0.3; NSDictionary*attrDict01=@{NSParagraphStyleAttributeName:paraStyle01, NSFontAttributeName:[UIFontsystemFontOfSize:15]}; _textview01.attributedText=[[NSAttributedStringalloc]initWithString:strstrattributes:attrDict01]; NSMutableParagraphStyle*paraStyle02=[[NSMutableParagraphStylealloc]init]; paraStyle02.hyphenationFactor=0.9; NSDictionary*attrDict02=@{NSParagraphStyleAttributeName:paraStyle02, NSFontAttributeName:[UIFontsystemFontOfSize:15]}; _textview02.attributedText=[[NSAttributedStringalloc]initWithString:strstrattributes: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。

大功終於告成!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved