TYAttributedLabel 簡單,強大的屬性文本控件

jopen 9年前發布 | 32K 次閱讀 iOS開發 移動開發 TYAttributedLabel

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

image

新-做題demo

image

weibo demo 使用TYAttributedLabel 截圖

image

Requirements

  • Xcode 5 or higher
  • Apple LLVM compiler
  • iOS 6.0 or higher
  • ARC

Features

  • 支持富文本,圖文混排顯示,支持行間距 字間距,設置行數,自適應高度
  • 支持添加高度自定義文本屬性
  • 支持添加屬性文本,自定義鏈接,新增高亮效果顯示(文字和背景)
  • 支持添加UIImage和UIView控件

Demo

運行demo可以查看效果,而且在demo中,針對各種文本和圖文的實現都有詳細的用例,每個頭文件中都有詳細的用法注釋,這里簡單的介紹下用法

Usage

API Quickstart

  • Category And Protocol
Class Function
NSMutableAttributedString (TY) category提供便利color,font CharacterSpacing,UnderlineStyle,ParagraphStyle的屬性添加,無需了解復雜的CoreText
TYTextStorageProtocol 自定義文本屬性 遵守最基本的協議 即可 addTextStorage 添加進去
TYAppendTextStorageProtocol 自定義文本屬性協議 遵守即可appendTextStorage 添加進去
TYLinkStorageProtocol 自定義文本鏈接屬性 繼承TYAppendTextStorageProtocol
TYDrawStorageProtocol 自定義顯示內容協議 如 UIImage UIView

下層協議繼承上層的協議,如果覺得復雜,其實我已經實現了常用的自定義屬性,拿來就可以用,或者繼承,添加你想要的

  • Label And Storage
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

// 點擊代理
- (void)attributedLabel:(TYAttributedLabel *)attributedLabel textStorageClicked:(id<TYTextStorageProtocol>)textStorage atPoint:(CGPoint)point;

// 長按代理 有多個狀態 begin, changes, end 都會調用,所以需要判斷狀態
- (void)attributedLabel:(TYAttributedLabel *)attributedLabel textStorageLongPressed:(id<TYTextStorageProtocol>)textStorage onState:(UIGestureRecognizerState)state atPoint:(CGPoint)point;

Examples

  • appendStorage demo
TYAttributedLabel *label = [[TYAttributedLabel alloc]init];
[self.view addSubview:label];

// 文字間隙
label.characterSpacing = 2;
// 文本行間隙
label.linesSpacing = 6;

NSString *text = @"\t總有一天你將破蛹而出,成長得比人們期待的還要美麗。\n";
[label appendText:text];

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:text];
[attributedString addAttributeTextColor:[UIColor blueColor]];
[attributedString addAttributeFont:[UIFont systemFontOfSize:15]];
[label appendTextAttributedString:attributedString];

[label appendImageWithName:@"CYLoLi" size:CGSizeMake(CGRectGetWidth(label.frame), 180)];

UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"CYLoLi"]];
imageView.frame = CGRectMake(0, 0, CGRectGetWidth(label.frame), 180);
[label appendView:imageView];

[label setFrameWithOrign:CGPointMake(0,0) Width:CGRectGetWidth(self.view.frame)];

addStorage demo
TYAttributedLabel *label = [[TYAttributedLabel alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 0)];
[self.view addSubview:label];

NSString *text = @"\t總有一天你將破蛹而出,成長得比人們期待的還要美麗。\n";
[label setText:text];

// 文字間隙
label.characterSpacing = 2;
// 文本行間隙
label.linesSpacing = 6;

textStorage = [[TYTextStorage alloc]init];
textStorage.range = [text rangeOfString:@"總有一天你將破蛹而出"]; 
textStorage.textColor = RGB(0, 155, 0, 1);
textStorage.font = [UIFont systemFontOfSize:18];
[label addTextStorage:textStorage];

[label addLinkWithLinkData:@"www.baidu.com" range:NSMakeRange(5, 8)];

[label addImageWithName:@"haha" range:NSMakeRange(2, 1)];

UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"CYLoLi"]];
imageView.frame = CGRectMake(0, 0, CGRectGetWidth(label.frame), 180);
[label addView:imageView range:NSMakeRange(16, 1)];

[label sizeToFit];

  • TextContainer demo
NSString *text = @"\t總有一天你將破蛹而出,成長得比人們期待的還要美麗。\n";
TYTextContainer *textContainer = [[TYTextContainer alloc]init];
    textContainer.text = text;
    // 文字間隙
textContainer.characterSpacing = 2;
// 文本行間隙
textContainer.linesSpacing = 5;

textStorage = [[TYTextStorage alloc]init];
textStorage.range = [text rangeOfString:@"總有一天你將破蛹而出"]; 
textStorage.textColor = RGB(0, 155, 0, 1);
textStorage.font = [UIFont systemFontOfSize:18];
[textContainer addTextStorage:textStorage];

[textContainer addLinkWithLinkData:@"www.baidu.com" range:NSMakeRange(5, 8)];

[textContainer addImageWithName:@"haha" range:NSMakeRange(2, 1)];

UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"CYLoLi"]];
imageView.frame = CGRectMake(0, 0, CGRectGetWidth(label.frame), 180);
[textContainer addView:imageView range:NSMakeRange(16, 1)];


// 生成 textContainer 文本容器
[textContainer createTextContainerWithTextWidth:CGRectGetWidth(self.view.frame)];

TYAttributedLabel *label = [[TYAttributedLabel alloc]init];
label.textContainer = textContainer;


// 也可以 生成NSAttributedString 屬性文本
//NSAttributedString *attString = [textContainer createAttributedString];
//label.attributedText = attString;

[label setFrameWithOrign:CGPointZero Width:CGRectGetWidth(self.view.frame)];
[self.view addSubView:label];

項目主頁:http://www.baiduhome.net/lib/view/home/1446475427888

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!