How to edit UIAlertAction text font size and color

23,245

Solution 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

Solution 2

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
        }

    }
}

Solution 3

Here is a solution for changing the text color in Swift:

let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
action.setValue(UIColor.black, forKey: "titleTextColor")

Or you could use this extension:

extension UIAlertAction {
    var titleTextColor: UIColor? {
        get { return self.value(forKey: "titleTextColor") as? UIColor } 
        set { self.setValue(newValue, forKey: "titleTextColor") }
    }
}

Solution 4

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

Share:
23,245
naomi
Author by

naomi

I code if I do not get answer I try hard and hard.

Updated on July 09, 2022

Comments

  • naomi
    naomi almost 2 years

    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.