iOS-文本段落樣式NSMutableParagraphStyle與NSParagraphStyle的使用和一些富文本處理屬性

來源:互聯網
上載者:User

標籤:eof   ack   mutable   指定   ali   target   表示   tom   attribute   

開發過程中,經常會遇到動態計算行高的問題, 

- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(nullable NSDictionary<NSString *, id> *)attributes context:(nullable NSStringDrawingContext *)context NS_AVAILABLE(10_11, 7_0);

是蘋果推薦的計算方法,顯然會遇到段落格式問題,例如行間距、縮排等格式設定需求,attributes傳進來的字典中,包含我們設定的字型及格式,其中NSParagraphStyleAttributeName是設定段落風格,NSFontAttributeName是設定字型。

ok,具體來看一下NSParagraphStyleAttributeName的功能。

 

[objc] view plain copy
  1. //   NSParagraphStyleAttributeName 段落的風格(設定首行,行間距,對齊什麼的)看自己需要什麼屬性,寫什麼    
  2.     NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];    
  3.     paragraphStyle.lineSpacing = 10;// 字型的行間距    
  4.     paragraphStyle.firstLineHeadIndent = 20.0f;//首行縮排    
  5.     paragraphStyle.alignment = NSTextAlignmentJustified;//(左右對齊的)文本對齊:(左,中,右,左右對齊,自然)    
  6.     paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;//結尾部分的內容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz")    
  7.     paragraphStyle.headIndent = 20;//整體縮排(首行除外)    
  8.     paragraphStyle.tailIndent = 20;//    
  9.     paragraphStyle.minimumLineHeight = 10;//最低行高    
  10.     paragraphStyle.maximumLineHeight = 20;//最大行高    
  11.     paragraphStyle.paragraphSpacing = 15;//段與段之間的間距    
  12.     paragraphStyle.paragraphSpacingBefore = 22.0f;//段首行空白空間/* Distance between the bottom of the previous paragraph (or the end of its paragraphSpacing, if any) and the top of this paragraph. */    
  13.     paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;//從左至右的書寫方向(一共??三種)    
  14.     paragraphStyle.lineHeightMultiple = 15;/* Natural line height is multiplied by this factor (if positive) before being constrained by minimum and maximum line height. */    
  15.     paragraphStyle.hyphenationFactor = 1;//連字屬性 在iOS,唯一支援的值分別為0和1    


好了,現在就可以很輕鬆的計算某一段落高度,例如:

 

[objc] view plain copy
  1. _descAtt = [[NSMutableAttributedString alloc] initWithString:_model.desc];  
  2.        UIFont *descFont = [UIFont PingFangSC_Regular_WithSize:12];  
  3.          
  4.        NSMutableParagraphStyle *descStyle = [[NSMutableParagraphStyle alloc]init];  
  5.        [descStyle setLineSpacing:1];//行間距  
  6.          
  7.        CGSize descSize = [_model.desc boundingRectWithSize:CGSizeMake(w, MAXFLOAT)  
  8.                                                    options:NSStringDrawingUsesLineFragmentOrigin  
  9.                                                 attributes:@{NSFontAttributeName:descFont,  
  10.                                                              NSParagraphStyleAttributeName :descStyle}  
  11.                                                    context:nil].size;  


另外,再介紹幾個富文本處理的屬性:

 

[objc] view plain copy
    1. // NSFontAttributeName                設定字型屬性,預設值:字型:Helvetica(Neue) 字型大小:12  
    2. // NSForegroundColorAttributeNam      設定字型顏色,取值為 UIColor對象,預設值為黑色  
    3. // NSBackgroundColorAttributeName     設定字型所在地區背景顏色,取值為 UIColor對象,預設值為nil, 透明色  
    4. // NSLigatureAttributeName            設定連體屬性,取值為NSNumber 對象(整數),0 表示沒有連體字元,1 表示使用預設的連體字元  
    5. // NSKernAttributeName                設定字元間距,取值為 NSNumber 對象(整數),正值間距加寬,負值間距變窄  
    6. // NSStrikethroughStyleAttributeName  設定刪除線,取值為 NSNumber 對象(整數)  
    7. // NSStrikethroughColorAttributeName  設定刪除線顏色,取值為 UIColor 對象,預設值為黑色  
    8. // NSUnderlineStyleAttributeName      設定底線,取值為 NSNumber 對象(整數),枚舉常量 NSUnderlineStyle中的值,與刪除線類似  
    9. // NSUnderlineColorAttributeName      設定底線顏色,取值為 UIColor 對象,預設值為黑色  
    10. // NSStrokeWidthAttributeName         設定筆畫寬度,取值為 NSNumber 對象(整數),負值填充效果,正值中空效果  
    11. // NSStrokeColorAttributeName         填充部分顏色,不是字型顏色,取值為 UIColor 對象  
    12. // NSShadowAttributeName              設定陰影屬性,取值為 NSShadow 對象  
    13. // NSTextEffectAttributeName          設定文本特殊效果,取值為 NSString 對象,目前只有圖版印刷效果可用:  
    14. // NSBaselineOffsetAttributeName      設定基準位移值,取值為 NSNumber (float),正值上偏,負值下偏  
    15. // NSObliquenessAttributeName         設定字形傾斜度,取值為 NSNumber (float),正值右傾,負值左傾  
    16. // NSExpansionAttributeName           設定文本橫向展開屬性,取值為 NSNumber (float),正值橫向展開文本,負值橫向壓縮文本  
    17. // NSWritingDirectionAttributeName    設定文字書寫方向,從左向右書寫或者從右向左書寫  
    18. // NSVerticalGlyphFormAttributeName   設定文字排版方向,取值為 NSNumber 對象(整數),0 表示橫排文本,1 表示豎排文本  
    19. // NSLinkAttributeName                設定連結屬性,點擊後呼叫瀏覽器開啟指定URL地址  
    20. // NSAttachmentAttributeName          設定文本附件,取值為NSTextAttachment對象,常用於文字圖片混排  
    21. // NSParagraphStyleAttributeName      設定文本段落排版格式,取值為 NSParagraphStyle 對象   

iOS-文本段落樣式NSMutableParagraphStyle與NSParagraphStyle的使用和一些富文本處理屬性

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.