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 ...
随机推荐
- pip UnicodeDecodeError: 'ascii' codec can't decode byte
http://blog.csdn.net/computerms/article/details/48056103
- SQLAlchemy模型使用
SQLAchemy模型使用 简介: SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是:将对象转换成SQL,然后使用 ...
- Salesforce 使用Js 调用Webservice实例
1,创建 Custom Button 在页面上 2, 创建CustomJs 代码调用Webservice <!--参数名区分大小写,对于跨层object直接在Object名后直接加参字段名即可- ...
- Spring使用非applicationContext.xm 默认名的配置文件的配置
Spring默认的配置文件是applicationContext.xml,但是有些时候,希望拆分Spring的配置文件,让其单一化,每一个都只进行自己的配置,如图所示 那么就需要在web.xml中配置 ...
- centos,nginx安装备忘
# ssh 登录慢解决 vim /etc/ssh/sshd_config UseDNS no # add limitsvi /etc/security/limits.conf* soft nproc ...
- Postgresql-xl 调研
Postgresql-xl 调研 来历 这个项目的背后是一家叫做stormDB的公司.整个代买基于postgres-xc.开源版本应该是stormdb的一个分支. In 2010, NTT's Ope ...
- PowerDesigner 逆向中 Name和Comment互换
在使用PowerDesigner对数据库进行概念模型和物理模型设计时,一般在NAME或Comment中写中文,在Code中写英文.Name用来显 示,Code在代码中使用,但Comment中的文字会保 ...
- 错误C4146的解决方法
error C4146: 一元负运算符应用于无符号类型,结果仍为无符号类型: 那么什么情况下会遇见这种错误呢,例如下代码: 错误代码1:int number = -2147483648; //erro ...
- asp.net core 阿里云消息服务(Message Service,原MQS)发送接口的实现
最近在后台处理订单统计等相关功能用到了大力的mqs,由于官方没有实现asp.net core的sdk,这里简单实现了发送信息的功能,有兴趣的可以参考实现其他相关功能 using System;usin ...
- php curl get
function curlGet($url){ $ch = curl_init(); $header = "Accept-Charset: utf-8"; curl_setopt( ...