代码之家  ›  专栏  ›  技术社区  ›  user6005637

更改UIAlertController中的标题颜色

  •  6
  • user6005637  · 技术社区  · 9 年前

    我有两个按钮,但我只想把一个改成红色
    它将全部更改为 红色 .我只想更改一个按钮的颜色。我该怎么做?

    alertController.view.tintColor = UIColor.redColor()
    
    4 回复  |  直到 9 年前
        1
  •  12
  •   Dhaval H. Nena    7 年前
        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")
    
        2
  •  7
  •   Phillip    8 年前

    你可以试试这个,

    deleteAction.setValue(color, forKey: titleTextColor)
    

    它对我有用!

        3
  •  3
  •   Anbu.Karthik    5 年前

    敏捷的

    你需要使用 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];
    

    输出

    enter image description here

    有关更多信息,请参阅 this

        4
  •  2
  •   Community CDub    8 年前

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

    检查此链接

    UIAlertController custom font, size, color