繁体   English   中英

在 UIAlertController 中更改标题颜色

[英]Change title color in UIAlertController

我有两个按钮,但我只想把一个改成红色。当我使用下面的功能时
它全部变为红色 我只想改变一个按钮的颜色。 我该怎么做?

alertController.view.tintColor = UIColor.redColor()
    let alertController = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)

alertController.setValue(NSAttributedString(string: title, attributes: [NSFontAttributeName : UIFont.appFont_OpenSans_Regular(fontSize: 15),NSForegroundColorAttributeName : BLACK_COLOR]), forKey: "attributedTitle")
alertController.setValue(NSAttributedString(string: message, attributes: [NSFontAttributeName : UIFont.appFont_OpenSans_Regular(fontSize: 13),NSForegroundColorAttributeName : APP_COLOR_BLUE_1]), forKey: "attributedMessage")

你可以试试这个

deleteAction.setValue(color, forKey: titleTextColor)

它对我有用!

斯威夫特

您需要将UIAlertActionStyle.Destructive用于红色的按钮文本颜色

    let alert = UIAlertController(
    title: "Basic alert style",
    message: "Basic alert With buttons",
    preferredStyle: .alert )

    let Reset = UIAlertAction(
    title: "Reset",
    style: .destructive) { (action) in
    // do your stuff
    }

    let Cancel = UIAlertAction(
    title: "Cancel", style: .default) { (action) in
    // do your stuff
    }

    alert.addAction(Reset)
    alert.addAction(Cancel)

    present(alert, animated: true, completion: nil)

目标-C

UIAlertController *alert = [UIAlertController
                          alertControllerWithTitle:@"Basic Alert style"
                          message:@"Basic Alert With Buttons"
                          preferredStyle:UIAlertControllerStyleAlert];

 UIAlertAction *Reset = [UIAlertAction 
        actionWithTitle:NSLocalizedString(@"Reset", @"Reset action")
                  style:UIAlertActionStyleDestructive
                handler:^(UIAlertAction *action)
                {
                  NSLog(@"Reset action");
                }];

UIAlertAction *Cancel = [UIAlertAction 
        actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action")
                  style:UIAlertActionStyleDefault
                handler:^(UIAlertAction *action)
                {
                  NSLog(@"Cancel action");
                }];

[alert addAction:Reset];
[alert addAction:Cancel];
[self presentViewController:alert animated:YES completion:nil];

输出

在此处输入图片说明

有关其他信息,请参阅

设置 UIAlertActionStyle.Destructive 时,只能使用红色

检查此链接

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM