UIAlertController的使用以及对字体颜色大小的修改

在我们的日常开发中弹窗可以说是一个非常常用的技能,弹窗的方式也是有很多种。之前用的最多的就是UIAlertView,但是iOS8之后可能UIAlertController用的就比较多了。用的多了要求也就多了,可能有些时候系统默认的字体大小、文字颜色不能满足我们的需要,这时候就要求我们能够修改文字的的颜色大小等。下边我就介绍一下利用KVC修改UIAlertController的标题以及内容的字体颜色

正常弹窗代码

   // 初始化UIAlertController
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"此处展示提示消息" preferredStyle:UIAlertControllerStyleActionSheet];
    // 添加UIAlertAction
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"确认");
    }];
    [alertController addAction:sureAction];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消");
    }];
    [alertController addAction:cancelAction];

    UIAlertAction *testActionOne = [UIAlertAction actionWithTitle:@"测试1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"测试1");
    }];
    [alertController addAction:testActionOne];

    UIAlertAction *testActionTwo = [UIAlertAction actionWithTitle:@"测试2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"测试2");
    }];
    [alertController addAction:testActionTwo];
     // 弹窗提示窗口
    [self presentViewController:alertController animated:YES completion:nil];

弹窗展示效果如图:
这里写图片描述

修改弹窗字体颜色

下边为修改标题提示信息文字颜色大小以及修改内容字体颜色的完整方法,想偷懒的同学可以直接将下方的代码复制到想要弹窗的位置,然后根据自己的需求修改对应的数值即可

    // 初始化UIAlertController
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];

    //修改title字体及颜色
    NSMutableAttributedString *titleStr = [[NSMutableAttributedString alloc] initWithString:@"标题"];
    [titleStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, titleStr.length)];
    [titleStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, titleStr.length)];
    [alertController setValue:titleStr forKey:@"attributedTitle"];

    // 修改message字体及颜色
    NSMutableAttributedString *messageStr = [[NSMutableAttributedString alloc] initWithString:@"此处展示提示消息"];
    [messageStr addAttribute:NSForegroundColorAttributeName value:[UIColor purpleColor] range:NSMakeRange(0, messageStr.length)];
    [messageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:NSMakeRange(0, messageStr.length)];
    [alertController setValue:messageStr forKey:@"attributedMessage"];


    // 添加UIAlertAction
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"确认");
    }];
    // KVC修改字体颜色
    [sureAction setValue:[UIColor blueColor] forKey:@"_titleTextColor"];
    [alertController addAction:sureAction];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消");
    }];
    [cancelAction setValue:[UIColor blackColor] forKey:@"_titleTextColor"];
    [alertController addAction:cancelAction];

    UIAlertAction *testActionOne = [UIAlertAction actionWithTitle:@"测试1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"测试1");
    }];
    [testActionOne setValue:[UIColor greenColor] forKey:@"_titleTextColor"];
    [alertController addAction:testActionOne];

    UIAlertAction *testActionTwo = [UIAlertAction actionWithTitle:@"测试2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"测试2");
    }];
    [testActionTwo setValue:[UIColor greenColor] forKey:@"_titleTextColor"];
    [alertController addAction:testActionTwo];

    // 弹窗提示窗口
    [self presentViewController:alertController animated:YES completion:nil];

利用上述代码修改后的效果图展示如下:
这里写图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值