测试测试值 | ✓ |
语言语言 | Obj-CObjective C |
许可证 | MIT |
发布最新发布 | 2015年5月 |
由 TTTAttributedLabelVodafone 维护。
为支持属性、数据检测、链接等而设计的 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
协议支持语音控制。每个链接都可以单独选择,其 accessibilityLabel
等于其字符串值,对于 URL、电话号码和日期链接,还有一个相应的 accessibilityValue
。希望更改此行为或提供自定义值的开发者应创建子类并覆盖 accessibilityElements
。
tttattributedlabel
)建议使用 CocoaPods 来安装 TTTAttributedLabel
。只需将以下行添加到您的 Podfile
文件中
pod 'TTTAttributedLabel'
TTTAttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
label.font = [UIFont systemFontOfSize:14];
label.textColor = [UIColor darkGrayColor];
label.lineBreakMode = NSLineBreakByWordWrapping;
label.numberOfLines = 0;
// If you're using a simple `NSString` for your text,
// assign to the `text` property last so it can inherit other label properties.
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 APIs use C functions without a direct bridge to UIFont. See Apple's "Core Text Programming Guide" to learn how to configure string attributes.
UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:14];
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
if (font) {
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(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",大小为 14 英寸的 Helvetica,颜色为深灰色。
正常的 setText:
设置器既可以接受 NSString
,也可以接受 NSAttributedString
;在后一种情况下,将直接设置属性字符串,而不继承标签的基本样式。
除了支持富文本外,TTTAttributedLabel
还可以自动检测日期、地址、URL、电话号码、交通信息等链接,并允许您嵌入自己的链接。
label.enabledTextCheckingTypes = NSTextCheckingTypeLink; // Automatically detect links when the label text is subsequently changed
label.delegate = self; // Delegate methods are called when the user taps on a link (see `TTTAttributedLabelDelegate` protocol)
label.text = @"Fork me on GitHub! (http://github.com/mattt/TTTAttributedLabel/)"; // Repository URL will be automatically detected and linked
NSRange range = [label.text rangeOfString:@"me"];
[label addLinkToURL:[NSURL URLWithString:@"http://github.com/mattt/"] withRange:range]; // Embedding a custom link in a substring
pod try TTTAttributedLabel
...或克隆此存储库,在 Xcode 中构建并运行测试 Espressos
项目以查看 TTTAttributedLabel
的实际效果。如果您还没有安装 CocoaPods,请通过 [sudo] gem install cocoapods
安装它。
cd Example
pod install
open Espressos.xcworkspace
Mattt Thompson
TTTAttributedLabel
在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。