[转] iOS使用NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)

摘要:
我以前在网上找到过一些资料。其中一些重绘了UILabel的textLayer,而其中一些是用html5实现的,这很麻烦。此外,许多UILabel属性不起作用,效果也不理想。

转自:

在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  阴影

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

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableAttributedString_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40003689

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

运行效果:

 

另外,其他可以设置text 的控件(如UIButton,UITextField)也都有该属性,该文章不够详细,只是简单介绍,其他效果的实现参考API中更多的属性及使用方法。

免责声明:文章转载自《[转] iOS使用NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Docker的Jenkins Pipeline工作流C# 对sharepoint 列表基本操纵下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Fiddler—APP抓包详解(十三)

第一步:Android手机代理设置: 手机和fiddler位于同一个局域网内: 1.首先从fiddler处获取到ip地址和端口号: ,点击online,最后一行就是ip地址 ,可以获知端口号为8888,同时将Allow remote computers to connet勾选上,允许远程设备连接 2.手机上填写代理: 代理主机名处填写刚才获取到的ip地址...

iOS定位元素

最近在做IOS自动化测试,IOS的Appium环境都配置OK,执行起来真的慢,慢到怀疑人生,那么今天就来总结一下IOS定位方式和各个定位方式的速度排序。 据我观察,按查找元素的顺序速度,从快到慢的顺序如下: ios_predicate >> accessibility_id >> class_name >>xpath...

iOS 添加启动图片

之前添加启动图片,一直都是通过添加LaunchImage来实现,见链接 http://www.cnblogs.com/jys509/p/4856068.html 这种方法,就需要给每个尺寸添加图片。 在Xcode 7 之后,建议使用LaunchScreen.storyboard,因为这种,只需要提供两张图片就行了。 背景图片只需要符合16:9就可以了,...

移动三“.NET研究”国己然鼎立,普​通开发者如何选择开发​平台? 狼人:

  Nokia和Microsoft 终于搞基成功,业内人事普遍都看好两家厂商的“软硬结合”。这一结局直接把Windows Phone 7 这一新兴手机移动操作系统送到与如日中天的 iPhone 与 Android 在同一位置上,变成了三国之势。究竟两家公司的合作是 " No - Win " 还是 "Win - Kia " 这里不做讨论,只能引用我们瓷器国的...

C++读写txt

1、File* (1) 写入txt void WriteTXT() { std::string filepath; FILE* file = fopen(filepath.c_str(), "wt+"); if (file) { std::string str = "test"; std::...

iOS 推送问题全解答《十万个为啥吖?》

Q 1:为啥收不到推送(1)? 如果收到推送时,App 在前台运行,那么: iOS 10 before 顶部横幅不会弹出。没有任何展示,你以为「没有收到推送」。 iOS 10 after 如果没有实现以下代码,也是不会有任何提示的,你以为「没有收到推送」。 //UNUserNotificationCenterDelegate -(void)userNo...