iOS UIAlertController上修改标题(title) 消息(message) 按钮 的字体大小及颜色

本文介绍了如何修改iOS UIAlertController的标题、消息和按钮的字体大小及颜色。通过提供GitHub Demo链接和引用Stack Overflow上的相关资源,展示了具体的实现步骤。

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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值