转自: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
从版本1.10.0,TTTAttributedLabel
通过UIAccessibilityElement
协议支持VoiceOver 。每个链接可以单独选择,accessibilityLabel
等于其字符串值,以及相应accessibilityValue
的URL,电话号码和日期链接。希望更改此行为或提供自定义值的开发人员应创建子类并覆盖accessibilityElements
。
tttattributedlabel
) CocoaPods是推荐的安装方法TTTAttributedLabel
。只需将以下行添加到您的Podfile
:
# Podfile
荚' TTTAttributedLabel “
TTTAttributedLabel
可以显示纯文本和属性文本:只需传递一个NSString
或NSAttributedString
到setText:
设置器。切勿分配到attributedText
酒店。
// NSAttributedString
TTTAttributedLabel * 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;
// NSString
TTTAttributedLabel * 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文件。