- 功能
- 让labe具有超链接效果
- 链接
<常用属性>
- 开启关闭标签的点击事件
self.label.automaticLinkDetectionEnabled = sender.isOn
- 打开URL点击事件
self.label.linkDetectionTypes |= KILinkTypeOptionURL;
- 关闭URL点击事件
self.label.linkDetectionTypes ^= KILinkTypeOptionURL;
- 打开@标签点击事件
self.label.linkDetectionTypes |= KILinkTypeOptionUserHandle;
- 闭关@标签点击事件
self.label.linkDetectionTypes ^= KILinkTypeOptionUserHandle;
- 打开#标签事件
self.label.linkDetectionTypes |= KILinkTypeOptionHashtag;
- 关闭#标签事件
self.label.linkDetectionTypes ^= KILinkTypeOptionHashtag;
- 设置URL下划线
_label.systemURLStyle = YES;
- 设置标签选中是的颜色
_label.selectedLinkBackgroundColor = [UIColor orangeColor];
- 设置标签属性
- (void)setAttributes:(nullable NSDictionary*)attributes forLinkType:(KILinkType)linkType;
<@标签>
- 给@标签添加点击事件
```objc
_label.userHandleLinkTapHandler = KILabel *label, NSString *string, NSRange range {
NSString *message = [NSString stringWithFormat:@"You tapped %@", string];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Username"
message:message
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleDefault handler:nil]];[self presentViewController:alert animated:YES completion:nil];
};
```<#标签>
- 给#标签添加点击事件
```objc
_label.hashtagLinkTapHandler = KILabel *label, NSString *string, NSRange range {
NSString *message = [NSString stringWithFormat:@"You tapped %@", string];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Hashtag"
message:message
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleDefault handler:nil]];[self presentViewController:alert animated:YES completion:nil];
};
``` - 给URL添加点击事件
```objc
_label.urlLinkTapHandler = KILabel *label, NSString *string, NSRange range {
// Open URLs
[self attemptOpenURL:[NSURL URLWithString:string]];
};- (void)attemptOpenURL:(NSURL *)url
{
BOOL safariCompatible = [url.scheme isEqualToString:@"http"] || [url.scheme isEqualToString:@"https"];
if (safariCompatible && [[UIApplication sharedApplication] canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Problem"
message:@"The selected link cannot be opened."
delegate:nil
cancelButtonTitle:@"Dismiss"
otherButtonTitles:nil];
[alert show];
}
}
``` - (void)attemptOpenURL:(NSURL *)url
时间: 2024-08-04 16:29:27