TYAttributedLabel 简单,强大的属性文本的控件(无需了解CoreText),支持图文混排显示,支持添加链接,image和UIView控件,支持自定义排版显示
更新:
v2.4 修复imge放大bug,新增imageAlignment 和 autolayout支持,以及相应的demo,感谢xinzhengzhang,nonstriater
v2.3 新增 做题demo,代码优化(4s真机测试tableview列表非常流畅)
v2.2 新增 TYImagecache类,新增 image URL 下载缓存,功能优化,改进
v2.1 添加 tableViewCell demo, cell 滚动非常流畅
v2.0 重构优化代码,性能提升,稳定(已在项目中使用), 分离出TYTextContainer ,可以提前生成,也可以生成attributedString,显著提升cell滑动场景流畅度,可以和微博一样流畅
v1.2 添加设置行数,修复bug,增强稳定性
v1.1 添加链接高亮效果,链接便利方法,长按手势代理,优化代码
ScreenShot
新-做题demo
weibo demo 使用TYAttributedLabel 截图
Requirements
- Xcode 5 or higher
- Apple LLVM compiler
- iOS 6.0 or higher
- ARC
Features
- 支持富文本,图文混排显示,支持行间距 字间距,设置行数,自适应高度
- 支持添加高度自定义文本属性
- 支持添加属性文本,自定义链接,新增高亮效果显示(文字和背景)
- 支持添加UIImage和UIView控件
Demo
运行demo可以查看效果,而且在demo中,针对各种文本和图文的实现都有详细的用例,每个头文件中都有详细的用法注释,这里简单的介绍下用法
Usage
API Quickstart
Class | Function |
NSMutableAttributedString (TY) |
category提供便利color,font CharacterSpacing,UnderlineStyle,ParagraphStyle的属性添加,无需了解复杂的CoreText |
TYTextStorageProtocol |
自定义文本属性 遵守最基本的协议 即可 addTextStorage 添加进去 |
TYAppendTextStorageProtocol |
自定义文本属性协议 遵守即可appendTextStorage 添加进去 |
TYLinkStorageProtocol |
自定义文本链接属性 继承TYAppendTextStorageProtocol |
TYDrawStorageProtocol |
自定义显示内容协议 如 UIImage UIView |
下层协议继承上层的协议,如果觉得复杂,其实我已经实现了常用的自定义属性,拿来就可以用,或者继承,添加你想要的
Class | Function |
TYAttributedLabel |
简单易用的属性文本,富文本的显示控件,
addTextStorage在已经设置文本的基础上添加属性,image或者view,
appendTextStorage(无需事先设置文本)直接添加属性,image或者view到最后
|
TYTextContainer |
文本容器,可以提前生成,也可以生成attributedString,显著提升cell滚动流畅度 |
TYTextStorage |
自定义文本属性,支持textColor,font,underLineStyle |
TYLinkTextStorage |
自定义链接属性,继承TYTextStorage,支持点击代理 |
TYDrawStorage |
自定义显示内容属性,如UIImage,UIView,支持点击代理 |
TYImageStorage |
自定义图片显示,继承TYDrawStorage |
TYViewStorage |
自定义UIView控件,继承TYDrawStorage |
TYImageCache |
image缓存类,支持URL请求 |
如果需要更加详细的内容,请看各个头文件,有详细的注释
Delegate
1 |
<span style= "font-size: medium;" > |
2 |
- ( void )attributedLabel:(TYAttributedLabel *)attributedLabel textStorageClicked:(id<TYTextStorageProtocol>)textStorage atPoint:(CGPoint)point; |
5 |
- ( void )attributedLabel:(TYAttributedLabel *)attributedLabel textStorageLongPressed:(id<TYTextStorageProtocol>)textStorage onState:(UIGestureRecognizerState)state atPoint:(CGPoint)point;</span> |
Examples
1 |
<span style= "font-size: medium;" >TYAttributedLabel *label = [[TYAttributedLabel alloc]init]; |
2 |
[self.view addSubview:label]; |
5 |
label.characterSpacing = 2; |
7 |
label.linesSpacing = 6; |
9 |
NSString *text = @ "/t总有一天你将破蛹而出,成长得比人们期待的还要美丽。/n" ; |
10 |
[label appendText:text]; |
12 |
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:text]; |
13 |
[attributedString addAttributeTextColor:[UIColor blueColor]]; |
14 |
[attributedString addAttributeFont:[UIFont systemFontOfSize:15]]; |
15 |
[label appendTextAttributedString:attributedString]; |
17 |
[label appendImageWithName:@ "CYLoLi" size:CGSizeMake(CGRectGetWidth(label.frame), 180)]; |
19 |
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@ "CYLoLi" ]]; |
20 |
imageView.frame = CGRectMake(0, 0, CGRectGetWidth(label.frame), 180); |
21 |
[label appendView:imageView]; |
23 |
[label setFrameWithOrign:CGPointMake(0,0) Width:CGRectGetWidth(self.view.frame)];</span> |
addStorage demo
1 |
<span style= "font-size: medium;" >TYAttributedLabel *label = [[TYAttributedLabel alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 0)]; |
2 |
[self.view addSubview:label]; |
4 |
NSString *text = @ "/t总有一天你将破蛹而出,成长得比人们期待的还要美丽。/n" ; |
8 |
label.characterSpacing = 2; |
10 |
label.linesSpacing = 6; |
12 |
textStorage = [[TYTextStorage alloc]init]; |
14 |
textStorage.textColor = RGB(0, 155, 0, 1); |
15 |
textStorage.font = [UIFont systemFontOfSize:18]; |
16 |
[label addTextStorage:textStorage]; |
18 |
[label addLinkWithLinkData:@ "www.baidu.com" range:NSMakeRange(5, 8)]; |
20 |
[label addImageWithName:@ "haha" range:NSMakeRange(2, 1)]; |
22 |
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@ "CYLoLi" ]]; |
23 |
imageView.frame = CGRectMake(0, 0, CGRectGetWidth(label.frame), 180); |
24 |
[label addView:imageView range:NSMakeRange(16, 1)]; |
26 |
[label sizeToFit];</span> |
1 |
<span style= "font-size: medium;" >NSString *text = @ "/t总有一天你将破蛹而出,成长得比人们期待的还要美丽。/n" ; |
2 |
TYTextContainer *textContainer = [[TYTextContainer alloc]init]; |
3 |
textContainer.text = text; |
5 |
textContainer.characterSpacing = 2; |
7 |
textContainer.linesSpacing = 5; |
9 |
textStorage = [[TYTextStorage alloc]init]; |
11 |
textStorage.textColor = RGB(0, 155, 0, 1); |
12 |
textStorage.font = [UIFont systemFontOfSize:18]; |
13 |
[textContainer addTextStorage:textStorage]; |
15 |
[textContainer addLinkWithLinkData:@ "www.baidu.com" range:NSMakeRange(5, 8)]; |
17 |
[textContainer addImageWithName:@ "haha" range:NSMakeRange(2, 1)]; |
19 |
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@ "CYLoLi" ]]; |
20 |
imageView.frame = CGRectMake(0, 0, CGRectGetWidth(label.frame), 180); |
21 |
[textContainer addView:imageView range:NSMakeRange(16, 1)]; |
24 |
[textContainer createTextContainerWithTextWidth:CGRectGetWidth(self.view.frame)]; |
26 |
TYAttributedLabel *label = [[TYAttributedLabel alloc]init]; |
27 |
label.textContainer = textContainer; |
33 |
[label setFrameWithOrign:CGPointZero Width:CGRectGetWidth(self.view.frame)]; |
34 |
[self.view addSubView:label];</span> |