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")
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)
}
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
Is the Current Location/Compass Heading Button Available in the iOS Sdk
Share Video on Twitter with Fabric API Without Composer iOS
Dragging Scnnode in Arkit Using Scenekit
How to Retrieve Messages Logged with Os_Log from iPad/Iphone
Where to Place a .Txt File and Read from It in a iOS Project
Wait for Swift Animation to Complete Before Executing Code
Swift: Equivalent Objective-C Runtime Class
Titletextattributes Uiappearance Font in iOS 7
Clipstobounds Causes Uiimage to Not Display in iOS10 & Xcode 8
Calayer - Cabasicanimation Not Scaling Around Center/Anchorpoint