【问题标题】:Clickable url at UIAlertcontrollerUIAlertcontroller 的可点击 url
【发布时间】:2018-12-13 15:46:10
【问题描述】:

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

【问题讨论】:

标签: ios alert uialertcontroller


【解决方案1】:

你可以试试这个,

    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)

输出应该是这样的,

【讨论】:

  • 这段代码很危险。它访问私有的内部结构,这些结构可能会在未来的 iOS 更新中发生变化并导致应用程序崩溃。避免使用这样的代码。
  • 我会补充一点,如果 Apple 决定这样做,将来可能会导致应用被拒绝。
猜你喜欢
  • 2015-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-24
  • 2010-12-27
  • 2010-12-22
相关资源
最近更新 更多