博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS开发--使用NSMutableAttributedString 实现富文本
阅读量:7250 次
发布时间:2019-06-29

本文共 2176 字,大约阅读时间需要 7 分钟。

在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求。之前在网上找了一些资料,有的是重绘UILabel的textLayer,有的是用html5实现的,都比较麻烦,而且很多UILabel的属性也不起作用了,效果都不理想。后来了解到NSMuttableAttstring(带属性的字符串),上面的一些需求都可以很简便的实现。

  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  字体

NSParagraphStyleAttributeName  段落格式 

NSForegroundColorAttributeName  字体颜色

NSBackgroundColorAttributeName   背景颜色

NSStrikethroughStyleAttributeName 删除线格式

NSUnderlineStyleAttributeName      下划线格式

NSStrokeColorAttributeName        删除线颜色

NSStrokeWidthAttributeName 删除线宽度

NSShadowAttributeName  阴影

更多方法和属性说明详见苹果官方说明文档:

  1.  使用实例

   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:16.0]

                        range:NSMakeRange(2, 2)];

  [AttributedStr addAttribute:NSForegroundColorAttributeName

                        value:[UIColor redColor]

                        range:NSMakeRange(2, 2)];

  testLabel.attributedText = AttributedStr;

  [self.view addSubview:testLabel];

运行效果:

 

转载地址:http://ieqbm.baihongyu.com/

你可能感兴趣的文章
iOS UIImageView(图片)
查看>>
可折叠显示的发光搜索表单
查看>>
PostgreSQL 10.1 手册_部分 II. SQL 语言_第 12 章 全文搜索_12.2. 表和索引
查看>>
java使用正则表达式判断手机号,固定电话,身份证,邮箱,url,车牌号,日期,ip地址,mac,人名等...
查看>>
新手也能轻松掌握的分布式系统「事务」技巧
查看>>
iOS开发之使用Git的基本使用(一)
查看>>
配置云存储网关在线服务支持多个互联VPC-高速通道版
查看>>
6个步骤从头开始编写机器学习算法:感知器案例研究
查看>>
NCalc 学习笔记 (三)
查看>>
NetBeans 成为 Apache 软件基金会顶级项目
查看>>
SSRF在Redis中反弹shell
查看>>
UML关系图
查看>>
SpringBoot 手写切片/面向切面编程
查看>>
动态 Web Server 技术发展历程
查看>>
使用pymysql(使用一)
查看>>
Redisson 3.10.6 发布,Redis 客户端
查看>>
日志框架 - 基于spring-boot - 使用入门
查看>>
用libtommath实现RSA算法
查看>>
基于POLARDB数据库的压测实践
查看>>
通过工具SecureCRTPortable将项目部署到服务器上
查看>>