Swift How to change UIAlertController's Title Color

后端 未结 6 1513
鱼传尺愫
鱼传尺愫 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:07

    I construct this class:

    class Alert {
    class func showAlert(title: String, titleColor: UIColor, message: String, preferredStyle: UIAlertControllerStyle, titleAction: String, actionStyle: UIAlertActionStyle, vc: UIViewController) {
    
        let attributedString = NSAttributedString(string: title, attributes: [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 16), NSAttributedStringKey.foregroundColor: titleColor])
        let alert = UIAlertController(title: title, message: message, preferredStyle: preferredStyle)
    
        alert.setValue(attributedString, forKey: "attributedTitle")
    
        alert.addAction(UIAlertAction(title: titleAction, style: actionStyle, handler: nil))
        vc.present(alert, animated: true)
    }
    

    }

    Usage

    Alert.showAlert(title: "yourTitle", titleColor: .yourcolor, message: "your message", preferredStyle: .alert, titleAction: "your title action", actionStyle: .yourActionStyle, vc: self)
    

    Hope this help :)

提交回复
热议问题