UILabel 富文本 显示文字和图片
来源:互联网 发布:网络实名制定义 编辑:程序博客网 时间:2024/04/29 04:33
// 在UILabel上显示文字和图片
//UIlabel 富文本 字体显示不同颜色
UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 320, 30)];
testLabel.backgroundColor = [UIColor lightGrayColor];
testLabel.textAlignment = NSTextAlignmentCenter;
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc] initWithString:@"今天天气不错呀"];
[AttributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20.0] range:NSMakeRange(2, 2)];
[AttributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2, 3)];
testLabel.attributedText = AttributedStr;
[self.view addSubview:testLabel];
//UIlabel 富文本 图文混排
UILabel *testLabel_2 =[[UILabel alloc]init];
testLabel_2.frame =CGRectMake(0, 100+50, 200, 100);
testLabel_2.backgroundColor =[UIColor redColor];
testLabel_2.textColor = [UIColor greenColor];
[self.view addSubview:testLabel_2];
NSString *str1 = @"我的";
NSString *str2 = @"图片";
// 创建一个富文本
NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@",str1,str2]];
// 修改富文本中的不同文字的样式
[attri addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, str1.length)];
[attri addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, str1.length)];
// 设置数字
[attri addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(str1.length, str2.length)];
[attri addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:NSMakeRange(str1.length, str2.length)];
// 创建一个放置图片的富文本
// 添加表情
NSTextAttachment *attch = [[NSTextAttachment alloc] init];
// 表情图片
attch.image = [UIImage imageNamed:@"biaoqian"];
// 设置图片大小
attch.bounds = CGRectMake(0, 0, 40, 40);
// 创建带有图片的富文本
NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
[attri insertAttributedString:string atIndex:3];
//UIlabel 富文本 字体显示不同颜色
UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 320, 30)];
testLabel.backgroundColor = [UIColor lightGrayColor];
testLabel.textAlignment = NSTextAlignmentCenter;
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc] initWithString:@"今天天气不错呀"];
[AttributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20.0] range:NSMakeRange(2, 2)];
[AttributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2, 3)];
testLabel.attributedText = AttributedStr;
[self.view addSubview:testLabel];
//UIlabel 富文本 图文混排
UILabel *testLabel_2 =[[UILabel alloc]init];
testLabel_2.frame =CGRectMake(0, 100+50, 200, 100);
testLabel_2.backgroundColor =[UIColor redColor];
testLabel_2.textColor = [UIColor greenColor];
[self.view addSubview:testLabel_2];
NSString *str1 = @"我的";
NSString *str2 = @"图片";
// 创建一个富文本
NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@",str1,str2]];
// 修改富文本中的不同文字的样式
[attri addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, str1.length)];
[attri addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, str1.length)];
// 设置数字
[attri addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(str1.length, str2.length)];
[attri addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:NSMakeRange(str1.length, str2.length)];
// 创建一个放置图片的富文本
// 添加表情
NSTextAttachment *attch = [[NSTextAttachment alloc] init];
// 表情图片
attch.image = [UIImage imageNamed:@"biaoqian"];
// 设置图片大小
attch.bounds = CGRectMake(0, 0, 40, 40);
// 创建带有图片的富文本
NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
[attri insertAttributedString:string atIndex:3];
testLabel_2.attributedText = attri;
如下图:
0 0
- UILabel 富文本 显示文字和图片
- UILabel显示富文本
- iOS 运用富文本在UILabel上显示图片
- 在UILabel中 显示文字和图片
- 解决TextView富文本显示时emoj或图片和文字不对齐的问题
- UILabel设置富文本格式显示
- iOS富文本编辑(在label里显示文字和图片)
- iOS富文本编辑(在label里显示文字和图片)
- iOS UILabel富文本
- 文字+图片的混编(富文本)
- 文字拼接图片+富文本操作
- iOS UILabel 显示带有html标签的富文本内容
- TextView文本显示图片和文字,竖直滚动显示
- iOS之UILabel和UITextView富文本操作
- UILabel展示html富文本
- UILabel 简单的富文本
- IOS之UILabel富文本
- 文字富文本化
- 插入排序
- Python:线程与GIL简述
- 嵌入式C编程经验 之 全局变量猛于虎
- web前端-CSS Padding(填充)-014
- 实现iOS图片等资源文件的热更新化(四): 一个最小化的补丁更新逻辑
- UILabel 富文本 显示文字和图片
- 基于Java的打包jar、war、ear包的作用与区别详解
- javascript高级模块化--require.js
- 直线段的扫描转换算法
- After Effects 帮助 / 混合模式和图层样式
- 学习笔记之ArcGIS Engine调用自定义GP服务(二)
- mysql 5.7以上版本安装配置方法图文教程(mysql 5.7.12\mysql 5.7.13\mysql 5.7.14)
- 设备模型之kobject,kset及其关系
- 符号-分节8(运算符的优先级下)