iOS 修改UIAlertController的标题按钮的字体颜色、字号、内容

自定义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];
}
特别声明:文章多为网络转载,资源使用一般不提供任何帮助,特殊资源除外,如有侵权请联系!
技术狗 » iOS 修改UIAlertController的标题按钮的字体颜色、字号、内容