UIAlertController custom font, size, color
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
UIAlertController does not provide a public API for customizing fonts, sizes, or colors of its title, message, or action buttons. The standard approach uses NSAttributedString set via Key-Value Coding (setValue(_:forKey:)) on the attributedTitle and attributedMessage private keys. For action button colors, use the UIView.appearance() API or set the action's titleTextColor via KVC. These techniques work but rely on private API that Apple could change or reject during App Store review. For full customization, build a custom alert view instead.
Custom Title and Message with NSAttributedString
The attributedTitle and attributedMessage keys are private but widely used. They override the plain title and message properties.
Custom Action Button Colors
Adding an Image to an Action
Using UIAppearance for Global Tint
Custom Alert View (Recommended for Full Customization)
Common Pitfalls
- App Store rejection for private API usage:
setValue(_:forKey: "attributedTitle")uses private keys. While many apps ship with this technique, Apple may reject apps during review. For production apps targeting strict review compliance, build a custom alert view. - KVC keys changing across iOS versions: Apple can rename or remove private keys in any iOS update without notice.
attributedTitleandtitleTextColorhave been stable since iOS 8, but future iOS versions could break them. Test on every new iOS beta. - Setting attributed text without setting the plain text first: Pass the same text to both the
UIAlertControllerinitializer and theNSAttributedString. IftitleisnilbutattributedTitleis set, some iOS versions show no title at all. - Forgetting dark mode support in custom colors: Hardcoded colors like
UIColor.blackorUIColor.whitelook wrong in the opposite appearance mode. Use semantic colors (UIColor.label,UIColor.secondaryLabel) or dynamic colors created withUIColor { trait in ... }. - Tint color not applying to destructive actions:
alert.view.tintColoraffects.defaultand.cancelactions but not.destructiveactions, which always use the system red. UsesetValue(_:forKey: "titleTextColor")on the specific destructive action to override its color.
Summary
- Use
setValue(attributedString, forKey: "attributedTitle")to customize alert title font, size, and color - Use
action.setValue(color, forKey: "titleTextColor")to change action button colors - Set
alert.view.tintColorfor a quick global color change on non-destructive actions - These techniques use private API — test on each iOS version and accept the App Store review risk
- For full customization without private API risk, build a custom
UIViewControllerstyled as an alert - Use semantic colors (
UIColor.label) to support both light and dark mode
Related reading
- UIAlertView first deprecated IOS 9
- UIApplication.registerForRemoteNotifications must be called from main thread only
- UIBarButtonItem in navigation bar programmatically?
- UIBarButtonItem in navigation bar programmatically?
- UIBarButtonItem target-action not working?
- UIBarButtonItem with custom image and no border
- UIButton custom font vertical alignment
- UIButton doesn't listen to content mode setting?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.