How to change font size and color of UIAlertAction in UIAlertController
16,733
Solution 1
This is from another stack overflow post but seems to work fine. Post found here.
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Dont care what goes here, since we're about to change below" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Presenting the great... Hulk Hogan!"];
[hogan addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:50.0]
range:NSMakeRange(24, 11)];
[alertVC setValue:hogan forKey:@"attributedTitle"];
UIAlertAction *button = [UIAlertAction actionWithTitle:@"Label text"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action){
//add code to make something happen once tapped
}];
UIImage *accessoryImage = [UIImage imageNamed:@"someImage"];
[button setValue:accessoryImage forKey:@"image"];
To change the color of the text with the following, adding it before the line setting [alertVC setValue:hogan forKey:@"attributedTitle"];
[hogan addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,35)];
Solution 2
change color a action in UIAlertController
SEL selector = NSSelectorFromString(@"_alertController");
if ([actionSheet respondsToSelector:selector])
{
UIAlertController *alertController = [actionSheet valueForKey:@"_alertController"];
if ([alertController isKindOfClass:[UIAlertController class]])
{
NSArray *arr = alertController.actions;
for (int i = 0; i <arr.count; i ++) {
UIAlertAction *alertAction = [arr objectAtIndex:i];
if ([alertAction.title isEqualToString:@"Logout"]) {
UIColor *color = [UIColor redColor];
[alertAction setValue:color forKey:@"titleTextColor"];
}
}
}
Solution 3
Simply do like this
UIAlertAction * action = [UIAlertAction actionWithTitle:@"ACTION TITLE" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// TODO : ACTION
}];
[action setValue:[UIColor redColor] forKey:@"titleTextColor"];
[alertController action];
Solution 4
I wasn't able to change the font, but you can change the font colour. From the 'hogan' example, changing the font colour on UIAlertAction:
[button setValue:[UIColor greenColor] forKey:@"titleTextColor"];
![Ronaldo.K](https://i.stack.imgur.com/fIdQ3.png?s=256&g=1)
Author by
Ronaldo.K
Updated on June 04, 2022Comments
-
Ronaldo.K about 2 years
In the image above how to change the font size and color of "Done", "Some Other action"? and how to change the font size and color of "title", and "message"?
Thank you.
-
picciano about 9 yearspossible duplicate of UIAlertController custom font, size, color
-
Constantin Saulenco over 8 yearscheck this solution stackoverflow.com/a/27518769/2050181
-
Ashok R over 7 yearscheck this solution stackoverflow.com/questions/26460706/…
-
-
vahotm over 6 yearsAnd you'll get a crash trying to set value for undefined key on some ios versions (don't remember which). You must also override
setValue: forUndefinedKey:
andvalueForUndefinedKey:
for the UIAlertAction, which you are trying to set image into.