Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit UIAlertAction text font size and color

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.

like image 517
naomi Avatar asked Jun 10 '16 09:06

naomi


People also ask

How do I change the color of my UIAlertAction text?

The text of the shown UIAlertAction uses the UIView 's tint color, so you can use the UIAppearence API to change it to any color you want, but the same color for all the UIAlertAction s. If you set the style to destructive instead of default , the text is shown as red, not affected by the tint color.

How do I change the font on my UIAlertAction?

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.


2 Answers

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

like image 59
kaushal Avatar answered Sep 23 '22 03:09

kaushal


I've written an extension

extension UIAlertController{
    open override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        for i in self.actions {
            let attributedText = NSAttributedString(string: i.title ?? "", attributes: [NSAttributedString.Key.font : UIFont(name: "SFProText-Semibold", size: 20.0)!])

            guard let label = (i.value(forKey: "__representer") as AnyObject).value(forKey: "label") as? UILabel else { return }
            label.attributedText = attributedText
        }

    }
}
like image 42
Farhad Faramarzi Avatar answered Sep 22 '22 03:09

Farhad Faramarzi