iOS:個人化UITextView(縮排,行距,鋪滿)

來源:互聯網
上載者:User

總體來說個人化定製UITextView中的內容有兩種方法:

1,從檔案中讀取內容到UITextView,這個個人感覺使用rtfd和rtf格式檔案效果非常好。

2,使用NSAttributeString進行定製

具體方法如下:

  1. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];  
  2.     paragraphStyle.lineHeightMultiple = 20.f;  
  3.     paragraphStyle.maximumLineHeight = 25.f;  
  4.     paragraphStyle.minimumLineHeight = 15.f;  
  5.     paragraphStyle.firstLineHeadIndent = 20.f;  
  6. paragraphStyle.alignment = NSTextAlignmentJustified;  
  7.   
  8. NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:14], NSParagraphStyleAttributeName:paragraphStyle, NSForegroundColorAttributeName:[UIColor colorWithRed:76./255. green:75./255. blue:71./255. alpha:1]  
  9.                                  };  
  10.  textView.attributedText = [[NSAttributedString alloc]initWithString:content attributes:attributes];  

 

當然也可以初始化一個NSMutableAttributedString,然後向裡面添加文字樣式,最後將它賦給textView的AttributedText即可

  1. NSMutableAttributedString *atr = [[NSMutableAttributedString alloc]initWithString:detail];  
  2.     [atr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, detail.length)];  
  3.     textView.attributedText = atr;  

 

另外,對於textview中的連結樣式,同樣也可以定製

  1. NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor blueColor],  
  2.                                      NSUnderlineColorAttributeName: [UIColor blackColor],  
  3.                                      NSUnderlineStyleAttributeName: @(NSUnderlinePatternDash)};  
  4. self.linkTextAttributes = linkAttributes;  

 

這裡只是個簡單的例子,具體還有很多屬性可以自行參考標頭檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.