若想对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添加图片之富文本的简单应用的更多相关文章

  1. iOS - UILabel添加图片之富文本的简单应用

    //创建富文本 NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@" ...

  2. UEditor富文本编辑器简单使用

    UEditor富文本编辑器简单使用 一.下载地址:https://ueditor.baidu.com/website/ 官网中并没有 python 版本的 UEditor 富文本编辑器,本文简单介绍 ...

  3. UILabel富文本 段落格式以及UILabel添加图片

    之前文本整理有一点乱,这边重新整理一下,下面是效果图,一共两个UILabel, 富文本整理: /*NSForegroundColorAttributeName设置字体颜色,对象UIColor; NSP ...

  4. ios富文本的简单使用 AttributedString

    富文本,顾名思义就是丰富的文本格式,本文demo使用NSMutableAttributedString //获取富文本 NSMutableAttributedString*attributeStrin ...

  5. Vue系列:wangEditor富文本编辑器简单例子

    考虑到该富文本编辑器可能会在后续项目中继续使用,因此单独将其做成一个组件,把wangeditor作为组件的形式使用. 以下是参考代码 子组件部分: 父组件引用子组件: 以上就是 wangEditor ...

  6. UILabel 添加图片

    //设置显示图片 NSMutableAttributedString * cellAttributeStr = [[NSMutableAttributedString alloc]initWithSt ...

  7. NSMutableAttributedString(富文本)的简单使用

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  8. summernote富文本的简单使用

    官方地址:https://summernote.org/ html代码 <div class="summernote" id="summernote" & ...

  9. iOS开发富文本制作 图片和文字/NSMutableParagraphStyle/NSMutableAttributedString

    /NSMutableParagraphStyle/NSMutableAttributedString 组合使 NSString * titlestr=@"日产GT-R"; NSMu ...

随机推荐

  1. ios调用本地拨打电话,发送短信

    电话.短信是手机的基础功能,iOS中提供了接口,让我们调用.这篇文章简单的介绍一下iOS的打电话.发短信在程序中怎么调用. 1.打电话   [[UIApplication sharedApplicat ...

  2. 仿Spring读取配置文件实现方案

    一.读取config和jar里的配置文件,以流的方式返回 /** * 根据路径,获取当前项目中路径(jar包和config文件)下的配置文件 * @param filePath 存放job配置信息文件 ...

  3. Redis安装手册

    转载请注明出处:http://www.cnblogs.com/robinjava77/p/5465146.html (Robin) 1)下载redis:wget http://download.red ...

  4. hibernate 左链接查询

    select pro from Provide as pro left join pro.labels as la left join pro.city as c where 1=1

  5. asp.net扩展名不显示并获得URL值

    由vs2013新建"web窗体应用程序"的网站,URL不显示扩展名.那今天就实现该功能 1.首先打开vs2013>新建项目>如下图选择 2.选择项目右击>管理Nu ...

  6. webApi上传下载文件

    上传文件通过webApi html端调用时包含(form提交包含 enctype="multipart/form-data",才可以启作用获取到文件) public class U ...

  7. C语言Notebook

    int *pointer=NULL    /*指针变量一定要赋初值*/pritnf("Pointer' address is:%p",&pointer);  /*打印指针变 ...

  8. 从不同方面寻找bug

    从小学生的角度,为了熟能生巧,更好掌握知识,那就需要不断的练习,然而app没有重新开始的功能,要退出再重新进入,很麻烦.还有,如果小学生只想做一类的计算,例如,只想做减法或乘法,那么它就不能满足. 我 ...

  9. ubuntu和win7 64双系统 安装

    我是thinkpad机器 主要参考到是http://www.weste.net/2012/4-29/82173.html这个地址到教程,表示感谢 这边说下安装好以后到环境设置 首先要 sudo apt ...

  10. CountDownLatch如何使用

    正如每个Java文档所描述的那样,CountDownLatch是一个同步工具类,它允许一个或多个线程一直等待,直到其他线程的操作执行完后再执行.在Java并发中,countdownlatch的概念是一 ...