8

我试图弄清楚如何更改 UIAlertAction 标题的字体类型。我假设,可以通过为特定键设置一个值来完成。例如,要设置图像,您可以这样做:

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

是否有所有可用密钥的列表?我不知道使用哪个键来更改字体、左/右对齐标题等...

4

2 回答 2

19

class_copyIvarList可能是你需要的。

迅速

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
    }
}

然后

print(UIAlertAction.propertyNames)

输出是

["_title", "_titleTextAlignment", "_enabled", "_checked", "_isPreferred", "_imageTintColor", "_titleTextColor", "_style", "_handler", "_simpleHandler", "_image", "_shouldDismissHandler", "__descriptiveText", "_contentViewController", "_keyCommandInput", "_keyCommandModifierFlags", "__representer", "__interfaceActionRepresentation", "__alertController"]
于 2019-01-21T05:56:34.833 回答
-1

您可以使用它来更改 UIAlertAction 标题的颜色。

 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];

或者

检查此链接- UIAlertController 自定义字体、大小、颜色

于 2015-11-19T10:07:01.390 回答