• [iOS]改变UIAlertController的标题、内容的字体和颜色


    https://www.jianshu.com/p/51949eec2e9c

    UIAlertController和UIAlertView

    https://www.jianshu.com/p/ddece8cf8ebe 

    iOS 修改UIAlertAction的字体大小

    https://www.jianshu.com/p/873fd35b18d4

    https://www.jianshu.com/p/f6752f7f8709

    https://www.cnblogs.com/tangyuanby2/p/7605476.html

    2016.03.23 22:36* 字数 272 阅读 37401评论 54

    在开发中,弹出框是必不可少的,通常情况下,我们只要弹出系统自带的弹出框就可以。but,在某些情况下,万恶的UI会要求你修改显示文字的大小、颜色,虽然系统自带有一种红色字体的UIAlertAction,但是这种Action并不能放在Cancel位置,所以,更多时候,需要我们自己修改文字字体和颜色。
    我采用的方法是KVC:
    正常情况下,我们配置出来的UIAlertController是这样的:

     

    123.png

    或者是这样:

     

    345.png

    代码如下:

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"提示内容" preferredStyle:UIAlertControllerStyleAlert];

    //    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"提示内容" preferredStyle:UIAlertControllerStyleActionSheet];

        

        UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Default" style:UIAlertActionStyleDefault handler:nil];

        UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:@"Destructive" style:UIAlertActionStyleDestructive handler:nil];

        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];

        

        [alertController addAction:defaultAction];

        [alertController addAction:destructiveAction];

        [alertController addAction:cancelAction];

        

        [self presentViewController:alertController animated:YES completion:nil];

    代码里展示了系统提供的三种UIAlertAction,现在我们要对文字的字体和颜色进行设置:

    • 1.标题和提示内容的文字设置
      代码如下:

    //修改title

        NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:@"提示"];

        [alertControllerStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];

        [alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, 2)];

        [alertController setValue:alertControllerStr forKey:@"attributedTitle"];

        //修改message

        NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:@"提示内容"];

        [alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 4)];

        [alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 4)];

        [alertController setValue:alertControllerMessageStr forKey:@"attributedMessage"];

    效果如下:

     

    123.png
    • 2.设置按钮文字,就拿取消按钮距离:
      代码如下:

    //修改按钮

        if (cancelAction valueForKey:@"titleTextColor") {

            [cancelAction setValue:[UIColor redColor] forKey:@"titleTextColor"];

        }

    效果如下:

     

    123.png

    至于里面的key值怎么得到的,过两天会写一篇文章来讲述。

    经常需要设置message左对齐、段间距这些属性


    - (IBAction)tapAlertControllerButton:(id)sender {
    NSString *titleString = @"商家发货规则";
    NSString *messageString = @"1、买家下单付款后,请尽快发货; 2、买家下单付款后,若超过最迟发货的时间,商家仍未发货,那么后台将自动取消订单; 3、若遇到物流高峰期,比如春节,双十一等,请点击【延迟发货】来延长最迟发货时间; 4、每个订单只允许【延迟发货】一次,每次可延迟5天;";
    NSString *alertTitle = @"知道了";

    UIAlertController *_alertController = [UIAlertController alertControllerWithTitle:titleString message:messageString preferredStyle:UIAlertControllerStyleAlert];
    [_alertController addAction:[UIAlertAction actionWithTitle:alertTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    }]];

    /* 修改title */
    NSMutableAttributedString *attTitleString = [[NSMutableAttributedString alloc] initWithString:titleString];
    [_alertController setValue:attTitleString forKey:@"attributedTitle"];

    /* 修改message */
    NSMutableAttributedString *attMsgString = [[NSMutableAttributedString alloc] initWithString:messageString];
    // 设置字体
    [attMsgString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, attMsgString.length)];
    // 设置颜色
    [attMsgString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 10)];

    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    // 设置行间距
    [paragraph setLineSpacing:3];
    // 设置段间距
    [paragraph setParagraphSpacingBefore:5];
    // 设置对齐方式
    [paragraph setAlignment:NSTextAlignmentLeft];
    // 设置书写方向
    [paragraph setBaseWritingDirection:NSWritingDirectionLeftToRight];
    [attMsgString addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, attMsgString.length)];
    [_alertController setValue:attMsgString forKey:@"attributedMessage"];

    /* 修改按钮的颜色 */
    NSArray *actionArr = [_alertController actions];
    [actionArr.firstObject setTitle:alertTitle];
    [actionArr.firstObject setValue:[UIColor orangeColor] forKey:@"titleTextColor"];

    [self presentViewController:_alertController animated:YES completion:nil];
    }
     
    示意图:
    系统默认样式:

     

    改变属性样式:



    ---------------------
    作者:枫志应明
    来源:CSDN
    原文:https://blog.csdn.net/wsyx768/article/details/60874420
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    mysql面试知识点
    计算机网络
    BFS
    拓扑排序
    双指针
    回溯算法
    hash表 算法模板和相关题目
    桶排序及其应用
    滑动窗口
    贪心算法
  • 原文地址:https://www.cnblogs.com/sundaysgarden/p/10309352.html
Copyright © 2020-2023  润新知