美文网首页
修改UIAlertController的标题按钮的字体颜色、字号

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

作者: 伯牙呀 | 来源:发表于2017-06-23 14:13 被阅读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];
}

相关文章

网友评论

      本文标题:修改UIAlertController的标题按钮的字体颜色、字号

      本文链接:https://www.haomeiwen.com/subject/zimfcxtx.html