iOS - UILabel添加图片之富文本的简单应用
//创建富文本
NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@" -积分商城-热门商品-优惠券,后续会陆续打通东方明珠塔等各类上海文化消费优惠券。"];
//NSTextAttachment可以将要插入的图片作为特殊字符处理
NSTextAttachment *attch = [[NSTextAttachment alloc] init];
//定义图片内容及位置和大小
attch.image = [UIImage imageNamed:@"home_profile_icon"];
attch.bounds = CGRectMake(, , , );
//创建带有图片的富文本
NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch]; //将图片放在最后一位
//[attri appendAttributedString:string];
//将图片放在第一位
[attri insertAttributedString:string atIndex:];
//用label的attributedText属性来使用富文本
_imgLabel.attributedText = attri; //若想对图片添加点击事件,现在的想法是在label上添加一个透明按钮,位置大小跟图片的相同
_imgLabel.userInteractionEnabled = YES;
UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
clearBtn.frame = CGRectMake(, , attch.bounds.size.width, attch.bounds.size.height+);
clearBtn.backgroundColor = [UIColor clearColor];
[clearBtn addTarget:self action:@selector(alertSth) forControlEvents:UIControlEventTouchUpInside];
[_imgLabel addSubview:clearBtn];
iOS - UILabel添加图片之富文本的简单应用的更多相关文章
- UILabel添加图片之富文本的简单应用
若想对UILabel添加图片,那么就需要使用NSMutableAttributedString来定义先定义一个普通的label UILabel *lab = [[UILabel alloc]initW ...
- iOS UILabel UITextView UIButton 等等显示文本行间距
iOS UILabel UITextView UIButton 等等显示文本行间距都用如下方法 NSMutableParagraphStyle *paragraphStyle = [[NSMutab ...
- UEditor富文本编辑器简单使用
UEditor富文本编辑器简单使用 一.下载地址:https://ueditor.baidu.com/website/ 官网中并没有 python 版本的 UEditor 富文本编辑器,本文简单介绍 ...
- UILabel富文本 段落格式以及UILabel添加图片
之前文本整理有一点乱,这边重新整理一下,下面是效果图,一共两个UILabel, 富文本整理: /*NSForegroundColorAttributeName设置字体颜色,对象UIColor; NSP ...
- ios富文本的简单使用 AttributedString
富文本,顾名思义就是丰富的文本格式,本文demo使用NSMutableAttributedString //获取富文本 NSMutableAttributedString*attributeStrin ...
- iOS正则表达式的使用案例-富文本
富文本(正则表达式) 一.新建工程导入图片 二 实现方式一(缺点是只能解决一个图片的替换) _myLabel.font = [UIFont systemFontOfSize:15]; //@" ...
- Vue系列:wangEditor富文本编辑器简单例子
考虑到该富文本编辑器可能会在后续项目中继续使用,因此单独将其做成一个组件,把wangeditor作为组件的形式使用. 以下是参考代码 子组件部分: 父组件引用子组件: 以上就是 wangEditor ...
- iOS开发--使用NSMutableAttributedString 实现富文本
在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘UILabel的textLayer,有的是用html5实现的,都比较麻烦 ...
- UILabel 添加图片
//设置显示图片 NSMutableAttributedString * cellAttributeStr = [[NSMutableAttributedString alloc]initWithSt ...
随机推荐
- iOS开发-获取属性和方法
iOS开发数据存储有两种方式,属性列表和对象编码,属性列表可以通过NSArray,NSMutableArray,NSMutableDictionary,存储对象我们可以通过归档和解档来完成.如果我们想 ...
- js获取客户端time,cookie,url,ip,refer,user_agent信息:
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script> <script type=& ...
- 添加JavaDoc
使用javadoc比较容易生成文档,命令如下: javadoc -d doc -sourcepath src/main/java/ -subpackages com -encoding UTF-8 - ...
- c# 序列化BinaryFormatter、SoapFormatter和XmlSerializer的区别
在C#中常见的序列化的方法主要也有三个:BinaryFormatter.SoapFormatter.XML序列化 1.BinaryFormatter 序列化 [Serializable] //如果要想 ...
- ZOJ 3827 Information Entropy 水
水 Information Entropy Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge Informati ...
- 【Windows】字符串处理
1.截取字符串 举例说明其基本功能: @echo off set ifo=abcdefghijklmnopqrstuvwxyz0123456789 echo 原字符串(第二行为各字符的序号): ech ...
- Spark2.3 HA集群的分布式安装
一.下载Spark安装包 1.从官网下载 http://spark.apache.org/downloads.html 2.从微软的镜像站下载 http://mirrors.hust.edu.cn/a ...
- 云服务器 - 安装zookeeper单机环境
由于电脑中病毒,被人当做肉鸡挖矿了...所以很多东西都要重装, 之前没有写过zk如何安装,所以安装的时候顺便来记录一下分享出来 第一步上传zk安装包到服务器,如图: 解压: tar -zxvf zoo ...
- 深入研究 Runloop 与线程保活
深入研究 Runloop 与线程保活 在讨论 runloop 相关的文章,以及分析 AFNetworking(2.x) 源码的文章中,我们经常会看到关于利用 runloop 进行线程保活的分析,但如果 ...
- IOS开发之Core Location
IOS 支持三种检测当前位置的方式:手机基站.Wi-Fi.和GPS,其中GPS是经度最高的,同时也是最耗费手机电量的.一般情况下在室内是无法通过GPS获 取位置信息的,通过Wi-Fi获取位置的原理是通 ...