UIAlertAction list of values
Asked Answered
D

2

8

I'm trying to figure out how to change font type for UIAlertAction title. I'm assuming, it can be done by setting a value for particular key. For instance, to set an image you would do this:

action.setValue(image, forKey: "image")

Is there a list of all keys that are available? I can't figure out which key to use for changing font, aligning the title left/right, etc...

Damien answered 5/5, 2015 at 14:47 Comment(1)
is there a way to use attributed string for UIAlertAction title?Damien
R
24

class_copyIvarList may be what you need.

Swift

extension UIAlertAction {
    static var propertyNames: [String] {
        var outCount: UInt32 = 0
        guard let ivars = class_copyIvarList(self, &outCount) else {
            return []
        }
        var result = [String]()
        let count = Int(outCount)
        for i in 0..<count {
            let pro: Ivar = ivars[i]
            guard let ivarName = ivar_getName(pro) else {
                continue
            }
            guard let name = String(utf8String: ivarName) else {
                continue
            }
            result.append(name)
        }
        return result
    }
}

then

print(UIAlertAction.propertyNames)

and the output is

["_title", "_titleTextAlignment", "_enabled", "_checked", "_isPreferred", "_imageTintColor", "_titleTextColor", "_style", "_handler", "_simpleHandler", "_image", "_shouldDismissHandler", "__descriptiveText", "_contentViewController", "_keyCommandInput", "_keyCommandModifierFlags", "__representer", "__interfaceActionRepresentation", "__alertController"]
Ravo answered 21/1, 2019 at 5:56 Comment(2)
Has anyone experimented with each key?Northernmost
Could you give an example for each key? it's helpfulNorthernmost
K
-1

You can use this to change the colour of the UIAlertAction title.

 UIAlertController *alertController = [UIAlertController
                                          alertControllerWithTitle:@"alert view"
                                          message:@"hello alert controller"
                                          preferredStyle:UIAlertControllerStyleAlert];

    alertController.view.tintColor = [UIColor greenColor];

    UIAlertAction *cancelAction = [UIAlertAction
                                   actionWithTitle:@"Cancel"
                                   style:UIAlertActionStyleCancel
                                   handler:^(UIAlertAction *action)
                                   {

                                   }];
    [alertController addAction:cancelAction];

    [self.navigationController presentViewController: alertController animated:YES completion:nil];

OR

Check this link-UIAlertController custom font, size, color

Kirven answered 19/11, 2015 at 10:7 Comment(1)
There are several things wrong with this approach now: (Xcode 8.2.1) It only affects the default and cancel buttons, not the title (or message), and in some versions of iOS the .tintColor should be changed after presentViewController is called. A better approach to set the button color is to place [[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:appBarColor]; in your app delegate.Wrongful

© 2022 - 2024 — McMap. All rights reserved.