UILabel设置富文本格式显示
- 实例化方法和使用方法
实例化方法:
使用字符串初始化
- (id)initWithString:(NSString *)str;
例:
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"今天天气不错呀"];
- (id)initWithString:(NSString *)str attributes:(NSDictionary *)attrs;
字典中存放一些属性名和属性值,如:
NSDictionary *attributeDict = [NSDictionarydictionaryWithObjectsAndKeys:
[UIFontsystemFontOfSize:15.0],NSFontAttributeName,
[UIColorredColor],NSForegroundColorAttributeName,
NSUnderlineStyleAttributeName,NSUnderlineStyleSingle,nil];
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"今天天气不错呀" attributes:attributeDict];
- (id)initWithAttributedString:(NSAttributedString *)attester;
使用NSAttributedString初始化,跟NSMutableString,NSString类似
使用方法:
为某一范围内文字设置多个属性
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;
为某一范围内文字添加某个属性
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
为某一范围内文字添加多个属性
- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;
移除某范围内的某个属性
- (void)removeAttribute:(NSString *)name range:(NSRange)range;
- 常见的属性及说明
2.实例设置文本的属性
//从网络获取的数组中拿到字典
NSDictionary *dataDic = @{@"Position":@"苏州",
@"TrueName":@"苏州***信息科技有限公司"};
//变换字体大小及颜色positionstr拼接设置一定的格式如(),{},[]……
NSString *positionStr = [NSString stringWithFormat:@"(%@)",dataDic[@"Position"]];
NSString *str = [NSString stringWithFormat:@"%@%@",dataDic[@"TrueName"],positionStr];
//初始化
NSMutableAttributedString *mutableStr = [[NSMutableAttributedString alloc]initWithString:str];
//限定范围
NSRange trueNameRange = [str rangeOfString:dataDic[@"TrueName"]];
NSRange positionRange = [str rangeOfString:positionStr];
//设置范围属性
[mutableStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:trueNameRange];
[mutableStr addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:positionRange];
[mutableStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:16] range:trueNameRange];
[mutableStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:13] range:positionRange];
//取出获得的数据,直接给label
nameLabel.attributedText = mutableStr;
具体的效果图,可以自己测试之后看到
UILabel设置富文本格式显示的更多相关文章
- UILabel设置富文本后不显示省略号
先描述一下问题,项目中用到了UILabel去显示一段富文本文字,超过label显示区域部分,省略号处理. 但是当设置好 attributedText 给label之后,显示出的效果是文字被切割了,并没 ...
- UILabel的富文本显示选项
UILabel的富文本格式设置 1.实例化方法和使用方法 实例化方法: 使用字符串初始化 - (id)initWithString:(NSString *)str; 例: NSMutableAttri ...
- 简易使用UILabel的富文本
简易使用UILabel的富文本 使用效果: 源码: NSString+YX.h NSString+YX.m // // NSString+YX.h // YXKit // // Copyrigh ...
- 在Excel中将数字设置成文本格式的技巧
在Excel中将数字设置成文本格式的技巧 一个简单的方法,利用[数据]菜单的[分列]功能来将数字设置为文本格式.具体操作步骤为: 1.选中所有需要处理的数字单元格. 2.选择[数据]菜单[分列]功能. ...
- SQL Server 获取某时间点后修改的函数Function 并以文本格式显示
修改查询分析器如下选项 右键=>查询选项 =>结果=>文本=> 取消 在结果集中包括列标题 的勾选 右键=>将结果保存到=> 选择 以文本格式显示结果 执行如下SQ ...
- PHP UEditor富文本编辑器 显示 后端配置项没有正常加载,上传插件不能正常使用
UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码... 问题描述 我的编辑器在本地测试的时候没问 ...
- POI锁定列并设置Cell文本格式
SXSSFWorkbook workbook = new SXSSFWorkbook(); Font font = workbook.createFont(); CellStyle style = w ...
- Layui之动态循环遍历出的富文本编辑器显示
这篇记得是工作中的例子 描述: 平常的富文本显示都是根据静态的html获取id来显示,比如: <textarea class="layui-textarea" id=&quo ...
- Think PHP 完整的带富文本格式以及图片上传,并且在页面上分页展示
Think php6.0官网网址:序言 · ThinkPHP6.0完全开发手册 · 看云 (kancloud.cn) 下面是基础配置 第一步:创建TP框架,命名为tp composer create- ...
随机推荐
- ECS活动真实IP (前端存在SLB)
log_format main 'realip:$http_x_forwarded_for slbip:$remote_addr-$remote_user [$time_local] "$r ...
- BZOJ 1303 【CQOI2009】中位数图
baidu了一下bzoj水题列表...找到这道题. 题目大意:给定一个数t,在给定的一段包含1-n的序列中找出多少个长度为奇数子序列的中位数为t. 第一眼没看数据范围,于是开心的打了一个O(n^3 ...
- CodeForces 710B Optimal Point on a Line
递推. 先对$a[i]$进行从小到大排序. 然后计算出每个点左边所有点到这个点的距离之和$L[i]$,以及右边每个点到这个点的距离之和$R[i]$. 这两个都可以递推得到. $L\left[ i \r ...
- uoj 55 紫荆花之恋 动态点分治+替罪羊式重构+treap
每插入一个点,直接把它当做重心插入原树,当做是动态点分树一样维护 但这样深度会越来越大,所以我们用类似替罪羊的方法 当树失去平衡时,对子树进行一次点分,保证复杂度 #include <cstdi ...
- js获取计算的样式(非行间样式)
function getStyle(obj, attr){ if(obj.currentStyle){ style = obj.currentStyle[attr]; //兼容IE8以下 }else{ ...
- iOS Plugins
iOS Plugins This section provides details for how to implement native plugin code on the iOS platfor ...
- lnmp一键安装包配置laravel项目
laravel一键安装包:https://lnmp.org/install.html 在server中加入 location / { try_files $uri $uri/ /index.php?$ ...
- 函数:atexit
函数说明: 1.原型:int atexit (void (*func)(void)); 2.功能:注册退出main函数之后将要被执行的函数: 3.参考:http://www.cplusplus.com ...
- Hibernate配置详细解释
hibernate.cfg.xml <!--标准的XML文件的起始行,version='1.0'表明XML的版本,encoding='gb2312'表明XML文件的编码方式--> < ...
- Output\TEST.sct(7): error: L6236E: No section matches selector - no section to be FIRST/LAST.
点击错误信息,跳转到了一个.sct文件:*.o (RESET, +First) 按照如下操作,也不能解决问题.对比别的工程,也没找出问题. "操作是: Options for Target ...