富文本(TTTAttributedLabel是一个替代,UILabel提供了一种简单的方式来高效地呈现属性字符串。作为奖励,它还支持链接嵌入,自动与NSTextCheckingTypes手动通过指定)
来源:互联网 发布:mac市场增长率 编辑:程序博客网 时间:2024/06/05 16:22
转自:https://github.com/TTTAttributedLabel/TTTAttributedLabel
一个下拉更换为UILabel
支持的属性,数据检测器,链接等
TTTAttributedLabel
是一个替代,UILabel
提供了一种简单的方式来高效地呈现属性字符串。作为奖励,它还支持链接嵌入,自动与NSTextCheckingTypes
手动通过指定URL,地址,电话号码,事件或公共交通信息的范围。
即使在iOS 6中UILabel
收到支持NSAttributedString
,TTTAttributedLabel
有几个独特的功能:
- 自动数据检测
- 手动链接嵌入
- 标记属性字符串的样式继承
- 标签中链接的自定义样式
- 长按手势除了点击手势的链接
它还包括高级段落样式属性:
attributedTruncationToken
firstLineIndent
highlightedShadowRadius
highlightedShadowOffset
highlightedShadowColor
lineHeightMultiple
lineSpacing
minimumLineHeight
maximumLineHeight
shadowRadius
textInsets
verticalAlignment
要求
- iOS 8+ / tvOS 9+
- Xcode 7+
辅助功能
从版本1.10.0,TTTAttributedLabel
通过UIAccessibilityElement
协议支持VoiceOver 。每个链接可以单独选择,accessibilityLabel
等于其字符串值,以及相应accessibilityValue
的URL,电话号码和日期链接。希望更改此行为或提供自定义值的开发人员应创建子类并覆盖accessibilityElements
。
通讯
- 如果需要帮助,请使用Stack Overflow。(Tag
tttattributedlabel
) - 如果你想问一个一般的问题,使用Stack Overflow。
- 如果您发现错误,请打开一个问题。
- 如果您有功能请求,请打开问题。
- 如果您想贡献,请提交拉取请求。
安装
CocoaPods是推荐的安装方法TTTAttributedLabel
。只需将以下行添加到您的Podfile
:
# Podfile荚' TTTAttributedLabel “
用法
TTTAttributedLabel
可以显示纯文本和属性文本:只需传递一个NSString
或NSAttributedString
到setText:
设置器。切勿分配到attributedText
酒店。
// NSAttributedStringTTTAttributedLabel * attributedLabel = [[TTTAttributedLabel alloc ] initWithFrame:CGRectZero ];NSAttributedString * attString = [[ NSAttributedString alloc ] initWithString:@“ Tom Bombadil ” 属性: @ { (id)kCTForegroundColorAttributeName:(id)[UIColor redColor ]。CGColor, NSFontAttributeName:[UIFont boldSystemFontOfSize:16 ], NSKernAttributeName:[ NSNull null ], (id)kTTTBackgroundFillColorAttributeName:(id)[UIColor greenColor ]。CGColor}];//直接设置属性字符串,而不继承标签的任何其他文本//属性。attributLabel.text = attString;
// NSStringTTTAttributedLabel * label = [[TTTAttributedLabel alloc ] initWithFrame:CGRectZero ];label.font = [UIFont systemFontOfSize:14 ];label.textColor = [UIColor darkGrayColor ];label.lineBreakMode = NSLineBreakByWordWrapping ;label.numberOfLines = 0 ;//如果你为你的文本使用一个简单的NSString,//最后分配给`text`属性,所以它可以继承其他标签属性。NSString * text = @“ Lorem ipsum dolor sit amet ” ;[label setText: text afterInheritingLabelAttributesAndConfiguringWithBlock: ^ NSMutableAttributedString *(NSMutableAttributedString * mutableAttributedString){ NSRange boldRange = [[mutableAttributedString string ] rangeOfString:@“ ipsum dolor ” options:NSCaseInsensitiveSearch ]; NSRange strikeRange = [[mutableAttributedString string ] rangeOfString:@“ sit amet ” options:NSCaseInsensitiveSearch ]; // Core Text API使用C函数,而不需要直接连接到UIFont。请参阅Apple的“核心文本编程指南”,了解如何配置字符串属性。 UIFont * boldSystemFont = [UIFont boldSystemFontOfSize:14 ]; CTFontRef字体= CTFontCreateWithName((__bridge CFStringRef)boldSystemFont。的fontName,boldSystemFont。的pointsize,NULL); if(font){ [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range: boldRange]; [mutableAttributedString addAttribute:kTTTStrikeOutAttributeName value: @ YES range: strikeRange]; CFrelease(font); }} return mutableAttributedString;}];
首先,我们创建和配置标签,与实例化的方式相同UILabel
。在使用-setText:afterInheritingLabelAttributesAndConfiguringWithBlock:
方法时,在标签上设置的任何文本属性都将继承为基本属性。在该示例中,子串“ipsum dolar”将以粗体显示,使得标签将读作“Lorem ipsum dolar sit amet”,大小为14Helvetica,具有深灰色。
IBDesignable
TTTAttributedLabel
包含IBInspectable
和IB_DESIGNABLE
注释以在Interface Builder中配置标签。但是,如果您在建立时看到这些警告...
IB Designables: Failed to update auto layout status: Failed to load designables from path (null)IB Designables: Failed to render instance of TTTAttributedLabel: Failed to load designables from path (null)
...那么你可能使用TTTAttributedLabel
作为静态库,它不支持IB注释。一些解决方法包括:
- 安装
TTTAttributedLabel
与使用的CocoaPods一个动态的框架use_frameworks!
在你的Podfile
,或者与迦太基 - 安装
TTTAttributedLabel
它的源文件拖放到你的项目
链接和数据检测
除了支持富文本,TTTAttributedLabel
还可以自动检测日期,地址,网址,电话号码,公交信息的链接,并允许您嵌入自己的链接。
label.enabledTextCheckingTypes = NSTextCheckingTypeLink ; //当标签文本随后更改时自动检测链接 label.delegate = self; //当用户点击链接时调用委托方法(参见`TTTAttributedLabelDelegate`协议)label.text = @“ Fork me on GitHub!(https://github.com/mattt/TTTAttedLabel/)” ; //存储库URL将被自动检测和链接NSRange range = [label.text rangeOfString:@“ me ” ];[label addLinkToURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://hotgood-detail?id=%@",model.roomId]
withRange: range]; //在子字符串中嵌入自定义链接
执行代理方法
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url{
if ([url.schemeisEqualToString:@"http"]) {
NSNumber *vId = (NSNumber*)[url queryParam:@"id"];
if([url.hostisEqualToString:@"hotgood-detail"]){
//跳转页面
}else {
//跳转页面
}
}
演示
pod test TTTAttributedLabel
...或克隆此repo并构建和运行/测试Espressos
项目在Xcode看到TTTAttributedLabel
在行动。如果你没有的CocoaPods安装,以抓住它[sudo] gem install cocoapods
。
cd示例pod安装打开Espressos.xcworkspace
执照
TTTAttributedLabel
根据MIT许可证提供。有关详细信息,请参阅LICENSE文件。
- 富文本(TTTAttributedLabel是一个替代,UILabel提供了一种简单的方式来高效地呈现属性字符串。作为奖励,它还支持链接嵌入,自动与NSTextCheckingTypes手动通过指定)
- UILabel 简单的富文本
- 富文本(TYAttributedLabel 简单,强大的属性文本控件(无需了解CoreText),支持图文混排显示,支持添加链接,image和UIView控件,支持自定义排版显示)
- prototype是javascript中的函数(function)的一个保留属性,并且它的值是一个对象,通过以此函数作为构造函数构造出来的对象都自动的拥有构造函数的prototype对象的成员属性和
- 手动地通过命令行的方式创建一个数据库
- TCP是通过什么方式来提供可靠传输的
- 富文本(属性字符串)
- iOS UIlabel怎么加载html字符串 富文本的用法
- 属性字符串(富文本)的使用
- 富文本带属性的字符串NSMutableAttributedString
- 前不久,微软发布了其网络硬盘的正式版本:Windows Live Skydrive。它面向公共用户免费提供5G空间,最大单个文件支持到50M,还提供个人文件夹。微软的东西,当然会让人更放心一些,而且最重要的是它提供永久存储服务,还提供超链接!
- 实例中的__class_属性链接到了它的类,而类的__bases__属性,是一个元组,包含了通往更高的超类的链接
- 随手笔记 UIlabel 简单富文本
- 源码变换 超文本标记语言(即HTML),是用于描述网页文档的一种标记语言。 HTML通过文本来描述文档显示出来应该具有的“样子”。它主要通过标签来定义对象的显示属性或行为。
- UILabel的改进版 TTTAttributedLabel
- 链式编程,iOS简单封装属性字符串,实现富文本.
- 在iOS中UILabel作为一个文本标签,在实现程序中几乎每个页面都会用到UILabel,UILabel的常用属性:textColor(文本颜色),text(文本值),font(文本字体),textA
- Oracle DBA之手动地通过命令行的方式创建一个数据库
- android系统的日志系统分层与logcat使用
- 剑指offer经典编程(十六)
- tcp协议
- C++成员变量的初始化顺序 标签: C++成员变量初始化顺序 2017-02-22 23:44 101人阅读 评论(0) 收藏 举报 分类: C++程序设计浅尝(46) 原文地址:http://w
- PHP的类自动加载机制
- 富文本(TTTAttributedLabel是一个替代,UILabel提供了一种简单的方式来高效地呈现属性字符串。作为奖励,它还支持链接嵌入,自动与NSTextCheckingTypes手动通过指定)
- Go语言 IDE之Gogland配置使用
- Cow Contest [最短路][floyd]
- html基础学习
- Mac下pm2启用进程,mocha测试
- 点击“下一页”页面异常报错问题,原因之一
- 【技巧】css之html样式能用 外部样式不能用
- Android手机遥控器解决方案
- 最大子序列和问题