本文转载至 http://www.mobile-open.com/2015/86578.html

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

  • 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

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
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——简单,强大的iOS属性文本控件的更多相关文章

  1. WCF学习(二)对控件简单了解以及4个文本控件的简介

    WPF基础控件 系统默认提供的基础控件: 文本控件介绍与用法 Label控件 label控件:一般用户描述性文字显示. 在Label控件使用时,一般给予用户提示.用法上没有什么很特殊的,label控件 ...

  2. UILabel iOS添加文本控件

    UILabel这是iOS控制,这是UIView子类,只有在UIView文字显示功能的基础上加入.UILabel还查看课程和UIView类别似     //1.创建一个视图对象     //2.配置视图 ...

  3. Victor 串口 VCL 控件 - 简单实用, 功能强大的 C++ Builder 串口控件!

    源:Victor 串口 VCL 控件 - 简单实用, 功能强大的 C++ Builder 串口控件! 2014年02月06日发布控件的重要更新版本: Victor 串口控件 1.5.0.2 版本 (包 ...

  4. 重新想象 Windows 8.1 Store Apps (77) - 控件增强: 文本类控件的增强, 部分控件增加了 Header 属性和 HeaderTemplate 属性, 部分控件增加了 PlaceholderText 属性

    [源码下载] 重新想象 Windows 8.1 Store Apps (77) - 控件增强: 文本类控件的增强, 部分控件增加了 Header 属性和 HeaderTemplate 属性, 部分控件 ...

  5. wxPython中按钮、文本控件的简单运用

    本节学习图形用户界面 ------------------------ 本节介绍如何创建python程序的图形用户界面(GUI),也就是那些带有按钮和文本框的窗口.这里介绍wxPython : 下载地 ...

  6. 从0到1搭建移动App功能自动化测试平台(2):操作iOS应用的控件

    转自:http://debugtalk.com/post/build-app-automated-test-platform-from-0-to-1-Appium-interrogate-iOS-UI ...

  7. 重新想象 Windows 8 Store Apps (1) - 控件之文本控件: TextBlock, TextBox, PasswordBox, RichEditBox, RichTextBlock, RichTextBlockOverflow

    原文:重新想象 Windows 8 Store Apps (1) - 控件之文本控件: TextBlock, TextBox, PasswordBox, RichEditBox, RichTextBl ...

  8. 无比迅速敏捷地开发iOS超精美控件

    目录 前言 设计 编码 PaintCode 前言 自从人生第一篇博客<iOS中的预编译指令的初步探究>问世以来 浏览量竟然达到了360多,(路过的大神勿笑!)这些浏览量使我兴奋异常但又令我 ...

  9. iOS基础UI控件介绍-Swift版

    iOS基础UI控件总结 iOS基础控件包括以下几类: 1.继承自NSObject:(暂列为控件) UIColor //颜色 UIImage //图像 2.继承自UIView: 只能相应手势UIGest ...

随机推荐

  1. 模拟ajax实现网络爬虫——HtmlUnit

        最近在用Jsoup抓取某网站数据,可有些页面是ajax请求动态生成的,去群里问了一下,大神说模拟ajax请求即可.去网上搜索了一下,发现了这篇文章,拿过来先用着试试.    转帖如下: 网上关 ...

  2. mysql查询常用小语句

    mysql  查询某个库里表的数量 在mysql中有个数据库information_schema下的表tables记录了所有数据库中所有的表相关信息 TABLE_SCHEMA 数据库名称 SELECT ...

  3. (笔记)boa服务器make错误

    编译一个linux下的c系统,包含词法和语法分析模块,Linux上用bison和flex.yacc是一个文法分析器的生成器,bison即是yacc的GNU版本.Lex和YACC是用于构造词法分析机和语 ...

  4. vector 用法小例子

    1. vector<int> vec; vec.push_back(1); 2. vector<int> vec(10); vec[0] = 1;

  5. QIIME1 聚OTU

    qiime 本身不提供聚类的算法,它只是对其他聚otu软件的封装 根据聚类软件的算法,分成了3个方向: de novo:                   pick_de_novo_otus.py  ...

  6. vue的安装

    第一步:环境的搭建 : vue推荐开发环境: Node.js: javascript运行环境(runtime),不同系统直接运行各种编程语言(https://nodejs.org/zh-cn/down ...

  7. u3d 加载PNG做 UI图片

    using UnityEngine; using System.Collections; using System.IO; using UnityEngine.UI; public class UIT ...

  8. 配置Tomcat监听80端口 配置Tomcat虚拟主机 Tomcat日志

    配置Tomcat监听80端口 • vim /usr/local/tomcat/conf/server.xml Connector port=" protocol="HTTP/1.1 ...

  9. 基于HBase Hadoop 分布式集群环境下的MapReduce程序开发

    HBase分布式集群环境搭建成功后,连续4.5天实验客户端Map/Reduce程序开发,这方面的代码网上多得是,写个测试代码非常容易,可是真正运行起来可说是历经挫折.下面就是我最终调通并让程序在集群上 ...

  10. 【matlab】命令行窗口一直不停的输出ans=1?

    ans= 1 matlab里面的ans是一个系统量,当m文件中出现非赋值性的计算时,matlab会把结果自动存入ans. 如果m文件中每一个计算都是赋值的,即所计算的结果都是保存在自己设定的变量中的, ...