【问题标题】:iOS - Swift how to make links clicked in UILabeliOS - Swift 如何在 UILabel 中点击链接
【发布时间】:2016-06-02 17:34:03
【问题描述】:

这是我在 UILabel 中显示链接的代码

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var textFiled: UITextField!

    @IBOutlet weak var label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let attrStr = try! NSAttributedString(
            data: "This is a link <a href='http://www.google.com'>google</a>".dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
            options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
            documentAttributes: nil)
        label.attributedText = attrStr
    }

}

从图片中可以看出,它正确显示了我想要的链接,但是链接无法点击跳转到网页视图或其他。其实我需要自定义链接点击事件,我真的不知道:

  1. 如何获取链接的 URL。
  2. 如果有多个链接,我需要做什么。

感谢您首先检查此问题。

【问题讨论】:

    标签: html swift tags uilabel


    【解决方案1】:

    简短的回答是 UILabel 不支持打开 URL。它可以检测到链接存在,但它不会做任何事情。

    您可以找到解决问题的替代方案,您可以阅读here

    【讨论】:

      【解决方案2】:

      强烈建议使用 UITextView 对象而不是 UILabel,但是您可以使用 KILabel 项目作为 UILabel 的替代方案。 见https://github.com/Krelborn/KILabel/blob/master/README.md

      【讨论】:

        【解决方案3】:

        这是满足您需求的基本方法,我习惯于创建可点击的标签

        private let linkLabel: UILabel = {
            let label = UILabel()
            label.text = "Link"
            return label
        }()
        
          func setup() {
                linkLabel.isUserInteractionEnabled = true
                let tap = UITapGestureRecognizer(target: self, action: #selector(openLink))
                linkLabel.addGestureRecognizer(tap)
        }
        
            if let openLink = URL(string: "http://www.apple.com") {
            if UIApplication.shared.canOpenURL(url) {
                UIApplication.shared.open(url, options: [:])
            }
        }
        

        【讨论】:

          【解决方案4】:

          正如卢克所说,UILabels 不支持 URL。但是您可以创建一个看起来像标签的按钮。摆脱背景(看起来像文本)并实现按钮的功能以打开webview。您可以使用属性字符串来编辑按钮中文本的样式。

          当您将按钮添加到您的 UI 时,您将要考虑使用自定义类而不是默认的 uibutton 类。

          您需要:

          1. 初始化

             - (instancetype)initWithFrame:(CGRect)frame {
               self = [super initWithFrame:frame];
                 if (self)
                 {
                     //call the method to configure the button
                     [self configure];
                 }
               return self;
             }
            
          2. 如果使用属性字符串,请实现 setTitle 或 setAttributedTitle

             - (void) setTitle:(NSString *)title forState:(UIControlState)state
             {
                    [super setTitle:title forState:state]
             }      
            

                - (void)setAttributedTitle:(NSAttributedString *)title forState:   (UIControlState)state{
                      [super setAttributedTitle:title forState:state];
                   }
          

          3。配置按钮

                   -(void) configure{
                       //configure the buttons label here 
                       //you will want to set no background colour
                    }
          

          4。将按钮添加到 viewcontroller 并将类更改为您创建的自定义类。

          【讨论】:

            【解决方案5】:

            对于链接,通常正确的答案是使用UITextView

            但我知道额外的两个子视图对于简单的用例可能有点重。 UILabel 子类很好,但是你被限制使用自己的子类。其他答案都依赖于重新创建您自己的文本堆栈,希望模仿 UILabel 使用的堆栈,尽管当文本开始缩小时这很快就会失败。

            正因为如此,我创建了 LinkGestureRecognizer (https://github.com/stuartjmoore/UILabel-LinkGestureRecognizer) 来满足您对 UILabel 的所有需求。

            很简单:

            let label = UILabel()
            label.attributedText = // An attributed string with inline link ranges
            
            let linkGestureRecognizer = LinkGestureRecognizer(target: self, action: #selector(handleLinkTap))
            label.addGestureRecognizer(linkGestureRecognizer)
            
            @objc func handleLinkTap(_ gestureRecognizer: LinkGestureRecognizer) {
                // handle gestureRecognizer.url in gestureRecognizer.range (UTF-16)
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2013-11-20
              • 1970-01-01
              • 1970-01-01
              • 2010-11-18
              • 1970-01-01
              相关资源
              最近更新 更多