如何将 url 添加到警报?
How can I add a url to an alert?
有没有办法为寻找更多信息的用户添加 url?我的警报看起来像这样
let alert = UIAlertController(title: "Alert details", message: "For more detailed information, click the link below", preferredStyle: UIAlertControllerStyle.Alert)
// add url here
let okayAction = UIAlertAction(title: "Ok", style: .Default) { (action) in
print(action)
}
alert.addAction(okayAction)
想法是将它们重定向到网页并关闭应用程序中的 UIAlertController
您不能向 UIAlertController 添加任意接口。标题和消息不可点击。您无法添加更多文本。相反,添加另一个按钮 (UIAlertAction) 将它们引导至网页。或者,或者使用其他接口而不是 UIAlertController(例如,您可以放置一个 看起来 像警报的呈现视图控制器)。
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)
从这里开始:https://overcoder.net/q/1502030/кликабельный-url-на-uialertcontroller
有没有办法为寻找更多信息的用户添加 url?我的警报看起来像这样
let alert = UIAlertController(title: "Alert details", message: "For more detailed information, click the link below", preferredStyle: UIAlertControllerStyle.Alert)
// add url here
let okayAction = UIAlertAction(title: "Ok", style: .Default) { (action) in
print(action)
}
alert.addAction(okayAction)
想法是将它们重定向到网页并关闭应用程序中的 UIAlertController
您不能向 UIAlertController 添加任意接口。标题和消息不可点击。您无法添加更多文本。相反,添加另一个按钮 (UIAlertAction) 将它们引导至网页。或者,或者使用其他接口而不是 UIAlertController(例如,您可以放置一个 看起来 像警报的呈现视图控制器)。
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)
从这里开始:https://overcoder.net/q/1502030/кликабельный-url-на-uialertcontroller