修改UIAlertController的标题按钮的字体颜色、字号
2017-06-23 本文已影响127人
伯牙呀
自定义UIAlertController
代码:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
// 修改title、message的内容、字号、颜色,使用的key值是 "attributedTitle" 和 "attributedMessage"
NSMutableAttributedString *message = [[NSMutableAttributedString alloc] initWithString:alertController.message];
// 修改对齐方式
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setAlignment:NSTextAlignmentLeft];
[message addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [[message string] length])];
[message addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:NSMakeRange(0, [[message string] length])];
[message addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, [[message string] length])];
[alertController setValue:message forKey:@"attributedMessage"];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了Cancel");
}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了OK");
}];
// 修改按钮的颜色
[cancelAction setValue:[UIColor blueColor] forKey:@"_titleTextColor"];
[okAction setValue:[UIColor greenColor] forKey:@"_titleTextColor"];
[alertController addAction:okAction];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
}