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

很简单!!!

-(void)touchesBegan:(NSSet<UITouch *> *)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
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容