You can't change UIAlertController's fonts or sizes through public API, and the common trick, setValue(_:forKey: "attributedMessage"), relies on private keys that Apple can rename or remove. Apple's documentation says the class's view hierarchy is private and must not be modified. Use the standard alert as it is, emphasize one button with preferredAction, and when you need your own fonts and colors, present a small custom view controller as a sheet.
SWIFT TIPS · UIKIT
First posted on July 5, 2018 for Swift 4, as "UIAlertController size, Font, swift 4". Rewritten on November 17, 2026. The Swift code below was type-checked on a Mac with Xcode 26.2 against the iOS 26.2 SDK, in Swift 5 and Swift 6 language modes, with iOS 15 as the deployment target. It wasn't run on a device.
In 2018 I wanted an action sheet that matched an app's design: a grey tint for the buttons and the message in Avenir at 14 points. The post set alert.view.tintColor and used key-value coding to set the alert's attributedMessage, and it ended by asking readers whether anyone knew how to change the font size of the buttons too.
Nobody can, at least not in a supported way, and that's the answer this rewrite starts with.
Why the key-value trick is risky
attributedMessage and attributedTitle are not part of UIAlertController's public interface. setValue(_:forKey:) reaches a private property by its name as a string. If a future iOS renames it, that call raises an exception for an unknown key and the app stops. Apple's documentation for the class is direct about it: UIAlertController is meant to be used as is, doesn't support subclassing, and its view hierarchy is private and must not be modified.
There's also an App Review angle: Apple's App Review Guidelines say apps may only use public APIs (guideline 2.5.1). Whether this specific key gets flagged is not something I can promise either way, which is itself a reason not to build on it.
What you can do with a standard alert
The standardAlert() method at the end of the code shows the supported options: a title, a message, actions in the default, cancel and destructive styles, and preferredAction, which Apple's documentation says highlights that action and lets the Return key on a hardware keyboard trigger it. The alert follows the system font, the user's text size and dark mode by itself, which a styled copy has to rebuild.
A custom sheet when you need your own look
import UIKit
/// A small custom sheet: your own fonts and colors, without touching UIAlertController's private views.
final class ChoiceSheetViewController: UIViewController {
struct Choice { let title: String; let handler: () -> Void }
private let message: String
private let choices: [Choice]
init(message: String, choices: [Choice]) {
self.message = message
self.choices = choices
super.init(nibName: nil, bundle: nil)
modalPresentationStyle = .pageSheet
if let sheet = sheetPresentationController {
sheet.detents = [.medium()]
sheet.prefersGrabberVisible = true
}
}
required init?(coder: NSCoder) { fatalError("Created in code") }
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
let messageLabel = UILabel()
messageLabel.text = message
messageLabel.numberOfLines = 0
messageLabel.textAlignment = .center
messageLabel.font = UIFontMetrics(forTextStyle: .subheadline)
.scaledFont(for: UIFont(name: "Avenir-Roman", size: 14) ?? .preferredFont(forTextStyle: .subheadline))
messageLabel.adjustsFontForContentSizeCategory = true
messageLabel.textColor = .secondaryLabel
let buttons = choices.map { choice -> UIButton in
var config = UIButton.Configuration.gray()
config.title = choice.title
config.baseForegroundColor = .label
return UIButton(configuration: config, primaryAction: UIAction { [weak self] _ in
self?.dismiss(animated: true) { choice.handler() }
})
}
let stack = UIStackView(arrangedSubviews: [messageLabel] + buttons)
stack.axis = .vertical
stack.spacing = 12
stack.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(stack)
NSLayoutConstraint.activate([
stack.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 24),
stack.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor),
stack.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor),
])
}
}
final class PackageViewController: UIViewController {
@IBAction func moreTapped(_ sender: UIButton) {
let sheet = ChoiceSheetViewController(message: "Choose a package", choices: [
.init(title: "Basic") { print("basic") },
.init(title: "Premium") { print("premium") },
])
present(sheet, animated: true)
}
// If a standard alert is enough, style it through supported properties only
func standardAlert() {
let alert = UIAlertController(title: "Choose a package", message: nil, preferredStyle: .alert)
let premium = UIAlertAction(title: "Premium", style: .default)
alert.addAction(UIAlertAction(title: "Basic", style: .default))
alert.addAction(premium)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
alert.preferredAction = premium // bold, and the Return key picks it
present(alert, animated: true)
}
}
ChoiceSheetViewController is an ordinary view controller presented as a sheet with a medium detent. Because you own it, you can use any font, color or layout:
- The message uses Avenir 14 like the 2018 version, but through
UIFontMetrics, so it still grows when the user picks a larger text size. It falls back to the system font if Avenir isn't available. - Each choice is a
UIButtonwith a configuration, which gives a pressed state and accessibility for free. - The sheet dismisses itself first and then runs the choice's handler, so pushing a new screen from the handler happens after the sheet is gone.
On iPad, a page sheet shows centered rather than as a popover from a button, which is usually fine for a list of choices.
A quick check for your project
- Search for
setValue(withforKey: "attributedor other string keys on UIKit classes. Each one is a private API dependency. - Search for code that walks
alert.view.subviews. That breaks the same rule and tends to break on new iOS versions. - Decide per screen: if the design only needs an emphasized button, a standard alert is enough. If it needs brand fonts, use a custom sheet.
What the 2018 version said
The original action sheet set a grey tintColor on the alert's view, built the message as an attributed string with kCTFontAttributeName, set it through KVC, and added three "Lorem Ipsum" actions plus Cancel. Type-checked as posted with Xcode 26.2, it stops on the Swift 4 name NSAttributedStringKey, which is now NSAttributedString.Key. Fixing that would make it compile again, but the private key underneath would still be there.
If this helped, you can buy me a coffee.
Keep reading on JR Devhub
Comments
swift developer course in mumbai
Cyber Security Course in Cochin
swift developer course in kolkata
swift developer course in trivandrum
swift developer course in Ahmedabad