修改UIAlertController的title 、message 以及button颜色

很简单!!!

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"嘿嘿嘿" message:@"修改你的字体颜色和大小" preferredStyle:UIAlertControllerStyleAlert];
//修改标题的内容,字号,颜色。使用的key值是“attributedTitle”
NSMutableAttributedString *attrTitleStr = [[NSMutableAttributedString alloc]initWithString:@"嘿嘿嘿"];
[attrTitleStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor greenColor]} range:NSMakeRange(0, attrTitleStr.length)];
[alertController setValue:attrTitleStr forKey:@"_attributedTitle"];

NSMutableAttributedString *attrMessageStr = [[NSMutableAttributedString alloc]initWithString:@"修改你的字体颜色和大小"];
[attrMessageStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor greenColor]} range:NSMakeRange(0, attrMessageStr.length)];
[alertController setValue:attrMessageStr forKey:@"_attributedMessage"];



//修改按钮的颜色 使用的key值可以使_titleTextColor
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"default" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    
}];
[defaultAction setValue:[UIColor blueColor] forKey:@"_titleTextColor"];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    
}];
[cancelAction setValue:[UIColor yellowColor] forKey:@"_titleTextColor"];

[alertController addAction:defaultAction];
[alertController addAction:cancelAction];

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

}

好 我们看一下效果


Simulator Screen Shot - iPhone 8 Plus - 2018-01-30 at 19.07.35.png

你可能感兴趣的:(修改UIAlertController的title 、message 以及button颜色)