iOS改变UIAlertView、UIActionSheet、UIAlertController系统字体颜色

 废话不多说,直接上代码,效果是最好的说服力

1、改变UIAlertView字体颜色

[UIView appearance].tintColor = [UIColor greenColor];

 由于在iOS 8推出UIAlertController取代UIAlertView,关于UIAlertController修改颜色请见下面 3

个人还是比较喜欢使用UIAlertView,简单粗暴达到想要的效果

2、改变UIActionSheet字体颜色

// 改变 actionSheet字体颜色

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {

    

    SEL selector = NSSelectorFromString(@"_alertController");

    

    if ([actionSheet respondsToSelector:selector])//ios8

    {

        

        UIAlertController *alertController = [actionSheet valueForKey:@"_alertController"];

        

        if ([alertController isKindOfClass:[UIAlertController class]]) {

            

            alertController.view.tintColor = [UIColor greenColor];

        }

    //ios7

    } else{

        

        for( UIView * subView in actionSheet.subviews ) {

            

            if( [subView isKindOfClass:[UIButton class]] ) {

                

                UIButton * btn = (UIButton*)subView;

            

                [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

            }

        }

    }

}

3、改变UIAlertController字体颜色

- (void)buttonClick:(UIButton *)sender {

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"提示内容" preferredStyle:UIAlertControllerStyleAlert];

    //修改标题的内容,字号,颜色。使用的key值是“attributedTitle”

    NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"heihei"];

    [hogan addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:50] range:NSMakeRange(0, [[hogan string] length])];

    [hogan addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, [[hogan string] length])];

    [alertController setValue:hogan forKey:@"attributedTitle"];

    //修改按钮的颜色,同上可以使用同样的方法修改内容,样式

    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Default" style:UIAlertActionStyleDefault handler:nil];

    [defaultAction setValue:[UIColor blueColor] forKey:@"_titleTextColor"];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];

    [cancelAction setValue:[UIColor greenColor] forKey:@"_titleTextColor"];

    [alertController addAction:defaultAction];

    [alertController addAction:cancelAction];

    [self presentViewController:alertController animated:YES completion:nil];

}

原文地址:https://www.cnblogs.com/yang-shuai/p/6673145.html