How to edit UIAlertAction text font size and color

2019-01-18 21:33发布

问题:

How to edit UIAlertAction text size and color? I have taken a UIAlertController acoording to it how to edit the size. This i smy Code

UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"Do you wish to logout?" message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *logOut = [UIAlertAction actionWithTitle:@"Log Out" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {}];

Now i want my 'Log Out' text with font size 22 and green color and in semibold.

回答1:

You can update text color using

       UIAlertAction *myGoalAction = [UIAlertAction
                                    actionWithTitle:NSLocalizedString(@"My Title", @"My Title")
                                    style:UIAlertActionStyleDefault
                                    handler:^(UIAlertAction *action)
                                    {

                                    }];
       [myGoalAction setValue:[UIColor greenColor] forKey:@"titleTextColor"];

There is no efficient way to update font size.I will suggest you to use standard font size.

UIAlertAction title label is private variable and not accessible directly. Label comes inside 3 level private view hierarchy. Showing logout action with bigger font make sense for app.

There are many open source solution available.I will recommend to try this



回答2:

Changing the color is pretty simple.

You could just change the tintColor of the underlying view, however, due to a known bug introduced in iOS 9 (https://openradar.appspot.com/22209332), the tintColor is overridden by the application window's tintColor.

See my full answer to: How to change tint color of UIAlertController


You shouldn't change the UIAlertController font. However it still can be done, see this answer



回答3:

Try this:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Do you wish to logout?" message:@"abcd" preferredStyle:UIAlertControllerStyleActionSheet];

NSMutableAttributedString *xyz = [[NSMutableAttributedString alloc] initWithString:@"pqrs"];

[xyz addAttribute:NSFontAttributeName
              value:[UIFont systemFontOfSize:30.0]
              range:NSMakeRange(20, 15)];
[alert setValue:xyz forKey:@"attributedTitle"];



UIAlertAction *logout = [UIAlertAction actionWithTitle:@"logout" 
                                        style:UIAlertActionStyleDefault
                                        handler:^(UIAlertAction *action){
                                                    //your code for handling this
}];
UIImage *Image = [UIImage imageNamed:@"yourImage"];
[logout setValue:accessoryImage forKey:@"image"];


回答4:

Use NSMutableAttributedString set the font size and color, use this below code,

UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"Do you wish to logout?" message:@"" preferredStyle:UIAlertControllerStyleAlert];

NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Do you wish to logout?"];

[hogan addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:50.0] range:NSMakeRange(24, 11)];

[hogan addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,35)];

[controller setValue:hogan forKey:@"attributedTitle"];

UIAlertAction *logOut = [UIAlertAction actionWithTitle:@"Log Out" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {}];

hope its helpful