KILabel

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了KILabel相关的知识,希望对你有一定的参考价值。

  • 功能
    • 让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];
    }
    }
    ```

以上是关于KILabel的主要内容,如果未能解决你的问题,请参考以下文章

从标签Regex中删除点

微信小程序代码片段

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板