總體來說個人化定製UITextView中的內容有兩種方法:
1,從檔案中讀取內容到UITextView,這個個人感覺使用rtfd和rtf格式檔案效果非常好。
2,使用NSAttributeString進行定製
具體方法如下:
- NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
- paragraphStyle.lineHeightMultiple = 20.f;
- paragraphStyle.maximumLineHeight = 25.f;
- paragraphStyle.minimumLineHeight = 15.f;
- paragraphStyle.firstLineHeadIndent = 20.f;
- paragraphStyle.alignment = NSTextAlignmentJustified;
-
- NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:14], NSParagraphStyleAttributeName:paragraphStyle, NSForegroundColorAttributeName:[UIColor colorWithRed:76./255. green:75./255. blue:71./255. alpha:1]
- };
- textView.attributedText = [[NSAttributedString alloc]initWithString:content attributes:attributes];
當然也可以初始化一個NSMutableAttributedString,然後向裡面添加文字樣式,最後將它賦給textView的AttributedText即可
- NSMutableAttributedString *atr = [[NSMutableAttributedString alloc]initWithString:detail];
- [atr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, detail.length)];
- textView.attributedText = atr;
另外,對於textview中的連結樣式,同樣也可以定製
- NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor blueColor],
- NSUnderlineColorAttributeName: [UIColor blackColor],
- NSUnderlineStyleAttributeName: @(NSUnderlinePatternDash)};
- self.linkTextAttributes = linkAttributes;
這裡只是個簡單的例子,具體還有很多屬性可以自行參考標頭檔