Change title color in UIAlertController

微笑、不失礼 提交于 2019-12-07 14:24:43

问题


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

回答1:


only red Color is possible when you set UIAlertActionStyle.Destructive

Check this link

UIAlertController custom font, size, color




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



回答3:


You can try this,

deleteAction.setValue(color, forKey: titleTextColor)

It works for me!




回答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: UIAlertControllerStyle.Alert )

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

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

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

 presentViewController(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

for additional Information see this



来源:https://stackoverflow.com/questions/36662233/change-title-color-in-uialertcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!