0

I'm new on iOS development.

I'm trying to change the title and message color of a UIAlertController but is not working, the color is not changing.

Here is my code:

let alert = UIAlertController(title: NSLocalizedString("notifications_popup2_title", comment: ""), message: NSLocalizedString("notifications_popup2_message", comment: ""), preferredStyle: UIAlertControllerStyle.actionSheet)

        // Change font of the title and message
        let titleFont:[NSAttributedStringKey : AnyObject] = [ NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue) : UIFont(name: "Flama-Basic", size: 22)!, NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue) : UIColor(hexString: "#2e2e2e")! ]
        let messageFont:[NSAttributedStringKey : AnyObject] = [ NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue) : UIFont(name: "Flama-light", size: 18)!, NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue) : UIColor(hexString: "#2e2e2e")! ]
        let attributedTitle = NSMutableAttributedString(string: NSLocalizedString("notifications_popup2_title", comment: ""), attributes: titleFont)
        let attributedMessage = NSMutableAttributedString(string: NSLocalizedString("notifications_popup2_message", comment: ""), attributes: messageFont)
        alert.setValue(attributedTitle, forKey: "attributedTitle")
        alert.setValue(attributedMessage, forKey: "attributedMessage")

If i change alert style to .alert it is working...

enter image description here

user229044
  • 232,980
  • 40
  • 330
  • 338
noquart3r
  • 1
  • 2
  • Welcome to SO! Please explain what you mean by "but is not working". Also, read [ask]. – koen Nov 29 '19 at 16:22
  • I upload a screenshot. I think is not detecting the color – noquart3r Nov 29 '19 at 16:24
  • UIAlertController's title/message take a string property, not an attributed string, so I;d expect them not to work. I'm guessing that it works for an alert for legacy compatibility reasons. – flanker Nov 29 '19 at 16:34
  • Don't. That's using private properties/libs, it could change as it did (between iOS 12 & iOS13 for instance) and so not work anymore, and someday could be a reason from Apple to reject the app. Instead create your own customized one. – Larme Nov 29 '19 at 16:44
  • Cf. https://stackoverflow.com/questions/58224830/ios-13-uialertcontroller-color-change-in-attributedmessage-and-attributedtitle?noredirect=1#comment102826246_58224830 – Larme Nov 29 '19 at 17:33

1 Answers1

1

Some of the code you used are deprecated like:

NSAttributedStringKey

is now:

NSAttributedString.Key

also you don't need to specify the rawValue. you just need something like:

    let alertController = UIAlertController(title: "title", message: "message", preferredStyle: .alert)
    alertController.setValue(NSAttributedString(string: "title", attributes: [.foregroundColor : UIColor.red]), forKey: "attributedTitle")
    alertController.setValue(NSAttributedString(string: "message", attributes: [.foregroundColor : UIColor.blue]), forKey: "attributedMessage")

Result: Alert

Note that it only works for .alert. .actionSheet title and message is not colorable unlike the .alert

Also be aware that Using private API can cause rejection. But experience shows that only private methods that their name starts with "_" cause rejection from AppStore private method usage detection system.

Also can stop working anytime if apple decides to change it.

The best option you have is to implement a custom alert yourself.

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
  • You are absolutely right @Sulthan. but experience shows that only private methods that their name starts with "_" cause rejection from *AppStore private method usage detection* system. I mentioned that in the answer. Thanks – Mojtaba Hosseini Nov 29 '19 at 16:32
  • I am not talking about rejections, I am talking about the fact the this needs a warning. Using a custom alert controller is usually safer. – Sulthan Nov 29 '19 at 16:36
  • Sorry I missed understand that. You are right as always. I have updated the answer and mentioned both issues. Thanks again @Sulthan – Mojtaba Hosseini Nov 29 '19 at 16:37
  • 3
    It just changed, FYI in IOS13: https://stackoverflow.com/questions/58224830/ios-13-uialertcontroller-color-change-in-attributedmessage-and-attributedtitle?noredirect=1#comment102826246_58224830 – Larme Nov 29 '19 at 17:34