如何在swift中的警报控制器中创建可点击链接

时间:2016-05-04 10:40:41

标签: ios swift uialertcontroller

My app screenshot

我想要像此图像一样的警告对话框。其中包含可点击的链接,如蓝色突出显示的文字。当用户点击该文本时,它将在浏览器中打开一个链接。为此我使用第三方库SimpleAlert

这是我的代码......

func popUpMsg(){

    let msg = "\n Hi Rahul! Welcome back to the early access queue for the app, Once you've been accepted into the beta, we'll notify you using the contact info you have on file with Facebook. \n\n But just a second, if you haven't already done so, you can increase your odds of jumping ahead in the queue by telling us a little about yourself. Please complete our 4-question survey to give it a shot. \n" as NSString
    let range = msg.rangeOfString("complete our 4-question survey")

    let alert = SimpleAlert.Controller(title: "Early Access to Application", message: msg as String, style: .Alert)
    alert.configContentView = { view in
        if let view = view as? SimpleAlert.ContentView {
            view.titleLabel.textColor = UIColor.blackColor()
            view.titleLabel.font = UIFont.boldSystemFontOfSize(32)
            print(view.titleLabel.font)
            view.messageLabel.textColor = UIColor.grayColor()

            let attributedString = NSMutableAttributedString(string: msg as String)
            attributedString.addAttribute(NSLinkAttributeName, value: NSURL(string: "http://www.google.com")!, range: range)
            attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSNumber(int: 1), range: range)
            attributedString.addAttribute(NSUnderlineColorAttributeName, value: UIColor.whiteColor(), range: range)

            view.messageLabel.attributedText = attributedString
            view.messageLabel.font = UIFont(name: "arial", size: 27)
            print(view.messageLabel.font)

            view.textBackgroundView.layer.cornerRadius = 3.0
            view.textBackgroundView.clipsToBounds = true
        }
    }
    alert.addAction(SimpleAlert.Action(title: "OK", style: .OK))
    presentViewController(alert, animated: true, completion: nil)
}

0 个答案:

没有答案