Is it possible to edit UIAlertAction title font size and style?
Asked Answered
C

2

10

Now that iOS8 deprecated UIActionsheet and UIAlertview the customization working on iOS7 is not taking effect anymore. So far the only customization I'm aware is the tint color. And what I need is changing the title's font size and style which I haven't found any way of doing so with the new UIAlertAction.

Already referred to this but I'm still hoping there's a way to change at least the title size and font.

Providing you some of my code for UIAlertAction

UIAlertController * alertActionSheetController = [UIAlertController alertControllerWithTitle:@"Settings"
                                                                              message:@""
                                                                       preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * aboutTheAppAction = [UIAlertAction actionWithTitle:@"About the App"
                                                                 style:UIAlertActionStyleDefault
                                                               handler:^(UIAlertAction * action){
                                                                   NSLog(@"About the app");
                                                                   [self openAbout];

                                                               }];


[alertActionSheetController addAction:aboutTheAppAction];
[self presentViewController:alertActionSheetController animated:YES completion:nil];
Caracas answered 23/9, 2014 at 6:48 Comment(2)
look at this github.com/ianb821/IBActionSheetHoffarth
Well that'll work for both since it's made up of a new subclass of uiview but what I'm looking for is ways to edit styling of an alertaction.Caracas
P
13

You can change UIAlertAction's Font and color. First you need to add UILabel Category

@interface UILabel (FontAppearance)
@property (nonatomic, copy) UIFont * appearanceFont UI_APPEARANCE_SELECTOR;
@end

@implementation UILabel (FontAppearance)

-(void)setAppearanceFont:(UIFont *)font {
    if (font)
        [self setFont:font];
}

-(UIFont *)appearanceFont {
    return self.font;
}

@end

Category File is also Uploaded on following URL https://www.dropbox.com/s/em91fh00fv3ut4h/Archive.zip?dl=0

After importing That file You need to call following function.

UILabel * appearanceLabel = [UILabel appearanceWhenContainedIn:UIAlertController.class, nil];
[appearanceLabel setAppearanceFont:yourDesireFont]]; 

Above code is tested on Color and font. and that will only valid for iOS8 or greater.

Pollination answered 17/12, 2014 at 5:16 Comment(3)
This changes the font for the all the labels in UIAlertController's view.Usurp
Thanks worked for me. I did put it into my own UILabel category that I already had, and the property declaration went in the .h I put method declarations for those new selectors in the .h as well I imported the .h where I used the UIAlertControllerVibrate
hey thank you the solution just have one query... how we can change the title color using the above category.Starnes
B
1

It is possible to change alert action's font using private APIs. It may get you app rejected, I have not yet tried to submit such code.

let alert = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)

let action = UIAlertAction(title: "Some title", style: .Default, handler: nil)let attributedText = NSMutableAttributedString(string: "Some title")

let range = NSRange(location: 0, length: attributedText.length)
attributedText.addAttribute(NSKernAttributeName, value: 1.5, range: range)
attributedText.addAttribute(NSFontAttributeName, value: UIFont(name: "ProximaNova-Semibold", size: 20.0)!, range: range)

alert.addAction(action)

presentViewController(alert, animated: true, completion: nil)

// this has to be set after presenting the alert, otherwise the internal property __representer is nil
guard let label = action.valueForKey("__representer")?.valueForKey("label") as? UILabel else { return }
label.attributedText = attributedText
Brendonbrenk answered 3/8, 2016 at 14:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.