UIAlertcontroller上的可点击网址

时间:2018-12-13 15:46:10

标签: ios alert uialertcontroller

是否可以创建一个警报,其中包含一条包含可点击URL的文本的消息,并在Safari中进一步重定向?示例:https://prntscr.com/lujcx5

enter image description here

1 个答案:

答案 0 :(得分:0)

您可能想尝试一下,

    let alert = UIAlertController(title: "Default Title", message: nil, preferredStyle: .alert)
    let textView = UITextView()
    textView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    let controller = UIViewController()

    textView.frame = controller.view.frame
    controller.view.addSubview(textView)
    textView.backgroundColor = .clear

    alert.setValue(controller, forKey: "contentViewController")

    let height: NSLayoutConstraint = NSLayoutConstraint(item: alert.view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 150)
    alert.view.addConstraint(height)

    let attributedString = NSMutableAttributedString(string: "http://movies.com")
    let url = URL(string: "http://movies.com")

    attributedString.setAttributes([.link: url ?? ""], range: NSMakeRange(0, attributedString.length))


    textView.attributedText = attributedString
    textView.isUserInteractionEnabled = true
    textView.isEditable = false

    // Set how links should appear: blue and underlined
    textView.linkTextAttributes = [
        .foregroundColor: UIColor.blue,
        .underlineStyle: NSUnderlineStyle.single.rawValue
    ]

    let okAction = UIAlertAction(title: "OK", style: .default) {
        UIAlertAction in
    }
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) {
        UIAlertAction in
    }

    alert.addAction(okAction)
    alert.addAction(cancelAction)

    present(alert, animated: true, completion: nil)

输出应如下所示: image