UILabel添加图片之富文本的简单应用
若想对UILabel添加图片,那么就需要使用NSMutableAttributedString来定义
先定义一个普通的label
UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width-, )];
lab.numberOfLines = ;
[self.view addSubview:lab];
然后对其定义
//创建富文本
NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@" 我纳斯达克市场部撒草卡死你查看售楼处内 按时打算打算的撒打算离开的骄傲是是大神快了解到撒开了就对啦可视对讲卢卡斯的卡洛斯的骄傲"];
//NSTextAttachment可以将要插入的图片作为特殊字符处理
NSTextAttachment *attch = [[NSTextAttachment alloc] init];
//定义图片内容及位置和大小
attch.image = [UIImage imageNamed:@"tab_suning"];
attch.bounds = CGRectMake(, , , );
//创建带有图片的富文本
NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
//将图片放在最后一位
//[attri appendAttributedString:string];
//将图片放在第一位
[attri insertAttributedString:string atIndex:];
//用label的attributedText属性来使用富文本
lab.attributedText = attri;
然后效果如下

若想对图片添加点击事件,现在的想法是在label上添加一个透明按钮,位置大小跟图片的相同
lab.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];
[lab addSubview:clearBtn];
效果如下

UILabel添加图片之富文本的简单应用的更多相关文章
- iOS - UILabel添加图片之富文本的简单应用
//创建富文本 NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@" ...
- UEditor富文本编辑器简单使用
UEditor富文本编辑器简单使用 一.下载地址:https://ueditor.baidu.com/website/ 官网中并没有 python 版本的 UEditor 富文本编辑器,本文简单介绍 ...
- UILabel富文本 段落格式以及UILabel添加图片
之前文本整理有一点乱,这边重新整理一下,下面是效果图,一共两个UILabel, 富文本整理: /*NSForegroundColorAttributeName设置字体颜色,对象UIColor; NSP ...
- ios富文本的简单使用 AttributedString
富文本,顾名思义就是丰富的文本格式,本文demo使用NSMutableAttributedString //获取富文本 NSMutableAttributedString*attributeStrin ...
- Vue系列:wangEditor富文本编辑器简单例子
考虑到该富文本编辑器可能会在后续项目中继续使用,因此单独将其做成一个组件,把wangeditor作为组件的形式使用. 以下是参考代码 子组件部分: 父组件引用子组件: 以上就是 wangEditor ...
- UILabel 添加图片
//设置显示图片 NSMutableAttributedString * cellAttributeStr = [[NSMutableAttributedString alloc]initWithSt ...
- NSMutableAttributedString(富文本)的简单使用
#import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...
- summernote富文本的简单使用
官方地址:https://summernote.org/ html代码 <div class="summernote" id="summernote" & ...
- iOS开发富文本制作 图片和文字/NSMutableParagraphStyle/NSMutableAttributedString
/NSMutableParagraphStyle/NSMutableAttributedString 组合使 NSString * titlestr=@"日产GT-R"; NSMu ...
随机推荐
- 500lines项目简介
"500行或更少" "What I cannot create, I do not understand." -- Richard Feynman <50 ...
- Oracle和SQLServer解锁杀进程
ORACLE:在平时工作中经常会遇到数据库死锁的情况,以前使用oracle时候(那时候还不懂),出现这种情况时前辈给了我一段命令:SELECT dob.OBJECT_NAME Table_Name,l ...
- php 判断文件或目录是否存在
判断文件或目录是否存在有自带的函数 file_exists:文件是否存在 $file = "check.txt"; if(file_exists($file)) { ech ...
- [转]:Delphi中Format的字符串格式化使用说明
一.Format函数的用法 Format是一个很常用,却又似乎很烦的方法,本人试图对这个方法的帮助进行一些翻译,让它有一个完整的概貌,以供大家查询之用: 首先看它的声明: function Forma ...
- js 判断IOS版本号
先来观察 iOS 的 User-Agent 串: Phone 4.3.2 系统:Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; e ...
- 用systemd脚本自动启动node js程序
vi /usr/lib/systemd/system/pano.service [Unit] Description=pano - main site of site_name.com Documen ...
- The source attachment does not contain the source for the file ActionSupport.class 错误
报错 Syntax error, insert ";" to complete FieldDeclaration 报错 The source attachment does ...
- SQL Server提高事务复制效率优化(二)快照初始化优化
测试数据表量1500w+,使用初始化默认的快照代理参数,复制的三个过程包括快照初始化,订阅初始化和数据修改复制,主要对快照代理.分发代理.日志读取代理分别作了参数优化,并给出优化前后的对照实验测试. ...
- input 放大镜
<input results="s" type="search" size="20px" placeholder="搜 ...
- jmc远程监控java服务
简介 JMC(Java mission control)是JDK自带的一个图形界面监控工具,监控信息非常全面.他的安装目录在%JAVA_HOME%\bin\jmc.exe 最近JSTORM程序在集群环 ...