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@interfaceSCAlertAction:UIAlertAction@property(nonatomic,strong)UIColor*textColor;/**< 按钮title字体颜色 */@end@interfaceSCAlertController: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@implementationSCAlertAction//按钮标题的字体颜色-(void)setTextColor:(UIColor*)textColor{    _textColor = textColor;unsignedintcount =0;    Ivar *ivars = class_copyIvarList([UIAlertActionclass], &count);for(inti =0;i < count;i ++){        Ivar ivar = ivars[i];NSString*ivarName = [NSStringstringWithCString:ivar_getName(ivar) encoding:NSUTF8StringEncoding];if([ivarName isEqualToString:@"_titleTextColor"]) {            [selfsetValue:textColor forKey:@"titleTextColor"];        }    }}@end@implementationSCAlertController-(void)viewDidLoad{    [superviewDidLoad];unsignedintcount =0;    Ivar *ivars = class_copyIvarList([UIAlertControllerclass], &count);for(inti =0;i < count;i ++){        Ivar ivar = ivars[i];NSString*ivarName = [NSStringstringWithCString:ivar_getName(ivar) encoding:NSUTF8StringEncoding];//标题颜色if([ivarName isEqualToString:@"_attributedTitle"] &&self.title&&self.titleColor) {NSMutableAttributedString*attr = [[NSMutableAttributedStringalloc]initWithString:self.titleattributes:@{NSForegroundColorAttributeName:self.titleColor,NSFontAttributeName:[UIFontboldSystemFontOfSize:14.0]}];            [selfsetValue:attr forKey:@"attributedTitle"];        }//描述颜色if([ivarName isEqualToString:@"_attributedMessage"] &&self.message&&self.messageColor) {NSMutableAttributedString*attr = [[NSMutableAttributedStringalloc] initWithString:self.messageattributes:@{NSForegroundColorAttributeName:self.messageColor,NSFontAttributeName:[UIFontsystemFontOfSize:14.0]}];            [selfsetValue:attr forKey:@"attributedMessage"];        }    }//按钮统一颜色if(self.tintColor) {for(SCAlertAction*actioninself.actions) {if(!action.textColor) {                action.textColor=self.tintColor;            }        }    }}@end

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

-(void)viewDidLoad{    [superviewDidLoad];  }

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

SCAlertController*alertController = [SCAlertControlleralertControllerWithTitle:@"你确定要退出吗?"message:nilpreferredStyle:UIAlertControllerStyleActionSheet];        alertController.tintColor= [UIColorredColor];//这里统一设置各个按钮的颜色都为红色.当然,你还可以自定义某一个按钮的颜色.比如下面的取消按钮        alertController.titleColor= [UIColorredColor];//取消SCAlertAction*cancelAction = [SCAlertActionactionWithTitle:@"我不想退出"style:UIAlertActionStyleCancelhandler:nil];//单独修改一个按钮的颜色cancelAction.textColor= [UIColorgreenColor];        [alertController addAction:cancelAction];//退出SCAlertAction*exitAction = [SCAlertActionactionWithTitle:@"退出"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction* _Nonnull action) {        }];        [alertController addAction:exitAction];        [selfpresentViewController:alertController animated:YEScompletion:nil];    }

文/青春微凉来时路(作者)

原文链接:http://www.jianshu.com/p/cecd1b4bbf27

著作权归作者所有,转载请联系作者获得授权,并标注“作者”。

你可能感兴趣的:(UIAlertController 简单修改title以及按钮的字体颜色)