XZ_iOS 之UIAlertController 修改title、message和action颜色、大小

7 篇文章 0 订阅

效果图如下:

之前看了很多的博客,都是先获取 Ivar属性,然后判断是title还是message再进行修改,其实不用,可以直接拿到title和message进行修改,代码如下:

/**
 简单的文字弹窗,带’确定‘文字

 @param title 题目
 @param message 内容
 @param target 目标控制器
 */
+ (void)showAlertWithTitle:(NSString *)title message:(NSString *)message target:(nullable id)target {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle: title message: message preferredStyle: UIAlertControllerStyleAlert];
    
    // title 颜色
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString: title attributes: @{NSForegroundColorAttributeName: [UIColor greenColor]}];
    [alertController setValue: attr forKey: @"attributedTitle"];
    // message颜色
    NSMutableAttributedString *attrMessage = [[NSMutableAttributedString alloc]initWithString: message attributes: @{NSForegroundColorAttributeName: [UIColor redColor], NSFontAttributeName:[UIFont boldSystemFontOfSize: 12.0]}];
    [alertController setValue: attrMessage forKey: @"attributedMessage"];
    // UIAlertAction颜色
    UIAlertAction *sure = [UIAlertAction actionWithTitle: @"确定" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alertController addAction: sure];
    [sure setValue: [UIColor darkGrayColor] forKey: @"titleTextColor"];
    
    [target presentViewController: alertController animated: YES completion:nil];
}

我把上面的方法封装在了的分类中,调用十分的方便,如下:

[UIAlertController showAlertWithTitle: @"标题" message: @"\n内容\n" target: self];

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值