強(qiáng)大的富文本功能解讀

利用NSAttributedString (富文本)能調(diào)整UILable等文字相關(guān)控件顯示文字的字體、顏色、字體間距、行距、左邊距、下劃線等,如果不調(diào)整的情況下,iOS10以下的和10以上的文字的行距不同。下面介紹兩個(gè)功能, 下面兩端代碼直接拿去用,看代碼即可理解做法

利用富文本展現(xiàn)換行的協(xié)議,效果如圖


換行的協(xié)議,需要點(diǎn)擊高亮

一、調(diào)整一段文字的行距

在有多行文字要展示的時(shí)候,UI設(shè)計(jì)圖上是有行距的,然而iOS9以下系統(tǒng)默認(rèn)的行距是比較小的不符合需要,需要改成自定義的行距。
1. 可以使用NSAttributedString或NSMutableAttributedString的attributes來(lái)實(shí)現(xiàn)設(shè)置行距等文字樣式,如下


NSString *regularStr = @"....要顯示的內(nèi)容....";

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];? ?


?[paragraphStyle setLineSpacing:5];// 調(diào)整行間距


NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:regularStr attributes:@{NSParagraphStyleAttributeName: paragraphStyle, NSFontAttributeName: label.font}]; showmessageLab.attributedText = attrStr;


// 通過(guò)NSMutableParagraphStyle還可以設(shè)置文本其他樣式,具體可以查看其頭文件
//?attributes字典里面還可以設(shè)置很多樣式:
/*@{ NSFontAttributeName://(字體)
NSBackgroundColorAttributeName://(字體背景色)
NSForegroundColorAttributeName://(字體顏色)
NSParagraphStyleAttributeName://(段落)
NSLigatureAttributeName://(連字符)
NSKernAttributeName://(字間距)
NSStrikethroughStyleAttributeName: NSUnderlinePatternSolid(實(shí)線) | NSUnderlineStyleSingle(刪除線) NSUnderlineStyleAttributeName://(下劃線)
?NSStrokeColorAttributeName://(邊線顏色)
NSStrokeWidthAttributeName://(邊線寬度)
NSShadowAttributeName://(陰影)
NSVerticalGlyphFormAttributeName://(橫豎排版) }*/


二、計(jì)算NSAttributedString所占的size

方法一:(最簡(jiǎn)單,推薦使用)利用UILabel 對(duì)象的sizeToFit方法計(jì)算

NSString *regularStr = @"....要顯示的內(nèi)容....";
?NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
?[paragraphStyle setLineSpacing:5];//調(diào)整行間距
?NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:regularStr attributes:@{NSParagraphStyleAttributeName: paragraphStyle, NSFontAttributeName: showmessageL.font}];
showmessageL.attributedText = attrStr; ? ?
[showmessageL sizeToFit];?
?UIScrollView *scrollV = (UIScrollView *)showmessageLab.superview; ? ?
scrollV.contentSize = CGSizeMake(0, showmessageLab.maxY + 30);
// 好處是不需要設(shè)置NSFontAttributeName能計(jì)算準(zhǔn)確,而且showmessageL不需要再重新調(diào)整高度

方法二:(比較麻煩)先計(jì)算尺寸再給label設(shè)置高度


NSString *regularStr = @"....要顯示的內(nèi)容....";
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; ? ?
[paragraphStyle setLineSpacing:5];//調(diào)整行間距 ? ?
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:regularStr attributes:@{NSParagraphStyleAttributeName: paragraphStyle, NSFontAttributeName: showmessageLab.font}];? ? ?
CGSize attrTextSize = [attrStr boundingRectWithSize:CGSizeMake(showmessageL.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil].size; ? ? showmessageL.attributedText = attrStr; ? ?
showmessageL.height = attrTextSize.height;? ?
?UIScrollView *scrollV = (UIScrollView *)showmessageL.superview; ? ?
scrollV.contentSize = CGSizeMake(0, showmessageLab.maxY + 30);


// 需要設(shè)置NSFontAttributeName才能計(jì)算準(zhǔn)確,而且showmessageL需要再重新調(diào)整高度

三、換行協(xié)議文字高亮響應(yīng)點(diǎn)擊的效果

隆重推薦:

YYText, 功能強(qiáng)大,效果像word文檔一樣編輯界面,文字中間加圖片、控件等,在github上下載demo即可查看到使用方式.

?NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"我已閱讀并同意"]; ? ?

text.yy_font = [UIFont systemFontOfSize:12]; ?

? text.yy_color = UIColor.wj_lightGray; ?

? [text yy_setTextHighlightRange:text.yy_rangeOfAll color:nil backgroundColor:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {? ? ? ? ? ?

? ?if (checkbox.enabled && checkbox.userInteractionEnabled ) {? ? ? ? ? ? ? ??

????????checkbox.selected = !checkbox.selected;? ? ? ? ?}?

? ?}];

? CGFloat labelW = bgViewW - checkbox.maxX; ?

? YYLabel *label = [YYLabel viewWithFrame:RECT(checkbox.maxX+2, 0, labelW, bgview.height) backgroundColor:nil superview:bgview]; ?

? label.textVerticalAlignment = YYTextVerticalAlignmentCenter; ? ?

label.textAlignment = NSTextAlignmentLeft; ?

? label.numberOfLines = 2;

label.attributedText =?text;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容