Swift How to Change Uialertcontroller's Title Color

Swift How to change UIAlertController's Title Color

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.

Change title color in UIAlertController

only red Color is possible when you set UIAlertActionStyle.Destructive

Check this link

UIAlertController custom font, size, color

Change Alertview UIAlertController Buttons and Title color in iOS

Use below code to change Alert title:

 alertController.setValue(NSAttributedString(string: "test", attributes: [NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 15),NSAttributedStringKey.foregroundColor : UIColor.red]), forKey: "attributedTitle")

Sample Image

Change UIAlertController's title fontsize

You can make title and message of UIAlertController attributed by using this code. You can customize as per your need. You can see the result in the image. I am not sure you can put it on Appstore.

func showAlert() {
let alert = UIAlertController(title: "", message: "", preferredStyle: .actionSheet)
let titleAttributes = [NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Bold", size: 25)!, NSAttributedStringKey.foregroundColor: UIColor.black]
let titleString = NSAttributedString(string: "Name Last name", attributes: titleAttributes)
let messageAttributes = [NSAttributedStringKey.font: UIFont(name: "Helvetica", size: 17)!, NSAttributedStringKey.foregroundColor: UIColor.red]
let messageString = NSAttributedString(string: "Company name", attributes: messageAttributes)
alert.setValue(titleString, forKey: "attributedTitle")
alert.setValue(messageString, forKey: "attributedMessage")
let labelAction = UIAlertAction(title: "Label", style: .default, handler: nil)
let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: nil)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alert.addAction(labelAction)
alert.addAction(deleteAction)
alert.addAction(cancelAction)
self.navigationController?.present(alert, animated: true, completion: nil)

}

Sample Image

How can assign UIAlertAction Image and Change the Title Color as well

I think that action1.setValue(image1, forKey: "image") could be dangerous: Apple could reject your app as you're using a private api. It could, I used private api sometimes and nothing happened.

Anyway, if you want to change the text color you can set a tint color to the alert controller's view:

alert.view.tintColor = .orange // or whatever

If you really want to put an image, the image itself will inherit the tint color of the view (which by default is that blue). To avoid using tint color, you could get your image changing the render mode:

let image = UIImage(named: "actionSheetGallery")?.withRenderingMode(.alwaysOriginal)


Related Topics



Leave a reply



Submit