tyattributedlabel--simple, powerful iOS property text control

Source: Internet
Author: User

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
    • Category and Protocol
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

    • Label and Storage
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;
3
4 // 长按代理 有多个状态 begin, changes, end 都会调用,所以需要判断状态
5 - (void)attributedLabel:(TYAttributedLabel *)attributedLabel textStorageLongPressed:(id<TYTextStorageProtocol>)textStorage onState:(UIGestureRecognizerState)state atPoint:(CGPoint)point;</span>
Examples
    • Appendstorage Demo
1 <span style="font-size: medium;">TYAttributedLabel *label = [[TYAttributedLabel alloc]init];
2 [self.view addSubview:label];
3
4 // 文字间隙
5 label.characterSpacing = 2;
6 // 文本行间隙
7 label.linesSpacing = 6;
8
9 NSString *text = @"/t总有一天你将破蛹而出,成长得比人们期待的还要美丽。/n";
10 [label appendText:text];
11
12 NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:text];
13 [attributedString addAttributeTextColor:[UIColor blueColor]];
14 [attributedString addAttributeFont:[UIFont systemFontOfSize:15]];
15 [label appendTextAttributedString:attributedString];
16
17 [label appendImageWithName:@"CYLoLi"size:CGSizeMake(CGRectGetWidth(label.frame), 180)];
18
19 UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"CYLoLi"]];
20 imageView.frame = CGRectMake(0, 0, CGRectGetWidth(label.frame), 180);
21 [label appendView:imageView];
22
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];
3
4 NSString *text = @"/t总有一天你将破蛹而出,成长得比人们期待的还要美丽。/n";
5 [label setText:text];
6
7 // 文字间隙
8 label.characterSpacing = 2;
9 // 文本行间隙
10 label.linesSpacing = 6;
11
12 textStorage = [[TYTextStorage alloc]init];
13 textStorage.range = ;
14 textStorage.textColor = RGB(0, 155, 0, 1);
15 textStorage.font = [UIFont systemFontOfSize:18];
16 [label addTextStorage:textStorage];
17
18 [label addLinkWithLinkData:@"www.baidu.com"range:NSMakeRange(5, 8)];
19
20 [label addImageWithName:@"haha"range:NSMakeRange(2, 1)];
21
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)];
25
26 [label sizeToFit];</span>
    • TextContainer Demo
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;
4     // 文字间隙
5 textContainer.characterSpacing = 2;
6 // 文本行间隙
7 textContainer.linesSpacing = 5;
8
9 textStorage = [[TYTextStorage alloc]init];
10 textStorage.range = ;
11 textStorage.textColor = RGB(0, 155, 0, 1);
12 textStorage.font = [UIFont systemFontOfSize:18];
13 [textContainer addTextStorage:textStorage];
14
15 [textContainer addLinkWithLinkData:@"www.baidu.com"range:NSMakeRange(5, 8)];
16
17 [textContainer addImageWithName:@"haha"range:NSMakeRange(2, 1)];
18
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)];
22
23 // 生成 textContainer 文本容器
24 [textContainer createTextContainerWithTextWidth:CGRectGetWidth(self.view.frame)];
25
26 TYAttributedLabel *label = [[TYAttributedLabel alloc]init];
27 label.textContainer = textContainer;
28
29 // 也可以 生成NSAttributedString 属性文本
30 //NSAttributedString *attString = [textContainer createAttributedString];
31 //label.attributedText = attString;
32
33 [label setFrameWithOrign:CGPointZero Width:CGRectGetWidth(self.view.frame)];
34 [self.view addSubView:label];</span>

tyattributedlabel--simple, powerful iOS property text control

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.