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- ...
随机推荐
- .net XML操作
一.常规介绍 //初始化一个xml对象 XmlDocument xml = new XmlDocument(); //加载xml文件 xml.Load("文件路径"); //读取指 ...
- hdu1031
#include <cstdio> #include <cstdlib> struct element{ int id; double sa; }e[10000]; int c ...
- LeetCode 395. Longest Substring with At Least K Repeating Characters C#
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- SQL Server函数格式
函数格式CREATE FUNCTION -- ============================================= -- Author: Clive Chinery -- Cre ...
- ogg 初始化
192.168.27.33test11ghdb11gtrandata: 同步delete,update 使用config 文件:同步表使用进程根据SCN号和RBA和主键同步##目的:数据定时同步,从源 ...
- python修炼4
---恢复内容开始--- 集合 建立 set() ={},集合没有顺序,由不可改变的数字 ,字符串,元组构成 #交集print(a&b) #a.intersection(b) #并集prin ...
- 在java中json的使用案例
import java.text.ParseException; import org.json.JSONArray; import org.json.JSONObject; public class ...
- python ast
import ast print ast.literal_eval('[1, 2, 3]')print eval("2 + 3 * len('hello')") == 17prin ...
- SpringMVC初步——HelloWorld的实现
开通博客园好几个月了,今天开始要用博客园记录自己的学习过程! 目录: 导包: 1. 配置web.xml文件的springDispatcherServlet 在xml中 alt+/ 找到springdi ...
- 2.安装中国版本的firefox
Linux刚安装好的时候,默认是火狐浏览器并且版本比较低,而且预装的是国际版 跟中国版无法同步,因为我windows上是中国版 首先去火狐主页,中文是http://www.firefox.com.cn ...