在iOS6之前我們都會使用OHAttributedLabel這個開發來支持富文本以及控制字符的間距和段落的行距;在iOS6之後,系統在UILabel的控件暴露屬性:attributedText和 UITextView也暴露三個屬性: allowsEditingTextAttributes,attributedText,typingAttributes。
在這裏我先給出UILabel的attributedText使用,後期我會補充UITextView部分的,同時提供封裝的類給大家
UILabel的attributedText的關鍵代碼:
NSString *contentString = @"長沙亞信長沙亞信長沙亞信長沙亞信長沙亞信長沙亞信長沙亞信長沙亞信長沙亞信長沙亞信長沙亞信長沙亞信長沙亞信長沙亞信長沙亞信長沙亞信";
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:6.0];//調整行間距
[paragraphStyle setFirstLineHeadIndent:30];
[paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
[paragraphStyle setAlignment:NSTextAlignmentJustified];
NSDictionary *attr1 = @{ NSFontAttributeName:[UIFont systemFontOfSize:15], NSForegroundColorAttributeName:[UIColor blackColor],NSParagraphStyleAttributeName:paragraphStyle };
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:contentString];
[attrString addAttributes:attr1 range:NSMakeRange(0, [attrString length])];
contentLabel.attributedText = attrString;
上面就是一個代碼片段,大家可以參考,根據自己的需要設置其他的風格,後面我會補充他們的封裝類。