首页 > 代码库 > UIAlertController 简单修改title以及按钮的字体颜色

UIAlertController 简单修改title以及按钮的字体颜色

苦逼的开发者,最终败给了一个任性的UI,系统原生UIAlertController的按纽颜色必须改.于是,开始了不归路.
之前的版本是自己用view写的一个仿系统UIActionSheet,动画感觉都挺好,就是毛玻璃背景没有系统的好,由于最低兼容了ios8,所以就抛弃了UIActionSheet,改用UIAlertController.

做法其实很简单,采用runtime机制.对于runtime不了解的,我想还是别看各种介绍文章了,找一个简单的demo多写几遍,就行了.

做法很简单,自己写一个类 继承自UIAlertController,还是先把.h和.m的代码都给大家吧.

SCAlertController.h

////  SCAlertController.h//  SCAlertController////  Created by it3部01 on 16/8/3.//  Copyright ? 2016年 benben. All rights reserved.//#import @interface SCAlertAction : UIAlertAction@property (nonatomic,strong) UIColor *textColor; /**< 按钮title字体颜色 */@end@interface SCAlertController : UIAlertController@property (nonatomic,strong) UIColor *tintColor; /**< 统一按钮样式 不写系统默认的蓝色 */@property (nonatomic,strong) UIColor *titleColor; /**< 标题的颜色 */@property (nonatomic,strong) UIColor *messageColor; /**< 信息的颜色 */@end

SCAlertController.m

////  SCAlertController.m//  SCAlertController////  Created by it3部01 on 16/8/3.//  Copyright ? 2016年 benben. All rights reserved.//#import "SCAlertController.h"#import @implementation SCAlertAction//按钮标题的字体颜色-(void)setTextColor:(UIColor *)textColor{    _textColor = textColor;    unsigned int count = 0;    Ivar *ivars = class_copyIvarList([UIAlertAction class], &count);    for(int i =0;i < count;i ++){        Ivar ivar = ivars[i];        NSString *ivarName = [NSString stringWithCString:ivar_getName(ivar) encoding:NSUTF8StringEncoding];        if ([ivarName isEqualToString:@"_titleTextColor"]) {            [self setValue:textColor forKey:@"titleTextColor"];        }    }}@end@implementation SCAlertController-(void)viewDidLoad{    [super viewDidLoad];    unsigned int count = 0;    Ivar *ivars = class_copyIvarList([UIAlertController class], &count);    for(int i = 0;i < count;i ++){        Ivar ivar = ivars[i];        NSString *ivarName = [NSString stringWithCString:ivar_getName(ivar) encoding:NSUTF8StringEncoding];        //标题颜色        if ([ivarName isEqualToString:@"_attributedTitle"] && self.title && self.titleColor) {            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:self.title attributes:@{NSForegroundColorAttributeName:self.titleColor,NSFontAttributeName:[UIFont boldSystemFontOfSize:14.0]}];            [self setValue:attr forKey:@"attributedTitle"];        }        //描述颜色        if ([ivarName isEqualToString:@"_attributedMessage"] && self.message && self.messageColor) {            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:self.message attributes:@{NSForegroundColorAttributeName:self.messageColor,NSFontAttributeName:[UIFont systemFontOfSize:14.0]}];            [self setValue:attr forKey:@"attributedMessage"];        }    }    //按钮统一颜色    if (self.tintColor) {        for (SCAlertAction *action in self.actions) {            if (!action.textColor) {                action.textColor = self.tintColor;            }        }    }}@end

一般来说对于SCAlertController里面的属性应该像SCAlertAction一样放在setter方法里面修改,这里我表示为了方便就放在这个方法里面了.

-(void)viewDidLoad{    [super viewDidLoad];   }

用法就很简单了,和系统原生UIAlertController一样,只是把UI换成SC,当然你可以改成自己喜欢的,但是别忘了改完.

SCAlertController *alertController = [SCAlertController alertControllerWithTitle:@"你确定要退出吗?" message:nil preferredStyle:UIAlertControllerStyleActionSheet];        alertController.tintColor = [UIColor redColor]; //这里统一设置各个按钮的颜色都为红色.当然,你还可以自定义某一个按钮的颜色.比如下面的取消按钮        alertController.titleColor = [UIColor redColor];        //取消        SCAlertAction *cancelAction = [SCAlertAction actionWithTitle:@"我不想退出" style:UIAlertActionStyleCancel handler:nil];        //单独修改一个按钮的颜色        cancelAction.textColor = [UIColor greenColor];        [alertController addAction:cancelAction];        //退出        SCAlertAction *exitAction = [SCAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {        }];        [alertController addAction:exitAction];        [self presentViewController:alertController animated:YES completion:nil];    }

UIAlertController 简单修改title以及按钮的字体颜色