一、label的富文本属性

label.attributedText

需要注意一点:如果一个label设置了富文本这个属性,那它其他的设置都将失效。

二、富文本对象的创建

//初始化富文本对象的方法一:- (id)initWithString:(NSString *)str;
//初始化富文本对象的同时设置富文本对象的属性- (id)initWithString:(NSString *)str attributes:(NSDictionary *)attrs;

三、设置Label的富文本对象

方法一:创建富文本对象后逐个添加富文本对象的属性

    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 300, 80)];label.backgroundColor = [UIColor lightGrayColor];//初始化富文本对象:NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc] initWithString:@"我们都有一个家名字叫中国"];//给富文本添加属性1-字体大小[attributedStr addAttribute:NSFontAttributeNamevalue:[UIFont systemFontOfSize:16.0]range:NSMakeRange(2, 2)];//给富文本添加属性2-字体颜色[attributedStr addAttribute:NSForegroundColorAttributeNamevalue:[UIColor redColor]range:NSMakeRange(2, 2)];//给富文本添加属性3-下划线[attributedStr addAttribute:NSUnderlineStyleAttributeNamevalue:@(NSUnderlineStyleSingle)range:NSMakeRange(2, 2)];//设置label的富文本属性label.attributedText = attributedStr;[self.view addSubview:label];

方法二:创建富文本对象后统一设置富文本对象的属性

    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 300, 80)];label.backgroundColor = [UIColor lightGrayColor];//初始化富文本对象NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc] initWithString:@"我们都有一个家名字叫中国"];//富文本的属性通过字典的形式传入NSDictionary *attributeDict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16.0],NSFontAttributeName,//字体[UIColor redColor],NSForegroundColorAttributeName,//字体颜色[UIColor greenColor],NSBackgroundColorAttributeName,//字体背景色@(NSUnderlineStyleSingle),NSUnderlineStyleAttributeName,//下划线@(NSUnderlineStyleSingle),NSStrikethroughStyleAttributeName,//删除线[UIColor blueColor],NSUnderlineColorAttributeName,//下划线颜色[UIColor yellowColor],NSStrikethroughColorAttributeName,//删除线颜色[NSURL URLWithString:@"http://www.baidu.com"],NSLinkAttributeName,nil];//统一设置富文本对象的属性[attributedStr addAttributes:attributeDict range:NSMakeRange(2, 2)];//或者也可以使用下面的方法[attributedStr setAttributes:attributeDict range:NSMakeRange(4, 2)];//设置label的富文本属性label.attributedText = attributedStr;[self.view addSubview:label];

方法三:创建富文本对象的同时就添加属性

    //富文本的属性通过字典的形式传入NSDictionary *attributeDict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16.0],NSFontAttributeName,//字体大小[UIColor redColor],NSForegroundColorAttributeName,//字体颜色@(NSUnderlineStyleSingle),NSUnderlineStyleAttributeName,//下划线nil];//初始化富文本对象的同时设置富文本对象的属性NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc]initWithString:@"我们都有一个家名字叫中国" attributes:attributeDict];//设置label的富文本属性label.attributedText = attributedStr;[self.view addSubview:label];

PS: 同理一般有 add 方法就会有 remove 方法,下面是富文本对象的其它方法

- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;//添加一个属性
- (void)addAttributes:(NSDictionary<NSString *, id> *)attrs range:(NSRange)range;//添加一组属性
- (void)removeAttribute:(NSString *)name range:(NSRange)range;//移除一个属性

四、常见的属性及说明

NSFontAttributeName

字体

NSParagraphStyleAttributeName

段落格式

NSForegroundColorAttributeName

字体颜色

NSBackgroundColorAttributeName

背景颜色

NSStrikethroughStyleAttributeName

删除线格式

NSUnderlineStyleAttributeName

下划线格式

NSStrokeColorAttributeName

删除线颜色

NSStrokeWidthAttributeName

删除线宽度

NSShadowAttributeName

阴影

参考文章:

http://www.tuicool.com/articles/QZ3If2

http://my.oschina.net/u/2340880/blog/397500

http://www.itstrike.cn/Question/c688711b-0eba-465f-9880-35076f5ba1c2.html//关于link富文本 textView

http://www.bubuko.com/infodetail-382485.html

Label--关于Label富文本相关推荐

  1. ios html字符串 label,iOS UIlabel怎么加载html字符串 富文本的用法

    要加载html字符串,用人说,直接用webView啊!但是,有时候我们只需要显示2行文字,如此少的内容却要在复杂的UI排版中加入一个占用资源较多的webview,得不偿失.这里要说的是,我们其实可以用 ...

  2. ios label html图片,iOS UILabel与UITextView加载图片富文本点击看大图

    背景: 功能:回复列表 要求:界面按UI给的效果图 收到的数据:带各种标签的html格式的字符串(包括web端的表情图片) 如果只是想简单的加载HTML(包括图片),点击这里. 解决方案:UILabe ...

  3. jeecg富文本编辑器增加字体(仿宋)

    jeecg富文本编辑器增加字体(仿宋) 温馨提示:jeecg 提供了 uedit 富文本的实现,如下针对的是 uedit 增加仿宋字体示例. 主要修改三个文件:plug-in\ueditor\uedi ...

  4. UILabel 使用 标签,圆角,富文本

    2019独角兽企业重金招聘Python工程师标准>>> 继承关系:UIView : UIResponder : NSObject ///UILabel 显示的文本只读,无法编辑,可以 ...

  5. avue form提交变为不可编辑_教程42——富文本编辑器的原理(项目)

    完整版项目的代码链接: https://github.com/lyandzao/note/tree/master/react/react-tuts/react-admin/src​github.com ...

  6. html编辑器自定义脚本,CKeditor富文本编辑器使用技巧之添加自定义插件的方法

    本文实例讲述了CKeditor富文本编辑器使用技巧之添加自定义插件的方法.分享给大家供大家参考,具体如下: 首先就是在CKeditor的plugins目录下新建一个目录qchoice: qchoice ...

  7. Vue中使用vue-quil-editor富文本编辑器+el-upload实现带图片上传到SpringBoot后台接口

    场景 系统中经常会用到富文本编辑器,比如新增通知和公告功能,并且需要添加上传图片. vue-quill-editor官网: https://www.npmjs.com/package/vue-quil ...

  8. iOS 富文本类库RTLabel

    本文转载至 http://blog.csdn.net/duxinfeng2010/article/details/9004749  本节关于RTLable基本介绍,原文来自 https://githu ...

  9. layui富文本编译器添加图片

    1.创建富文本编辑器 <form class="layui-form" method="post" id="myForm" encty ...

最新文章

  1. CVPR2019--Binary Ensemble Neural Network--二值CNN网络集成
  2. 数据竞赛:第四届工业大数据竞赛-虚拟测量
  3. 论文多次被拒怎么办?Best Paper Award获得者聊聊如何才能中顶会
  4. CRMEB系统使用协议
  5. SpringBoot 2.x (12):整合Elasticsearch
  6. Timus Online Judge:ural:1006. Square Frames
  7. [渝粤教育] 郑州航空工业管理学院 航空概论 参考 资料
  8. Lucene.Net:关于索引的一些补充说明和总结
  9. java union pay 代码_Java标记了union / sum类型
  10. Java小 orm_这么优雅的Java ORM没见过吧!
  11. 【C语言】大程序(.c和.h)头文件和源文件
  12. 《从NLP反作弊技术看马蜂窝注水事件》笔记
  13. 混淆的概念:SIF、CIF、4CIF、D1
  14. 电子元器件之电容-2
  15. ip okhttp 设置_okhttp3及httpclient中的代理设置
  16. R语言实战Topsis综合评价法
  17. Python学习学期专业总结
  18. Paperreading——SCRDet Towards More Robust Detection for Small, Cluttered and Rotated Objects
  19. bp 神经网络 优点 不足_【学术论文】基于灰度共生矩阵和BP神经网络的乳腺肿瘤识别...
  20. (二十三)Kotlin简单易学 基础语法-什么是函数式编程

热门文章

  1. Python判断字符串包含子字符串(个数、索引、全部位置)
  2. 中国眼博会,2022第五届中国国际眼视光与视力矫正品牌展会
  3. 混合云是实现业务敏捷性的关键
  4. Fel表达式计算引擎学习 侵删
  5. HTML/CSS自制网页
  6. 前后端分离Nginx部署解决方案
  7. (Lx)Linux 实验
  8. 机器学习数据中类别变量(categorical variable)的处理方法
  9. 【微信小程序】实现广告轮播图
  10. php7 xdebug 性能,PHP 7 Xdebug 深深的坑