Swift How to change UIAlertController's Title Color

后端 未结 6 1484
鱼传尺愫
鱼传尺愫 2020-12-14 09:27

How do I change the UIAlertController\'s Title font using Swift?

I\'m not talking about the message color, I\'m not talking about the buttons color.

I\'m tal

6条回答
  •  醉梦人生
    2020-12-14 10:23

    Here is for Swift 4 ++ also using a global instance for UIAlert

    func showAlertError(withTitle title: String, withMessage message: String) {
        var dialogTitle = title
        if dialogTitle.isEmpty { dialogTitle = "Error:" }
    
    
        let attributedString = NSAttributedString(string: dialogTitle, attributes: [
            NSAttributedString.Key.font : UIFont.systemFont(ofSize: 15),
            NSAttributedString.Key.foregroundColor : UIColor.red
            ])
    
        let alert = UIAlertController(title: "", message: message, preferredStyle: .alert)
        alert.setValue(attributedString, forKey: "attributedTitle")
        let ok = UIAlertAction(title: "OK", style: .default, handler: nil)
    
        alert.addAction(ok)
        DispatchQueue.main.async(execute: {
            self.present(alert, animated: true)
        })
    }
    

提交回复
热议问题