美文网首页IOS 知识积累iOS DeveloperiOS
iOS开发UIAlertViewController字体颜色修改

iOS开发UIAlertViewController字体颜色修改

作者: 重驹 | 来源:发表于2017-01-09 13:57 被阅读1152次

    首先我们看一下修改后的效果:

    效果.png

    具体的方法如下:

    NSString *message = @"请确认信息是否正确?";
    NSString *title = @"提示";
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    //改变title的大小和颜色
    NSMutableAttributedString *titleAtt = [[NSMutableAttributedString alloc] initWithString:title];
    [titleAtt addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, title.length)];
    [titleAtt addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, title.length)];
    [alertController setValue:titleAtt forKey:@"attributedTitle"];
    //改变message的大小和颜色
    NSMutableAttributedString *messageAtt = [[NSMutableAttributedString alloc] initWithString:message];
    [messageAtt addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:NSMakeRange(0, message.length)];
    [messageAtt addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(0, message.length)];
    [alertController setValue:messageAtt forKey:@"attributedMessage"];
     [self presentViewController:alertController animated:YES completion:nil];
    

    上面的是修改UIAlertViewController的title和message字体的大小和颜色,采用的是修改attributedString其中的NSForegroundColorAttributeName颜色属性和NSFontAttributeName字体大小属性。UIAlertViewController中的标题的key:@"attributedTitle",标题中提示信息的key:@"attributedMessage"。

    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:nil];
    [alertAction setValue:[UIColor purpleColor] forKey:@"_titleTextColor"];
    //    alertController.view.tintColor = [UIColor greenColor];
    [alertController addAction:alertAction];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    /*取消按钮的颜色*/
    [cancel setValue:[UIColor redColor] forKey:@"_titleTextColor"];
    [alertController addAction:cancel];
    

    修改UIAlertViewController中修改所有按钮的颜色使用下面这个方法:

    alertController.view.tintColor = [UIColor greenColor];
    

    修改单个UIAlertAction按钮的字体颜色使用下面这个方法:

    [cancel setValue:[UIColor redColor] forKey:@"_titleTextColor"];
    

    有人可能会问,你怎么知道Alertaction中有"_titleTextColor"这个key呢?其实很简单,我们可以通过runtime获取这个类中所有的属性,代码如下

    unsigned int count;
    Ivar *ivars =  class_copyIvarList([UIAlertAction class], &count);
    for (int i = 0; i < count; i++) {
        Ivar ivar = ivars[i];
        const char * cName =  ivar_getName(ivar);
        NSString *ocName = [NSString stringWithUTF8String:cName];
        MYLog(@"这里是runtime获取到alertaction的属性  %@",ocName);
    }
    free(ivars);
    

    打印的日志如下

    dlog.png

    会点英语的老铁们应该知道每个单词对应的属性大概是干什么事情的吧,我们在这里只修改颜色,所以重用到了"_titleTextColor"这个key。
    如果这个能解决您的问题,请给个👍,谢谢!我还有个需求,改变这个字体大小的方法,我没找到,如果有知道的大神,请不吝赐教!

    相关文章

      网友评论

        本文标题:iOS开发UIAlertViewController字体颜色修改

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