Swift How to change UIAlertController's Title Color

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

    let attributedString = NSAttributedString(string: "Title", attributes: [
        NSFontAttributeName : UIFont.systemFontOfSize(15), //your font here
        NSForegroundColorAttributeName : UIColor.redColor()
    ])
    
    let alert = UIAlertController(title: "", message: "",  preferredStyle: .alert)
    
    alert.setValue(attributedString, forKey: "attributedTitle")
    
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in
    }
    
    alert.addAction(cancelAction)
    
    present(alert, animated: true, completion: nil)
    

    Added the correct line of code to my answer as it's much more concise than the answer below.

提交回复
热议问题