【问题标题】:iPhone:- Link Identify From Label TextiPhone:- 从标签文本链接识别
【发布时间】:2012-12-20 05:41:01
【问题描述】:

在我的 iPhone 应用程序中,我想以不同颜色显示标签内的链接(如超链接)。

当有人点击链接时,它应该在 Safari 浏览器中打开该链接。

我该怎么做?

【问题讨论】:

  • UITextview 和 Webview 中的显示文本在哪里?
  • 在 uilabel 中不在 uitextview 或 webiview 中

标签: iphone objective-c uilabel


【解决方案1】:

下面是两个示例示例代码链接,希望对您有所帮助:-

https://github.com/twotoasters/TTTAttributedLabel

http://furbo.org/stuff/FancyLabel_1.0.zip

或者你也可以这样做

将标签的 userInteractionEnabled 设置为 YES 并向其添加手势识别器:

myLabel.userInteractionEnabled = YES;

    UITapGestureRecognizer *gestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openUrl:)];
    gestureRec.numberOfTouchesRequired = 1;
    gestureRec.numberOfTapsRequired = 1;
    [myLabel addGestureRecognizer:gestureRec];
    [gestureRec release];

Then implement the action method:

    - (void)openUrl:(id)sender
    {
        UIGestureRecognizer *rec = (UIGestureRecognizer *)sender;

        id hitLabel = [self.view hitTest:[rec locationInView:self.view] withEvent:UIEventTypeTouches];

        if ([hitLabel isKindOfClass:[UILabel class]]) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:((UILabel *)hitLabel).text]];
        }
    }

【讨论】:

    【解决方案2】:

    我们使用正则表达式来检查链接

    NSString *urlRegEx =@"((http|https)://)?((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
    NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
    if ([urlTest evaluateWithObject:lbl.text])
    {
           //set color of lbl
    }
    

    【讨论】:

      【解决方案3】:

      您无法在标签中识别任何内容来了解​​超链接。

      制作一个看起来像标签的自定义按钮(仅文本和背景为 clearColor),以产生效果。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-20
        • 1970-01-01
        • 1970-01-01
        • 2017-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-24
        相关资源
        最近更新 更多