iOS文本布局探讨之三——使用TextKit框架进行富文本布局

iOS文本布局探讨之三——使用TextKit框架进行富文本布局

1、引言

        关于图文混排,其实之前的博客已经讨论不少,在实际开发中,常常使用第三方的框架来完成排版的需求,其中RCLabel和RTLabel是两个比较好用的第三方库,他们的实现都是基于UIView的,经过更底层的CoreText相关API来进行图文处理。相关介绍博客地址以下:数组

iOS中支持HTML标签渲染的MDHTMLLaebl:http://my.oschina.net/u/2340880/blog/703254网络

扩展于RCLabel的支持异步加载网络图片的富文本引擎的设计:http://my.oschina.net/u/2340880/blog/499311app

iOS开发封装一个能够响应超连接的label——基于RCLabel的交互扩展:http://my.oschina.net/u/2340880/blog/550194框架

2、原生UILabel真的只能渲染文字么?

        CoreText是一个比较底层且十分强大的文本渲染框架,可是其使用起来并非十分方便。在较低版本的iOS系统中,要进行富文本排版十分困难。在iOS6中,系统为UILabel,UITextView等这类文本渲染控件引入了NSAttributedString属性,有了NSAttributedString这个类,建立灵活多彩的文本控件变得十分轻松,开发者只须要配置NSAttributedString属性字符串便可。可是要进行图文混排,依然比较困难。iOS7以后引入TextKit框架,就完美的解决了图文混排这样的问题。异步

        首先,iOS7中新添加了一类NSTextAttachment,从类名理解它是一个文本附件,其实也正是如此,NSTextAttachment类能够向文本中添加一些附件,这有些向邮件系统,寄信者能够向邮件中添加附件一同发送出去。NSTextAttachment类并不直接参与富文本的渲染与布局,渲染和布局依然由NSAttributedString类来完成,NSAttributedString类中提供了方法将NSTextAttachment所描述的内容转换为NSAttributedString示例。以一个简单的图文混排为例:布局

- (void)viewDidLoad {
    [super viewDidLoad];
    //进行NSTextAttachment的建立
    NSTextAttachment * attach = [[NSTextAttachment  alloc]init];
    //设置显示的图片
    attach.image =[UIImage imageNamed:@"image"];
    //设置尺寸
    attach.bounds = CGRectMake(0, 0, 120, 60);
    NSTextAttachment * attach2 = [[NSTextAttachment  alloc]init];
    attach2.image =[UIImage imageNamed:@"image2"];
    attach2.bounds = CGRectMake(0, 0, 100, 90);
    //建立文本NSAttributedString对象
    NSMutableAttributedString * attri = [[NSMutableAttributedString alloc]initWithString:@"Describes a dictionary that fully specifies a font.... UIFontDescriptorInherits From NSObject UIFontDescriptor NSObject UIFontDescriptor Conforms To CVarArgT... 这里是中文"];
    //将NSTextAttachment映射为NSAttributedString对象
    NSMutableAttributedString * att = [[NSMutableAttributedString alloc]initWithAttributedString:[NSAttributedString attributedStringWithAttachment:attach]];
    //将图片插入NSAttributedString中
    [attri insertAttributedString:att atIndex:15];
    [attri insertAttributedString:[NSAttributedString attributedStringWithAttachment:attach2] atIndex:130];
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 280, 540)];
    label.backgroundColor = [UIColor grayColor];
    label.numberOfLines = 0;
    label.attributedText = attri;
    [self.view addSubview:label];
}

运行工程后,效果以下图所示,其实只使用UILabel也能够实现复杂的富文本和图文混排:url

3、为富文本附件添加用户交互能力

        TextKit框架强大到只使用UILabel就能够完成复杂的富文本布局,可是UILabel有一个致命的缺陷,其没法进行用户交互。试想,若是能够向一段文本中添加任意数据类型的文件,当用户点击这个文件时,能够获取到文件数据并进行业务逻辑处理,这将十分酷。这样富文本布局其实就不仅局限于图文混排了,咱们能够插入音频,插入视频,甚至插入任意自定义格式的数据。结合使用NSTextAttachment与UITextView,这些都能实现。先看NSTextAttachment类中的一些经常使用属性与方法:spa

//这个初始化方法用于建立携带任意数据的文本附件
- (instancetype)initWithData:(nullable NSData *)contentData ofType:(nullable NSString *)uti NS_DESIGNATED_INITIALIZER NS_AVAILABLE(10_11, 7_0);
//携带的数据内容
@property(nullable, copy, NS_NONATOMIC_IOSONLY) NSData *contents NS_AVAILABLE(10_11, 7_0);
//数据类型
@property(nullable, copy, NS_NONATOMIC_IOSONLY) NSString *fileType NS_AVAILABLE(10_11, 7_0);

//设置渲染的图片 须要注意 若是设置的这个 附件携带的数据 fileWrapper目录内容将无效
@property(nullable, strong, NS_NONATOMIC_IOSONLY) UIImage *image NS_AVAILABLE(10_11, 7_0);
//设置图片渲染的尺寸
@property(NS_NONATOMIC_IOSONLY) CGRect bounds NS_AVAILABLE(10_11, 7_0);

//设置附件携带的文件目录 须要注意 若是设置了这个属性 image和data将无效
@property(nullable, strong, NS_NONATOMIC_IOSONLY) NSFileWrapper *fileWrapper;

结合UITextView能够为NSAttributedString属性字符串添加超连接,在代码回调中监听此超连接的回调能够获取NSTextAttachment携带的附件内容,如此就能够自由的进行业务处理了,示例代码以下:.net

- (void)viewDidLoad {
    [super viewDidLoad];
    //保留一个数组存放附件
    _attArray = [NSMutableArray array];
    //建立附件数据
    NSData * stringData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image3" ofType:@"gif"]];
    NSTextAttachment * attach = [[NSTextAttachment  alloc]initWithData:stringData ofType:@"gif"];
    [_attArray addObject:attach];
    attach.bounds = CGRectMake(0, 0, 30, 40);
    NSMutableAttributedString * attri = [[NSMutableAttributedString alloc]initWithString:@"Describes a dictionary that fully specifies a font.... UIFontDescriptorInherits From NSObject UIFontDescriptor NSObject UIFontDescriptor Conforms To CVarArgT... 这里是中文"];
    NSMutableAttributedString * att = [[NSMutableAttributedString alloc]initWithAttributedString:[NSAttributedString attributedStringWithAttachment:attach]];
    //为NSTextAttachment转换为的NSAttributedString添加超连接
    [att addAttributes:@{NSLinkAttributeName:@"url..."} range:NSMakeRange(0, att.string.length)];
    [attri insertAttributedString:att atIndex:15];
    UITextView * textView = [[UITextView alloc]initWithFrame:CGRectMake(20, 20, 280, 540)];
    textView.backgroundColor = [UIColor grayColor];
    textView.dataDetectorTypes = UIDataDetectorTypeLink;
    textView.delegate =self;
    textView.attributedText = attri;
    textView.editable =    NO;
    [self.view addSubview:textView];
}

实现以下的TextView代理方法:设计

-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
    //能够获取到url 进行匹配
    NSLog(@"%@",URL);
    //取出NSTextAttachment附件
    NSTextAttachment * attach =_attArray.firstObject;
    NSLog(@"%@--",attach.contents);
    return YES;
}

向文本中添加任意数据的NSTextAttachment会展示一个文件的图标,以下图所示:

当用户点击文件图标时,会将携带的gif文件数据进行打印。

 

专一技术,热爱生活,交流技术,也作朋友。

——珲少 QQ群:203317592

相关文章
相关标签/搜索