服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS富文本的使用方法示例详解

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

2021-04-08 17:34iOS_xuanhe IOS

这篇文章主要给大家介绍了关于iOS富文本的使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

前言

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

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

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

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

每月价格

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

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

先创建一个label:

?
1
2
3
4
5
6
7
8
9
-(uilabel *)pricelabel{
 if (_pricelabel == nil) {
 _pricelabel = [[uilabel alloc]init];
 _pricelabel.font = kfont(13);
 _pricelabel.textcolor = kcolortheme;
 _pricelabel.textalignment = nstextalignmentright;
 }
 return _pricelabel;
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
-(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,然后对"/"前面的文字进行设置.

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

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

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

我们依次实现一些功能

在指定位置添加图片

?
1
2
3
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)];

添加图片到指定的位置

?
1
2
3
4
5
6
7
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];

追加图片到最后一位

?
1
2
3
4
5
6
7
8
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];

设置中间位置文字为红色

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

综合写法

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

效果图:

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 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的对象.直接上代码:

?
1
2
3
4
5
6
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的属性:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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

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

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

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

总结

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

原文链接:http://www.jianshu.com/p/ae795de53308

延伸 · 阅读

精彩推荐
  • IOSxcode8提交ipa失败无法构建版本问题的解决方案

    xcode8提交ipa失败无法构建版本问题的解决方案

    xcode升级到xcode8后发现构建不了新的版本。怎么解决呢?下面小编给大家带来了xcode8提交ipa失败无法构建版本问题的解决方案,非常不错,一起看看吧...

    Cinna丶7542021-02-03
  • IOSiOS逆向教程之logify跟踪方法的调用

    iOS逆向教程之logify跟踪方法的调用

    这篇文章主要给大家介绍了关于iOS逆向教程之logify跟踪方法调用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学...

    Mr.Guo11472021-04-28
  • IOSIOS网络请求之AFNetWorking 3.x 使用详情

    IOS网络请求之AFNetWorking 3.x 使用详情

    本篇文章主要介绍了IOS网络请求之AFNetWorking 3.x 使用详情,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    总李写代码6892021-03-04
  • IOSiOS中时间与时间戳的相互转化实例代码

    iOS中时间与时间戳的相互转化实例代码

    这篇文章主要介绍了iOS中时间与时间戳的相互转化实例代码,非常具有实用价值,需要的朋友可以参考下。...

    张无忌!4812021-03-09
  • IOS谈一谈iOS单例模式

    谈一谈iOS单例模式

    这篇文章主要和大家谈一谈iOS中的单例模式,单例模式是一种常用的软件设计模式,想要深入了解iOS单例模式的朋友可以参考一下...

    彭盛凇11872021-01-19
  • IOSiOS常见的几个修饰词深入讲解

    iOS常见的几个修饰词深入讲解

    这篇文章主要给大家介绍了关于iOS常见的几个修饰词的相关资料,iOS修饰词包括assign、weak、strong、retain、copy、nonatomic、atomic、readonly、readwrite,文中通过示...

    郡王丶千夜7422021-05-10
  • IOSiOS APP实现微信H5支付示例总结

    iOS APP实现微信H5支付示例总结

    这篇文章主要介绍了iOS APP实现微信H5支付示例总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下...

    一张小A11332021-06-01
  • IOSiOS10 Xcode8适配7个常见问题汇总

    iOS10 Xcode8适配7个常见问题汇总

    这篇文章主要为大家详细汇总了iOS10 Xcode8适配7个常见问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    索马里猫10332021-02-01