UILabel添加图片之富文本的简单应用

若想对UILabel添加图片,那么就需要使用NSMutableAttributedString来定义
先定义一个普通的label

UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, self.view.frame.size.width-10, 100)];
lab.numberOfLines = 0;
[self.view addSubview:lab];

然后对其定义

//创建富文本
NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@"   我纳斯达克市场部撒草卡死你查看售楼处内 按时打算打算的撒打算离开的骄傲是是大神快了解到撒开了就对啦可视对讲卢卡斯的卡洛斯的骄傲"];
//NSTextAttachment可以将要插入的图片作为特殊字符处理
NSTextAttachment *attch = [[NSTextAttachment alloc] init];
//定义图片内容及位置和大小
attch.image = [UIImage imageNamed:@"tab_suning"];
attch.bounds = CGRectMake(0, 0, 61, 14);
//创建带有图片的富文本
NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
//将图片放在最后一位
//[attri appendAttributedString:string];
//将图片放在第一位
[attri insertAttributedString:string atIndex:0];
//用label的attributedText属性来使用富文本
lab.attributedText = attri;

然后效果如下

若想对图片添加点击事件,现在的想法是在label上添加一个透明按钮,位置大小跟图片的相同

lab.userInteractionEnabled = YES;
UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
clearBtn.frame = CGRectMake(0, 12, attch.bounds.size.width, attch.bounds.size.height+3);
clearBtn.backgroundColor = [UIColor clearColor];
[clearBtn addTarget:self action:@selector(alertSth) forControlEvents:UIControlEventTouchUpInside];
[lab addSubview:clearBtn];

效果如下

时间: 2024-08-27 16:34:15

UILabel添加图片之富文本的简单应用的相关文章

富文本内容简单的的增删改查

由于html本身的textarea标签的文本编辑功能较为简单,不能设置文字的样式,因此需要富文本控件来增强textarea的功能.       一些常见的富文本控件有:UEditor.kindeditor.simditor.bootstrap-wysiwyg.wangEditor.CKEditor.tinymce,各有优缺点,网上也有对不介绍,不再赘述. 此处选用tinymce,因其兼容性较好,插入页面也较为简单,此外还有丰富的插件可以扩展功能. 首先,在页面上使用tinymce:1.引入js文

ios富文本的简单使用 AttributedString

富文本,顾名思义就是丰富的文本格式,本文demo使用NSMutableAttributedString //获取富文本    NSMutableAttributedString*attributeString_atts=[[NSMutableAttributedString alloc]initWithString:string];    //背景色    UIColor  *backgroundColor=[UIColor whiteColor];    //字体    UIFont *fon

NSMutableAttributedString(富文本)的简单使用

#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 84, self.view.frame.size.width-40, self.view.fr

BBS(仿博客园系统)项目05(后台管理功能实现:文章添加、富文本编辑器使用、xss攻击、BeautifulSoup4模块、富文本编辑器上传图片、修改头像)

摘要 布局框架搭建 随笔添加 后台管理富文本编辑器KindEditor xss攻击 文章简介的截取,BeautifulSoup4模块 富文本编辑器上传图片 头像修改 一.后台管理框架布局搭建 后台管理布局框架分析:导航条.左侧功能区.右侧主要功能显示和实现区 实现: 导航条:使用bootstrap模板:JavaScript>>导航条 左侧:使用bootstrap模板:组件>>列表组 右侧:使用bootstrap模板:JavaScript>>标签页 新建后台管理路由(注意

ios中label富文本的设置

1.修改不同文字和颜色 // 创建一个富文本 NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@"哈哈哈哈哈123456789"]; // 修改富文本中的不同文字的样式 [attri addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 5

iOS UILable 文字添加图片 (文字前面,中间,后面添加图)

1,实例化一个UILable 2, // 创建一个富文本 NSMutableAttributedString *attri =     [[NSMutableAttributedString alloc] initWithString:@"超时罚款24元/小时"]; NSTextAttachment *attch = [[NSTextAttachment alloc] init]; // 表情图片 attch.image = [UIImage imageNamed:@"par

label中添加图片

创建NSTextAttachment的对象,用来装在图片 将NSTextAttachment对象的image属性设置为想要使用的图片 设置NSTextAttachment对象bounds大小,也就是要显示的图片的大小 用[NSAttributedString attributedStringWithAttachment:attch]方法,将图片添加到富文本上 //创建富文本字符串 NSMutableAttributedString *mAString = [[NSMutableAttribute

属性字符串添加图片

- (NSMutableAttributedString *)getAttributedStringWithSting:(NSString *)astring image:(UIImage *)image index:(NSInteger)index{        // 1.创建一个富文本    NSMutableAttributedString *attri =  [[NSMutableAttributedString alloc] initWithString:astring];    /

django项目中使用KindEditor富文本编辑器

先从官网下载插件,放在static文件下 前端引入 <script type="text/javascript" src="/static/back/kindeditor/kindeditor-all.js"></script> <script> KindEditor.ready(function (K) { window.editor = K.create('#content', { {# 加上这句话可以使jquery能获取到富