我想在UIAlertController中添加一个网站链接,用我的字符串消息添加它,这意味着我不使用文本字段,我需要添加带有标题标签的超链接来描述swift中的这个链接。
所以它看起来像一个可点击的按钮。
到目前为止我做了什么不行
var mysite = <a href=LINK URL\">TEXT</a>
let messageTxt = "hello people thanks for installing my apps \(my site)"
例
您可以尝试在文本字段中使用属性字符串,并将其添加到UIAlertView中。
步骤1:创建属性字符串的扩展名以查找链接并为链接文本添加适当的样式。
extension NSMutableAttributedString {
public func SetAsLink(textToFind:String, linkURL:String) {
let foundRange = self.mutableString.range(of: textToFind)
if foundRange.location != NSNotFound {
self.addAttribute(.link, value: linkURL, range: foundRange)
}
}
}
第2步:创建属性字符串并将样式添加到其中。
let attributedString = NSMutableAttributedString(string:"Please Open this LINK!")
attributedString.SetAsLink(textToFind: "LINK", linkURL: "http://stackoverflow.com")
第3步:创建警报视图
let alert: UIAlertView = UIAlertView(title: "Title", message: "message",
delegate: self, cancelButtonTitle: "OK", otherButtonTitles: "Cancel")
第4步:创建不可编辑的文本字段
let Txt:UITextView = UITextView(frame:CGRect(x: 0, y: 0, width: 100, height: 100))
第5步:将属性字符串设置为文本字段
Txt.attributedText = attributedString;
步骤6:使文本字段不可编辑并检测链接类型
Txt.isEditable=false;
Txt.dataDetectorTypes = UIDataDetectorTypes.link;
步骤7:将文本字段设置为警报并显示警报
Txt.isEditable=false;
Txt.dataDetectorTypes = UIDataDetectorTypes.link;
总结一下
要显示警报:
let attributedString = NSMutableAttributedString(string:"Please Open this LINK!")
attributedString.SetAsLink(textToFind: "LINK", linkURL: "http://stackoverflow.com")
let alert: UIAlertView = UIAlertView(title: "Title", message: "message",
delegate: self, cancelButtonTitle: "OK", otherButtonTitles: "Cancel")
let Txt:UITextView = UITextView(frame:CGRect(x: 0, y: 0, width: 100, height: 100))
Txt.attributedText = attributedString;
Txt.isEditable=false;
Txt.dataDetectorTypes = UIDataDetectorTypes.link;
alert.setValue(Txt, forKey: "accessoryView")
alert.show()
扩展属性字符串
extension NSMutableAttributedString {
public func SetAsLink(textToFind:String, linkURL:String) {
let foundRange = self.mutableString.range(of: textToFind)
if foundRange.location != NSNotFound {
self.addAttribute(.link, value: linkURL, range: foundRange)
}
}
}
P.S:别忘了让你的视图控制器成为UIAlertViewDelegate。
这是sample项目
如果您使用的是CDAlertView,可以试试这个:
let alert = CDAlertView(title: "Awesome Title", message: "Are you in?!", type: .notification)
let doneAction = CDAlertViewAction(title: "Sure! 💪")
alert.add(action: doneAction)
let nevermindAction = CDAlertViewAction(title: "Nevermind 😑")
alert.add(action: nevermindAction)
let myCustomView = UIVIew(frame: CGRectMake(0,0,280,200))
let Txt:UITextView = UITextView(frame:CGRect(x: 0, y: 0, width: 100, height: 100))
Txt.attributedText = attributedString;
Txt.isEditable=false;
Txt.dataDetectorTypes = UIDataDetectorTypes.link;
myCustomView.addSubview(Txt)
alert.customView = myCustomView
alert.show()
最简单的方法是将URL定义为URL数据类型,并使用UIApplication:openURL方法在Safari中打开它。
在Swift中,这可能就像(代码偏离我的头脑,而不是来自应用程序):
let url = URL(string:"http://www.foobar.com")
UIApplication.shared.open(url)
这将在手机上启动Safari并在Safari中打开URL。如果您想在应用中执行此操作,则需要启动Web视图并在该视图中打开URL。由于您是从组件构建URL,因此您需要使用其他一些URL方法,而不是上面的简单示例。
但是,如果您只想感谢您的用户,为什么要使用网络链接?使用UILabel并将其文本属性设置为您想要告诉用户的内容会更容易。这比web URL更简单,更像iOS。 UILabel的初始值将为空或仅为空格。