UIAlertController自定义字体、大小、颜色

128

我正在使用新的UIAlertController来显示警报。我有以下代码:

// nil titles break alert interface on iOS 8.0, so we'll be using empty strings
UIAlertController *alert = [UIAlertController alertControllerWithTitle: title == nil ? @"": title message: message preferredStyle: UIAlertControllerStyleAlert];


UIAlertAction *defaultAction = [UIAlertAction actionWithTitle: cancelButtonTitle style: UIAlertActionStyleCancel handler: nil];

[alert addAction: defaultAction];

UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootViewController presentViewController:alert animated:YES completion:nil];

现在我想改变标题和消息的字体、颜色和大小等。有什么最好的方法来做到这一点吗?

编辑: 我应该插入整个代码。我创建了一个UIView的类别,可以在iOS版本中显示正确的警报。

@implementation UIView (AlertCompatibility)

+( void )showSimpleAlertWithTitle:( NSString * )title
                          message:( NSString * )message
                cancelButtonTitle:( NSString * )cancelButtonTitle
{
    float iOSVersion = [[UIDevice currentDevice].systemVersion floatValue];
    if (iOSVersion < 8.0f)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle: title
                                                        message: message
                                                       delegate: nil
                                              cancelButtonTitle: cancelButtonTitle
                                              otherButtonTitles: nil];
        [alert show];
    }
    else
    {
        // nil titles break alert interface on iOS 8.0, so we'll be using empty strings
        UIAlertController *alert = [UIAlertController alertControllerWithTitle: title == nil ? @"": title
                                                                       message: message
                                                                preferredStyle: UIAlertControllerStyleAlert];


        UIAlertAction *defaultAction = [UIAlertAction actionWithTitle: cancelButtonTitle
                                                                style: UIAlertActionStyleCancel
                                                              handler: nil];

        [alert addAction: defaultAction];

        UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
        [rootViewController presentViewController:alert animated:YES completion:nil];
    }
}

2
免责声明:对于阅读以下答案的任何人。如果您倾向于使用任何私有API,苹果将拒绝您的应用程序。在下面的答案中,这就是发生的事情。 - Yash Bedi
25个回答

1
要将一个按钮的颜色改为红色,比如 CANCEL 按钮,你可以使用名为 UIAlertActionStyle.destructive 的样式属性:
let prompt = UIAlertController.init(title: "Reset Password", message: "Enter Your E-mail :", preferredStyle: .alert)
        let okAction = UIAlertAction.init(title: "Submit", style: .default) { (action) in
              //your code
}

let cancelAction = UIAlertAction.init(title: "Cancel", style: UIAlertActionStyle.destructive) { (action) in
                //your code
        }
        prompt.addTextField(configurationHandler: nil)
        prompt.addAction(okAction)
        prompt.addAction(cancelAction)
        present(prompt, animated: true, completion: nil);

0

有点笨重,但目前对我来说可以用来设置背景和文本颜色。我在这里找到了它

UIView * firstView = alertController.view.subviews.firstObject;
    UIView * nextView = firstView.subviews.firstObject;
    nextView.backgroundColor = [UIColor blackColor];

它可以改变背景颜色,但是无法改变色调颜色,这让我有点困惑。 - Akhilesh Sharma

0

我只是使用这种类型的需求,看起来和系统有些不同,所以我们需要... OC实现Alert和Sheet弹出窗口的封装。

在日常开发中经常遇到需要向Alert添加一个图形或更改按钮颜色等“简单”需求,今天带来了一个与系统组件高度相似且完全满足定制包装组件需求的方案。

Github:https://github.com/ReverseScale/RSCustomAlertView


0

我已经创建了一个Objective-C方法

-(void)customAlertTitle:(NSString*)title message:(NSString*)message{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 80)];

UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 270, 50)];
titleLabel.text = title;
titleLabel.font = [UIFont boldSystemFontOfSize:20];
titleLabel.numberOfLines = 2;
titleLabel.textColor = [UIColor redColor];
titleLabel.textAlignment = NSTextAlignmentCenter;

[subView addSubview:titleLabel];

UILabel *messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, 270, 50)];
messageLabel.text = message;
messageLabel.font = [UIFont systemFontOfSize:18];
messageLabel.numberOfLines = 2;
messageLabel.textColor = [UIColor redColor];
messageLabel.textAlignment = NSTextAlignmentCenter;

[subView addSubview:messageLabel];

[alertView setValue:subView forKey:@"accessoryView"];
[alertView show];
}

代码在Xcode 8.3.1上完美运行。您可以根据需求进行自定义。


-3

您可以使用以下语法,无需任何私有API。

alert.view.tintColor = alert.view.tintColor = UIColor.green
alert.setTitle(font: UIFont.boldSystemFont(ofSize: 26), color: UIColor.darkGray)
alert.setMessage(font: UIFont.systemFont(ofSize: 26), color: UIColor.darkGray)

alert


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接