1. 实例化方法和使用方法

实例化方法:

使用字符串初始化

- (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;

  1. 常见的属性及说明
 NSFontAttributeName                       设置字体属性,默认值:字体:Helvetica(Neue) 字号:12
 NSForegroundColorAttributeNam       设置字体颜色,取值为 UIColor对象,默认值为黑色
 NSBackgroundColorAttributeName     设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色
 NSLigatureAttributeName                 设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符
 NSKernAttributeName                      设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
 NSStrikethroughStyleAttributeName  设置删除线,取值为 NSNumber 对象(整数)
 NSStrikethroughColorAttributeName  设置删除线颜色,取值为 UIColor 对象,默认值为黑色
 NSUnderlineStyleAttributeName        设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似
 NSUnderlineColorAttributeName        设置下划线颜色,取值为 UIColor 对象,默认值为黑色
 NSStrokeWidthAttributeName            设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
 NSStrokeColorAttributeName             填充部分颜色,不是字体颜色,取值为 UIColor 对象
 NSShadowAttributeName                  设置阴影属性,取值为 NSShadow 对象
 NSTextEffectAttributeName               设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用:
 NSBaselineOffsetAttributeName         设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
 NSObliquenessAttributeName            设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
 NSExpansionAttributeName               设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
 NSWritingDirectionAttributeName      设置文字书写方向,从左向右书写或者从右向左书写
 NSVerticalGlyphFormAttributeName   设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
 NSLinkAttributeName                       设置链接属性,点击后调用浏览器打开指定URL地址
 NSAttachmentAttributeName            设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
 NSParagraphStyleAttributeName      设置文本段落排版格式,取值为 NSParagraphStyle 对象

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设置富文本格式显示的更多相关文章

  1. UILabel设置富文本后不显示省略号

    先描述一下问题,项目中用到了UILabel去显示一段富文本文字,超过label显示区域部分,省略号处理. 但是当设置好 attributedText 给label之后,显示出的效果是文字被切割了,并没 ...

  2. UILabel的富文本显示选项

    UILabel的富文本格式设置 1.实例化方法和使用方法 实例化方法: 使用字符串初始化 - (id)initWithString:(NSString *)str; 例: NSMutableAttri ...

  3. 简易使用UILabel的富文本

    简易使用UILabel的富文本 使用效果: 源码: NSString+YX.h    NSString+YX.m // // NSString+YX.h // YXKit // // Copyrigh ...

  4. 在Excel中将数字设置成文本格式的技巧

    在Excel中将数字设置成文本格式的技巧 一个简单的方法,利用[数据]菜单的[分列]功能来将数字设置为文本格式.具体操作步骤为: 1.选中所有需要处理的数字单元格. 2.选择[数据]菜单[分列]功能. ...

  5. SQL Server 获取某时间点后修改的函数Function 并以文本格式显示

    修改查询分析器如下选项 右键=>查询选项 =>结果=>文本=> 取消 在结果集中包括列标题 的勾选 右键=>将结果保存到=> 选择 以文本格式显示结果 执行如下SQ ...

  6. PHP UEditor富文本编辑器 显示 后端配置项没有正常加载,上传插件不能正常使用

    UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码... 问题描述 我的编辑器在本地测试的时候没问 ...

  7. POI锁定列并设置Cell文本格式

    SXSSFWorkbook workbook = new SXSSFWorkbook(); Font font = workbook.createFont(); CellStyle style = w ...

  8. Layui之动态循环遍历出的富文本编辑器显示

    这篇记得是工作中的例子 描述: 平常的富文本显示都是根据静态的html获取id来显示,比如: <textarea class="layui-textarea" id=&quo ...

  9. Think PHP 完整的带富文本格式以及图片上传,并且在页面上分页展示

    Think php6.0官网网址:序言 · ThinkPHP6.0完全开发手册 · 看云 (kancloud.cn) 下面是基础配置 第一步:创建TP框架,命名为tp composer create- ...

随机推荐

  1. Jmeter连接SqlServer数据库进行压力测试

    Jmeter连接SqlServer数据库进行压力测试 前提准备:先安装jdbc驱动 驱动下载链接地址:http://pan.baidu.com/s/1bpDpjSr 密码:v6tn 下载解压之后,讲s ...

  2. 特性(Attributes)

    用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体相关联后,即可在运行时用反射技术查询特性. 例如,在一个方法前标注[Obsolete]特性,则调用该方法时VS则会提示该 ...

  3. html布局,左侧固定右侧自适应

    前几天看到我们的UI稿,要实现左侧固定树结构,右侧自适应.想着自己写过几次但是每次都会忘记,在这里做个笔记. 第一种方法: <!DOCTYPE html> <html lang=&q ...

  4. Request for the permission of type异常

    调用wcf调用的时候引发一个错误,错误信息如下: <Message>Request for the permission of type 'System.Configuration.Con ...

  5. 【Python之路】第四篇--Python基础之函数

    三元运算 三元运算(三目运算),是对简单的条件语句的缩写 # 书写格式 result = 值1 if 条件 else 值2 # 如果条件成立,那么将 “值1” 赋值给result变量,否则,将“值2” ...

  6. URL中文参数乱码的一个解决办法

      浏览器对有中文参数的url大部分都用utf-8编码,但我也曾经遇见过用GB2312编码的:如果遇见这样情况,那么接受到的参数就会出现乱码.乱码情况视服务器解码方式,Asp.Net网站一般默认为ut ...

  7. 万恶的tileMap

    先吐槽下.. 本来,我们准备用tileMap来做地图的,但发现一个问题,就是tileMap层中不能添加cc.Sprite,这导致了tileMap只适合做2D平面没有遮挡的游戏,并且主角是不能有效率的进 ...

  8. zTree 勾选checkbox

    zTree 勾选checkbox var setting = {    check: {        enable: true//        chkboxType : { "Y&quo ...

  9. Android;设置TextView加粗 代码设置

    我用过paint的那种方式,不好使. private void setTextBold(TextView textView) { //android中为textview动态设置字体为粗体 textVi ...

  10. LYF电子书制作工具(CHM格式)

    可以制作CHM电子书 可以制作电子相册 下载地址:http://files.cnblogs.com/files/blogLYF/LYF电子书制作安装包.zip http://files.cnblogs ...