this article reprinted to http://www.mobile-open.com/2015/86578.html
Tyattributedlabel simple, powerful property text controls (no need to understand Coretext), support for graphics and graphic display, support for adding links, image and UIView controls, support for custom typography display
Update:
v2.4 Repair Imge Amplification bug, add imagealignment and AutoLayout support, and the corresponding demo, thanks Xinzhengzhang,nonstriater
v2.3 new demo, code optimization (4s real machine test TableView list is very smooth)
v2.2 new Tyimagecache class, new image URL download cache, feature optimization, improved
v2.1 Add Tableviewcell demo, cell scrolling is very smooth
v2.0 refactoring optimization code, performance improvement, stability (already used in the project), separation of Tytextcontainer, can be generated in advance, can also generate attributedstring, significantly improve the cell sliding scene smoothness, can be as smooth as Weibo
v1.2 add set number of lines, fix bugs, enhance stability
v1.1 Add link highlighting effect, link convenience method, long press gesture proxy, optimize code
Screenshot
New-Do Demo
Weibo demo using Tyattributedlabel
Requirements
- Xcode 5 or higher
- Apple LLVM Compiler
- IOS 6.0 or higher
- ARC
Features
- Support Rich text, graphic mixed display, support line spacing word spacing, set number of lines, adaptive height
- Support for adding height custom text properties
- Support for adding attribute text, custom links, new highlight display (text and background)
- Support for adding uiimage and UIView controls
Demo
Run the demo can see the effect, and in the demo, for a variety of text and graphics implementation has detailed use cases, each header file has a detailed usage notes, here is a brief introduction to the use of
Usageapi Quickstart
| Class |
Function |
| Nsmutableattributedstring (TY) |
Category provides convenient Color,font Characterspacing,underlinestyle,paragraphstyle property additions without the need to understand complex coretext |
| Tytextstorageprotocol |
Custom Text Properties adhere to the most basic protocol to addtextstorage add in |
| Tyappendtextstorageprotocol |
Custom Text Property Protocol compliance can be appendtextstorage added in |
| Tylinkstorageprotocol |
Custom text Link Property inheritance Tyappendtextstorageprotocol |
| Tydrawstorageprotocol |
Custom display content protocol such as UIImage UIView |
The lower level protocol inherits the upper layer of the Protocol, if it is complicated, I have already implemented the common custom properties, can be used, or inherit, add you want to
| Class |
Function |
| Tyattributedlabel |
Easy-to-use property text, Rich text display controls,
Addtextstorage adds an attribute, image or view, based on the text already set,
Appendtextstorage (no need to set text beforehand) to add attributes directly, image or view to the last |
| Tytextcontainer |
Text container, can be generated in advance, can also generate attributedstring, significantly improve cell scrolling smoothness |
| Tytextstorage |
Custom text properties, support Textcolor,font,underlinestyle |
| Tylinktextstorage |
Custom link Properties, inherit tytextstorage, support click Proxy |
| Tydrawstorage |
Custom display content properties, such as Uiimage,uiview, support for click-through proxy |
| Tyimagestorage |
Custom picture display, inheriting tydrawstorage |
| Tyviewstorage |
Customizing UIView controls, inheriting Tydrawstorage |
| Tyimagecache |
Image cache class, support URL request |
If you need more detailed information, please see each header file, with detailed comments
Delegate
1 |
<span style="font-size: medium;">// 点击代理 |
2 |
- (void)attributedLabel:(TYAttributedLabel *)attributedLabel textStorageClicked:(id<TYTextStorageProtocol>)textStorage atPoint:(CGPoint)point; |
4 |
// 长按代理 有多个状态 begin, changes, end 都会调用,所以需要判断状态 |
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> |
View Source code Printing Help
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)]; |
23 |
// 生成 textContainer 文本容器 |
24 |
[textContainer createTextContainerWithTextWidth:CGRectGetWidth(self.view.frame)]; |
26 |
TYAttributedLabel *label = [[TYAttributedLabel alloc]init]; |
27 |
label.textContainer = textContainer; |
29 |
// 也可以 生成NSAttributedString 属性文本 |
30 |
//NSAttributedString *attString = [textContainer createAttributedString]; |
31 |
//label.attributedText = attString; |
33 |
[label setFrameWithOrign:CGPointZero Width:CGRectGetWidth(self.view.frame)]; |
34 |
[self.view addSubView:label];</span> |
tyattributedlabel--simple, powerful iOS property text control