Demo GitHub地址:点击打开链接
之前的程序做了一个可以输入的消息弹出框,样式如下:
那么问题就来了,我现在想修改一下title、message、按钮的字体大小和颜色,在网上查阅了一些资料,完成了修改。
<span style="font-size:24px;">oc代码如下:</span><span style="font-size:24px; font-family: Arial, Helvetica, sans-serif;">使用</span><span class="s1" style="font-size:24px; font-family: Arial, Helvetica, sans-serif;">KVC的方式</span>
<span style="font-size:18px;"> UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请修改输入内容" preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
textField.placeholder = @"内容";
// [textField setFont:[UIFont systemFontOfSize:16]];
}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
UITextField *login = alertController.textFields.firstObject;
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
/*title*/
NSMutableAttributedString *alertTitleStr = [[NSMutableAttributedString alloc] initWithString:@"提示"];
[alertTitleStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 2)];
[alertTitleStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
[alertController setValue:alertTitleStr forKey:@"attributedTitle"];
/*message*/
NSMutableAttributedString *alertMessageStr = [[NSMutableAttributedString alloc] initWithString:@"请修改输入内容"];
[alertMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:NSMakeRange(0, 7)];
[alertMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 3)];
[alertMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(3, 2)];
[alertMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, 2)];
[alertController setValue:alertMessageStr forKey:@"attributedMessage"];
/*取消按钮的颜色*/
[cancel setValue:[UIColor redColor] forKey:@"_titleTextColor"];
[alertController addAction:cancel];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];</span>
参考了文章:http://stackoverflow.com/questions/26460706/uialertcontroller-custom-font-size-color
本文介绍了如何修改iOS UIAlertController的标题、消息和按钮的字体大小及颜色。通过提供GitHub Demo链接和引用Stack Overflow上的相关资源,展示了具体的实现步骤。

9442

被折叠的 条评论
为什么被折叠?



