当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS富文本的使用方法示例详解

iOS富文本的使用方法示例详解

2019年07月24日  | 移动技术网移动技术  | 我要评论

前言

常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求。

使用富文本nsmuttableattstring(带属性的字符串),上面的一些需求都可以很简便的实现。

最近想实现一个功能,如图:

每月价格

最初实现的时候想到了用两个label,来实现,第一个显示¥4000,设置一个字体,第二个显示/月,设置另一个字体.这样就能实现这个效果了,但是最后想一想还是用富文本比较好,顺便可以学习一下.

今天我们先实现这个简单的效果.

先创建一个label:

-(uilabel *)pricelabel{
 if (_pricelabel == nil) {
 _pricelabel = [[uilabel alloc]init];
 _pricelabel.font = kfont(13);
 _pricelabel.textcolor = kcolortheme;
 _pricelabel.textalignment = nstextalignmentright;
 }
 return _pricelabel;
}

自己再创建一个私有方法,把字符串(比如:¥4000/月)传进来,进行转换,返回富文本,赋值给所需要的label.

-(nsmutableattributedstring *)getpriceattribute:(nsstring *)string{
 
 nsmutableattributedstring *attribut = [[nsmutableattributedstring alloc]initwithstring:string];
 //目的是想改变 ‘/'前面的字体的属性,所以找到目标的range
 nsrange range = [string rangeofstring:@"/"];
 nsrange pointrange = nsmakerange(0, range.location);
 nsmutabledictionary *dic = [nsmutabledictionary dictionary];
 dic[nsfontattributename] = [uifont systemfontofsize:18];
 //赋值
 [attribut addattributes:dic range:pointrange]; 
 return attribut;
}

首先创建一个富文本nsmutableattributedstring对象,把传进来的nsstring对象转化为nsmutableattributedstring对象.
然后对nsmutableattributedstring进行设置.

nsrange range = [string rangeofstring:@"/"];取到一个标志的位置:range,然后对"/"前面的文字进行设置.

然后,返回富文本,再进行赋值.

 _pricelabel.attributedtext = [self getpriceattribute:@"¥4000/月"]; 

上面只是一个简单应用,还有很多常用到的富文本.比如,文字和图片的混排,文字点击事件.等等.

我们依次实现一些功能

在指定位置添加图片

nsmutableattributedstring * attristr = [[nsmutableattributedstring alloc] initwithstring:@"不要问我为什么编程,我喜欢手指在键盘上飞舞的感觉"];
[attristr addattribute:nsforegroundcolorattributename value:[uicolor bluecolor] range:nsmakerange(0, 5)];
[attristr addattribute:nsfontattributename value:[uifont systemfontofsize:20] range:nsmakerange(0, 5)];

添加图片到指定的位置

nstextattachment *attchimage = [[nstextattachment alloc] init];
// 表情图片
attchimage.image = [uiimage imagenamed:@"pic3"];
// 设置图片大小
attchimage.bounds = cgrectmake(0, -5, 20, 20);
nsattributedstring *stringimage = [nsattributedstring attributedstringwithattachment:attchimage];
[attristr insertattributedstring:stringimage atindex:2];

追加图片到最后一位

nstextattachment *attch = [[nstextattachment alloc] init];
// 表情图片
attch.image = [uiimage imagenamed:@"pic2"];
// 设置图片大小
attch.bounds = cgrectmake(0, -5, 20, 15);
// 创建带有图片的富文本
nsattributedstring *string = [nsattributedstring attributedstringwithattachment:attch];
[attristr appendattributedstring:string];

设置中间位置文字为红色

[attristr addattribute:nsforegroundcolorattributename value:[uicolor redcolor] range:nsmakerange(6, 4)];
[attristr addattribute:nsfontattributename value:[uifont systemfontofsize:30] range:nsmakerange(6, 4)];

综合写法

nsdictionary * attribute = @{nsforegroundcolorattributename:[uicolor yellowcolor],nsfontattributename:[uifont systemfontofsize:25]};
[attristr addattributes:attribute range:nsmakerange(10, 4)];
self.attrobiutelabel.attributedtext = attristr;

效果图:


最后认识一下各个属性的意思:

// nsfontattributename    设置字体属性,默认值:字体:helvetica(neue) 字号:12
// nsforegroundcolorattributenam  设置字体颜色,取值为 uicolor对象,默认值为黑色
// nsbackgroundcolorattributename  设置字体所在区域背景颜色,取值为 uicolor对象,默认值为nil, 透明色
// nsligatureattributename   设置连体属性,取值为nsnumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符
// nskernattributename    设定字符间距,取值为 nsnumber 对象(整数),正值间距加宽,负值间距变窄
// nsstrikethroughstyleattributename 设置删除线,取值为 nsnumber 对象(整数)
// nsstrikethroughcolorattributename 设置删除线颜色,取值为 uicolor 对象,默认值为黑色
// nsunderlinestyleattributename  设置下划线,取值为 nsnumber 对象(整数),枚举常量 nsunderlinestyle中的值,与删除线类似
// nsunderlinecolorattributename  设置下划线颜色,取值为 uicolor 对象,默认值为黑色
// nsstrokewidthattributename   设置笔画宽度,取值为 nsnumber 对象(整数),负值填充效果,正值中空效果
// nsstrokecolorattributename   填充部分颜色,不是字体颜色,取值为 uicolor 对象
// nsshadowattributename    设置阴影属性,取值为 nsshadow 对象
// nstexteffectattributename   设置文本特殊效果,取值为 nsstring 对象,目前只有图版印刷效果可用:
// nsbaselineoffsetattributename  设置基线偏移值,取值为 nsnumber (float),正值上偏,负值下偏
// nsobliquenessattributename   设置字形倾斜度,取值为 nsnumber (float),正值右倾,负值左倾
// nsexpansionattributename   设置文本横向拉伸属性,取值为 nsnumber (float),正值横向拉伸文本,负值横向压缩文本
// nswritingdirectionattributename 设置文字书写方向,从左向右书写或者从右向左书写
// nsverticalglyphformattributename 设置文字排版方向,取值为 nsnumber 对象(整数),0 表示横排文本,1 表示竖排文本
// nslinkattributename    设置链接属性,点击后调用浏览器打开指定url地址
// nsattachmentattributename   设置文本附件,取值为nstextattachment对象,常用于文字图片混排
// nsparagraphstyleattributename  设置文本段落排版格式,取值为 nsparagraphstyle 对象

格式&排版

上面属性的最后一个就是排版.需要去一nsmutableparagraphstyle的对象.直接上代码:

nsmutableparagraphstyle *paragraphstyle = [[nsmutableparagraphstyle alloc] init];
paragraphstyle.alignment = aligent;
paragraphstyle.linespacing = linespace; // 调整行间距
paragraphstyle.firstlineheadindent = firstlineheadindent;//首行缩进
nsrange range = nsmakerange(0, [string length]);
[attributedstring addattribute:nsparagraphstyleattributename value:paragraphstyle range:range];

我们再来认识一下nsmutableparagraphstyle的属性:

cgfloat linespacing;     // 字体的行间距 
cgfloat paragraphspacing;   // 段与段之间的间距 
nstextalignment alignment;   // (两端对齐的)文本对齐方式(左,中,右,两端对齐,自然) 
cgfloat firstlineheadindent;   // 首行缩进 
cgfloat headindent;     // 整体缩进(首行除外) 
cgfloat tailindent;     // 尾部缩进
nslinebreakmode linebreakmode;  // 结尾部分的内容以……方式省略
cgfloat minimumlineheight;   // 最低行高
cgfloat maximumlineheight;   // 最大行高 
nswritingdirection basewritingdirection; // 书写方向
cgfloat lineheightmultiple;   // 行间距多少倍
cgfloat paragraphspacingbefore;  // 段首行空白空
float hyphenationfactor;    // 连字属性 在ios,唯一支持的值分别为0和1 

设置了这么多的格式,进行了各种各样的排版那么怎么计算行高呢.

- (cgrect)boundingrectwithsize:(cgsize)size options:(nsstringdrawingoptions)options context:(nullable nsstringdrawingcontext *)context ns_available(10_11, 6_0);

是苹果推荐的计算方法,显然会遇到段落格式问题,例如行间距、缩进等格式设置需求,attributes传进来的字典中,包含我们设置的字体及格式,其中nsparagraphstyleattributename是设置段落风格,nsfontattributename是设置字体。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网