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

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

效果如下:


  • 2.设置按钮文字,就拿取消按钮距离:
    代码如下:
//修改按钮
  
[cancelAction setValue:[UIColor redColor] forKey:@"titleTextColor"];
效果如下:


君凯商联网-iOS-字唐名僧

猜你喜欢

转载自blog.csdn.net/u010960265/article/details/79711457
今日推荐