I am using new UIAlertController for showing alerts. I have this code:
// nil titles break alert interface on iOS 8.0, so we\'ll be using empty strings
UIAle
I just completed a replacement for UIAlertController
. This is the only sensible way to go, I think:
Here's my method in Swift which mashes up a lot of information from answers here
func changeAlert(alert: UIAlertController, backgroundColor: UIColor, textColor: UIColor, buttonColor: UIColor?) {
let view = alert.view.firstSubview().firstSubview()
view.backgroundColor = backgroundColor
view.layer.cornerRadius = 10.0
// set color to UILabel font
setSubviewLabelsToTextColor(textColor, view: view)
// set font to alert via KVC, otherwise it'll get overwritten
let titleAttributed = NSMutableAttributedString(
string: alert.title!,
attributes: [NSFontAttributeName:UIFont.boldSystemFontOfSize(17)])
alert.setValue(titleAttributed, forKey: "attributedTitle")
let messageAttributed = NSMutableAttributedString(
string: alert.message!,
attributes: [NSFontAttributeName:UIFont.systemFontOfSize(13)])
alert.setValue(messageAttributed, forKey: "attributedMessage")
// set the buttons to non-blue, if we have buttons
if let buttonColor = buttonColor {
alert.view.tintColor = buttonColor
}
}
func setSubviewLabelsToTextColor(textColor: UIColor, view:UIView) {
for subview in view.subviews {
if let label = subview as? UILabel {
label.textColor = textColor
} else {
setSubviewLabelsToTextColor(textColor, view: subview)
}
}
}
This works in some situations perfectly, and in others it's a total fail (the tint colors do not show as expected).
You can use the following syntax without any private API.
alert.view.tintColor = alert.view.tintColor = UIColor.green
alert.setTitle(font: UIFont.boldSystemFont(ofSize: 26), color: UIColor.darkGray)
alert.setMessage(font: UIFont.systemFont(ofSize: 26), color: UIColor.darkGray)
In Xcode 8 Swift 3.0
@IBAction func touchUpInside(_ sender: UIButton) {
let alertController = UIAlertController(title: "", message: "", preferredStyle: .alert)
//to change font of title and message.
let titleFont = [NSFontAttributeName: UIFont(name: "ArialHebrew-Bold", size: 18.0)!]
let messageFont = [NSFontAttributeName: UIFont(name: "Avenir-Roman", size: 12.0)!]
let titleAttrString = NSMutableAttributedString(string: "Title Here", attributes: titleFont)
let messageAttrString = NSMutableAttributedString(string: "Message Here", attributes: messageFont)
alertController.setValue(titleAttrString, forKey: "attributedTitle")
alertController.setValue(messageAttrString, forKey: "attributedMessage")
let action1 = UIAlertAction(title: "Action 1", style: .default) { (action) in
print("\(action.title)")
}
let action2 = UIAlertAction(title: "Action 2", style: .default) { (action) in
print("\(action.title)")
}
let action3 = UIAlertAction(title: "Action 3", style: .default) { (action) in
print("\(action.title)")
}
let okAction = UIAlertAction(title: "Ok", style: .default) { (action) in
print("\(action.title)")
}
alertController.addAction(action1)
alertController.addAction(action2)
alertController.addAction(action3)
alertController.addAction(okAction)
alertController.view.tintColor = UIColor.blue
alertController.view.backgroundColor = UIColor.black
alertController.view.layer.cornerRadius = 40
present(alertController, animated: true, completion: nil)
}
Output
Here is an extension for Swift 4.1 and Xcode 9.4.1:
extension UIAlertController{
func addColorInTitleAndMessage(color:UIColor,titleFontSize:CGFloat = 18, messageFontSize:CGFloat = 13){
let attributesTitle = [NSAttributedStringKey.foregroundColor: color, NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: titleFontSize)]
let attributesMessage = [NSAttributedStringKey.foregroundColor: color, NSAttributedStringKey.font: UIFont.systemFont(ofSize: messageFontSize)]
let attributedTitleText = NSAttributedString(string: self.title ?? "", attributes: attributesTitle)
let attributedMessageText = NSAttributedString(string: self.message ?? "", attributes: attributesMessage)
self.setValue(attributedTitleText, forKey: "attributedTitle")
self.setValue(attributedMessageText, forKey: "attributedMessage")
}}
You can use an external library like PMAlertController without using workaround, where you can substitute Apple's uncustomizable UIAlertController with a super customizable alert.
Compatible with Xcode 8, Swift 3 and Objective-C
In Swift 4.1 and Xcode 10
//Displaying alert with multiple actions and custom font ans size
let alert = UIAlertController(title: "", message: "", preferredStyle: .alert)
let titFont = [NSAttributedStringKey.font: UIFont(name: "ArialHebrew-Bold", size: 15.0)!]
let msgFont = [NSAttributedStringKey.font: UIFont(name: "Avenir-Roman", size: 13.0)!]
let titAttrString = NSMutableAttributedString(string: "Title Here", attributes: titFont)
let msgAttrString = NSMutableAttributedString(string: "Message Here", attributes: msgFont)
alert.setValue(titAttrString, forKey: "attributedTitle")
alert.setValue(msgAttrString, forKey: "attributedMessage")
let action1 = UIAlertAction(title: "Action 1", style: .default) { (action) in
print("\(String(describing: action.title))")
}
let action2 = UIAlertAction(title: "Action 2", style: .default) { (action) in
print("\(String(describing: action.title))")
}
let okAction = UIAlertAction(title: "Ok", style: .default) { (action) in
print("\(String(describing: action.title))")
}
alert.addAction(action1)
alert.addAction(action2)
alert.addAction(okAction)
alert.view.tintColor = UIColor.blue
alert.view.layer.cornerRadius = 40
// //If required background colour
// alert.view.backgroundColor = UIColor.white
DispatchQueue.main.async(execute: {
self.present(alert, animated: true)
})