Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift How to change UIAlertController's Title Color

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 talking about the Title.

like image 780
Jonathan Soifer Avatar asked Jul 27 '15 20:07

Jonathan Soifer


1 Answers

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.

like image 70
pbush25 Avatar answered Sep 19 '22 13:09

pbush25