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文章

iOS设置圆角的三种方法

2020-1-9 10:58:01

iOS文章

iOS设置字体 systemFontOfSize: weight:

2020-1-9 11:12:02

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索