Change title color in UIAlertController

11,714

Solution 1

only red Color is possible when you set UIAlertActionStyle.Destructive

Check this link

UIAlertController custom font, size, color

Solution 2

    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")

Solution 3

You can try this,

deleteAction.setValue(color, forKey: titleTextColor)

It works for me!

Solution 4

Swift

you need to use UIAlertActionStyle.Destructive for button text color in red

    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)

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

output

enter image description here

for additional Information see this

Share:
11,714

Related videos on Youtube

Anbu.Karthik
Author by

Anbu.Karthik

I will show my love in your trouble. Contact: +91 - 87 54 846 846

Updated on June 06, 2022

Comments

  • Anbu.Karthik
    Anbu.Karthik about 2 years

    I have two button but I just wanna change one to red.When I use the function below
    it change all to red. I just want to change color of only one button. How can i do it?

    alertController.view.tintColor = UIColor.redColor()
    
  • Andrew Edwards
    Andrew Edwards over 5 years
    This is the best answer! Works well