0

I have a requirement that the text in the buttons of a UIAlertController should be set to bold for every button (as opposed to the standard iOS behavior which is that the button assigned the style cancel is bold, or that for which preferredAction has been set is bold. The requirement is that all button text should be bold).

Is there a way to achieve this using a UIAlertController? Or will I be forced to created a custom dialog using a UIView?

There's plenty of past questions/answers on manipulating the body text for a UIAlertController using an attributed string, but I've not found anything for doing the equivalent for the text of the action buttons of an UIAlertController.

Gruntcakes
  • 37,738
  • 44
  • 184
  • 378
  • I feel your pain. I have been hoping that system generated alerts will use something that cant be replicated in-app so that uialertview opens up a little bit. (I had to make a custom one to do what you asked) – solenoid Apr 13 '18 at 18:51
  • Thought something like this might have worked, but no. UILabel.appearance(whenContainedInInstancesOf: [UIAlertController.self]).font = UIFont.boldSystemFont(ofSize: 14) It seems one can no longer set appearance properties for UILabel. See https://stackoverflow.com/a/11839198/600753 – picciano Apr 13 '18 at 19:05

2 Answers2

1

Buttons on UIAlertController are of UIAlertAction. There are no any setter methods on any property of UIAlertAction available, so you can't change the button text to bold.

Even title have to be String type, it doesn't accept NSAttributedString. So you should go with a custom dialog using a UIView.

Apple class reference for UIAlertAction:

@available(iOS 8.0, *)
open class UIAlertAction : NSObject, NSCopying {

    public convenience init(title: String?, style: UIAlertActionStyle, handler: ((UIAlertAction) -> Swift.Void)? = nil)

    open var title: String? { get }

    open var style: UIAlertActionStyle { get }

    open var isEnabled: Bool
}
Ankit Jayaswal
  • 5,549
  • 3
  • 16
  • 36
1

Or will I be forced to created a custom dialog using a UIView?

Yes, that’s it. But it’s very easy, and gives you much more power and flexibility than UIAlertController. There are lots of sample projects out there, and once you’ve used one you may never go back to UIAlertController again!

matt
  • 515,959
  • 87
  • 875
  • 1,141