强大的富文本功能解读

2017-12-05  本文已影响18人  Sweet丶

利用NSAttributedString (富文本)能调整UILable等文字相关控件显示文字的字体、颜色、字体间距、行距、左边距、下划线等,如果不调整的情况下,iOS10以下的和10以上的文字的行距不同。下面介绍两个功能, 下面两端代码直接拿去用,看代码即可理解做法

利用富文本展现换行的协议,效果如图

换行的协议,需要点击高亮

一、调整一段文字的行距

在有多行文字要展示的时候,UI设计图上是有行距的,然而iOS9以下系统默认的行距是比较小的不符合需要,需要改成自定义的行距。
1. 可以使用NSAttributedString或NSMutableAttributedString的attributes来实现设置行距等文字样式,如下

NSString *regularStr = @"....要显示的内容....";

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

 [paragraphStyle setLineSpacing:5];// 调整行间距

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

// 通过NSMutableParagraphStyle还可以设置文本其他样式,具体可以查看其头文件
// attributes字典里面还可以设置很多样式:
/*@{ NSFontAttributeName://(字体)
NSBackgroundColorAttributeName://(字体背景色)
NSForegroundColorAttributeName://(字体颜色)
NSParagraphStyleAttributeName://(段落)
NSLigatureAttributeName://(连字符)
NSKernAttributeName://(字间距)
NSStrikethroughStyleAttributeName: NSUnderlinePatternSolid(实线) | NSUnderlineStyleSingle(删除线) NSUnderlineStyleAttributeName://(下划线)
 NSStrokeColorAttributeName://(边线颜色)
NSStrokeWidthAttributeName://(边线宽度)
NSShadowAttributeName://(阴影)
NSVerticalGlyphFormAttributeName://(横竖排版) }*/

二、计算NSAttributedString所占的size

方法一:(最简单,推荐使用)利用UILabel 对象的sizeToFit方法计算

NSString *regularStr = @"....要显示的内容....";
 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
 [paragraphStyle setLineSpacing:5];//调整行间距
 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);
// 好处是不需要设置NSFontAttributeName能计算准确,而且showmessageL不需要再重新调整高度

方法二:(比较麻烦)先计算尺寸再给label设置高度

NSString *regularStr = @"....要显示的内容....";
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];    
[paragraphStyle setLineSpacing:5];//调整行间距    
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);

// 需要设置NSFontAttributeName才能计算准确,而且showmessageL需要再重新调整高度

三、换行协议文字高亮响应点击的效果

隆重推荐:

YYText, 功能强大,效果像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;

上一篇下一篇

猜你喜欢

热点阅读