一个简单易用的 iOS 7 及以上版本 UILabel 替代品,可突出显示链接(如网址、Twitter 风格的用户名和标签),并使其可触摸。
KILabel 没有任何特殊依赖,只需将 KILabel/Source 中的文件包含到您的项目中,然后使用 KILabel 类代替 UILabel。
因为也提供了 Podspec,所以可以通过在项目的 Podfile 中添加以下行来使用 KILabel。
pod 'KILabel', '1.0.0'
您还可以使用 Swift 来使用 KILabel。只需将文件编译进您的 XCode 项目中的常规方式,但需要在Objective-C 桥接头中添加以下行。
#import "KILabel.h"
下方的代码片段展示了如何设置一个带有触摸处理块的标签。一个更完整的示例可以在存储库中包含的 KILabelDemo 项目中找到。
// Create the label, you can do this in Interface Builder as well
KILabel *label = [[KILabel alloc] initWithFrame:NSRectMake(20, 64, 280, 60)];
label.text = @"Follow @krelborn or visit http://compiledcreations.com #shamelessplug";
// Attach a block to be called when the user taps a user handle
label.userHandleLinkTapHandler = ^(KILabel *label, NSString *string, NSRange range) {
NSLog(@"User tapped %@", string);
};
// Attach a block to be called when the user taps a hashtag
label.hashtagLinkTapHandler = ^(KILabel *label, NSString *string, NSRange range) {
NSLog(@"Hashtag tapped %@", string);
};
// Attach a block to be called when the user taps a URL
label.urlLinkTapHandler = ^(KILabel *label, NSString *string, NSRange range) {
NSLog(@"URL tapped %@", string);
};
[self.view addSubview:label];
KILabel 也支持 Swift。下面是上述示例的 Swift 版本。
// Create the label, you can do this in Interface Builder as well
let label = KILabel(frame: CGRect(x: 20, y: 64, width: 280, height: 60))
label.text = "Follow @krelborn or visit http://compiledcreations.com #shamelessplug"
// Attach a block to be called when the user taps a user handle
label.userHandleLinkTapHandler = { label, handle, range in
NSLog("User handle \(handle) tapped")
}
// Attach a block to be called when the user taps a hashtag
label.hashtagLinkTapHandler = { label, hashtag, range in
NSLog("Hashtah \(hashtag) tapped")
}
// Attach a block to be called when the user taps a URL
label.urlLinkTapHandler = { label, url, range in
NSLog("URL \(url) tapped")
}
view.addSubview(label)
存储库包含一个名为 KILabelDemo 的 Objective-C 项目,它展示了在 storyboarding 中使用标签的简单示例,并提供了实现可触摸链接的示例。
演示还演示了如何使用手势识别器与标签配合使用,以实现对链接的长按功能,这一功能使用了 linkAtPoint 方法。
还有一个示例,使用了UITableView,其中单元格的动态高度取决于内容。
KILabel 在 MIT 许可下可用。
KILabel 受 STTweetLabel (http://github.com/SebastienThiebaud) 以及其他人如 NimbusAttributedLabel (http://latest.docs.nimbuskit.info/NimbusAttributedLabel.html) 的启发。如果 KILabel 不能满足您的需求,可能他们可以。
打开一个问题来报告错误或请求功能。
其他反馈也欢迎通过明显的渠道。