还没有基础的同学可以看看我讲解的基础篇文章
这里我们讲一下快速设置富文本显示
和计算富文本高度
NSAttributedString
设置段落样式(不考虑首行缩进情况)
/**
* 设置段落样式
*
* @param lineSpacing 行高
* @param textcolor 字体颜色
* @param font 字体
*
* @return 富文本
*/
-(NSAttributedString *)stringWithParagraphlineSpeace:(CGFloat)lineSpacing
textColor:(UIColor *)textcolor
textFont:(UIFont *)font {
// 设置段落
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = lineSpacing;
// NSKernAttributeName字体间距
NSDictionary *attributes = @{ NSParagraphStyleAttributeName:paragraphStyle,NSKernAttributeName:@1.5f};
NSMutableAttributedString * attriStr = [[NSMutableAttributedString alloc] initWithString:self attributes:attributes];
// 创建文字属性
NSDictionary * attriBute = @{NSForegroundColorAttributeName:textcolor,NSFontAttributeName:font};
[attriStr addAttributes:attriBute range:NSMakeRange(0, self.length)];
return attriStr;
}
计算富文本字体高度
/**
* 计算富文本字体高度
*
* @param lineSpeace 行高
* @param font 字体
* @param width 字体所占宽度
*
* @return 富文本高度
*/
-(CGFloat)getSpaceLabelHeightwithSpeace:(CGFloat)lineSpeace withFont:(UIFont*)font withWidth:(CGFloat)width {
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
// paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
/** 行高 */
paraStyle.lineSpacing = lineSpeace;
// NSKernAttributeName字体间距
NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f
};
CGSize size = [self boundingRectWithSize:CGSizeMake(width,MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
return size.height;
}
/********************************************************************
* 返回包含关键字的富文本编辑
*
* @param lineSpacing 行高
* @param textcolor 字体颜色
* @param font 字体
* @param KeyColor 关键字字体颜色
* @param KeyFont 关键字字体
* @param KeyWords 关键字数组
*
* @return
********************************************************************/
-(NSAttributedString *)stringWithParagraphlineSpeace:(CGFloat)lineSpacing
textColor:(UIColor *)textcolor
textFont:(UIFont *)font
withKeyTextColor:(UIColor *)KeyColor
keyFont:(UIFont *)KeyFont
keyWords:(NSArray *)KeyWords;
.m文件如下
/********************************************************************
* 返回包含关键字的富文本编辑
*
* @param lineSpacing 行高
* @param textcolor 字体颜色
* @param font 字体
* @param KeyColor 关键字字体颜色
* @param KeyFont 关键字字体
* @param KeyWords 关键字数组
*
* @return
*******************************************************************/
-(NSAttributedString *)stringWithParagraphlineSpeace:(CGFloat)lineSpacing
textColor:(UIColor *)textcolor
textFont:(UIFont *)font
withKeyTextColor:(UIColor *)KeyColor
keyFont:(UIFont *)KeyFont
keyWords:(NSArray *)KeyWords {
NSAttributedString * AttributeString = [self stringWithParagraphlineSpeace:lineSpacing textColor:textcolor textFont:font compated:^(NSMutableAttributedString *attriStr) {
NSDictionary * KeyattriBute = @{NSForegroundColorAttributeName:KeyColor,NSFontAttributeName:KeyFont};
for (NSString * item in KeyWords) {
NSRange range = [self rangeOfString:item options:(NSCaseInsensitiveSearch)];
[attriStr addAttributes:KeyattriBute range:range];
}
}];
return AttributeString;
}
/********************************************************************
* 基本校验方法
*
* @param lineSpacing 行高
* @param textcolor 字体颜色
* @param font 字体
* @param KeyColor 关键字字体颜色
* @param KeyFont 关键字字体
* @param KeyWords 关键字字符数组
*
* @return <#return value description#>
********************************************************************/
-(NSAttributedString *)stringWithParagraphlineSpeace:(CGFloat)lineSpacing
textColor:(UIColor *)textcolor
textFont:(UIFont *)font
compated:(void(^)(NSMutableAttributedString * attriStr))compalted
{
// 设置段落
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = lineSpacing;
// NSKernAttributeName字体间距
NSDictionary *attributes = @{ NSParagraphStyleAttributeName:paragraphStyle,NSKernAttributeName:@1.5f};
NSMutableAttributedString * attriStr = [[NSMutableAttributedString alloc] initWithString:self attributes:attributes];
// 创建文字属性
NSDictionary * attriBute = @{NSForegroundColorAttributeName:textcolor,NSFontAttributeName:font};
[attriStr addAttributes:attriBute range:NSMakeRange(0, self.length)];
if (compalted) {
compalted(attriStr);
}
return attriStr;
}
各位看官来看看怎么使用
最多的场景就是我们要算出这个富文本的高度去做一些事情。我们这里算出富文本的高度来让他充分的显示完成
设置一个富文本
// 没有设置Frma,我们要根据他的自身的大小来设置
UILabel *label=[[UILabel alloc]init];
label.backgroundColor=[UIColor grayColor];
label.numberOfLines=0;
[self.view addSubview:label];
NSString * conts = @"公元前3000年,印度河流域的居民的数字使用就已经比较普遍,居民们采用了十进位制的计算法。到吠陀时代(公元前1500~公元前500年),雅利安人已意识到数字在生产活动和日常生活中的作用,创造了一些简单的、不完全的数字。\n\n公元前3世纪,印度出现了整套的数字,但各地的写法不一,其中最典型的是婆罗门式,它的独到之处就是从1~9每个数都有专用符号,现代数字就是从它们中脱胎而来的。\n当时,“0”还没有出现。到了笈多时代(300~500年)才有了“0”,叫“舜若”(shunya),表示方式是一个黑点“●”,后来衍变成“0”。\n这样,一套完整的数字便产生了。这项劳动创作也对世界文化做出了巨大的贡献。lll";
//设置富文本显示,
label.attributedText = [conts stringWithParagraphlineSpeace:6 textColor:[UIColor redColor] textFont:[UIFont systemFontOfSize:15]];
计算富文本的高度,并设置其高度
// 计算富文本的高度
CGFloat contHeight = [conts getSpaceLabelHeightwithSpeace:6 withFont:[UIFont systemFontOfSize:15] withWidth:300];
label.frame = CGRectMake(0, 50, 300, contHeight);
到此已经完毕。大家伙可以动手试试了。。。
如有问题可添加我的QQ:1290925041
还可添加QQ群:234812704(洲洲哥学院)
欢迎各位一块学习,提高逼格!
也可以添加洲洲哥的微信公众号
更多消息
更多信iOS开发信息 请以关注洲洲哥 的微信公众号,不定期有干货推送:
这里写图片描述