开发过程中,我们经常用到弹窗界面,各式各样的弹窗,五花八门,眼花缭乱,个人感觉,还是系统的UI看着让人舒服!
那么实现一个弹窗,你可能首先想到的是UIAlertView,但是这个类苹果在iOS9.0就废弃了:
NS_CLASS_DEPRECATED_IOS(2_0, 9_0, “UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead”)
从他的提示看,系统希望我们使用UIAlertController来代替.当然,我相信每一个iOS开发者都能熟练的写出UIAlertController弹窗.今天我们来说说简单的设置弹窗标题、消息文字color和font,以及按钮color的实现.
-
我们先来创建一个弹窗
/* UIAlertControllerStyleActionSheet //底部 UIAlertControllerStyleAlert //中间弹窗 */ UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"通知" message:@"你好吗!你好吗!你好吗!你好吗!你好吗!你好吗!你好吗!你好吗!你好吗!你好吗!你好吗!你好吗!你好吗!你好吗!你好吗!你好吗!" preferredStyle:UIAlertControllerStyleAlert];//使用弹窗模式 /* UIAlertActionStyleDefault,默认 显示蓝色 UIAlertActionStyleCancel,取消 显示蓝色 UIAlertActionStyleDestructive,警告 显示红色 */ //添加事件 UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { NSLog(@"取消"); }]; UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"确定"); }]; //添加action [alertVC addAction:cancelAction]; [alertVC addAction:confirmAction]; //展示 [self presentViewController:alertVC animated:YES completion:nil];
-
下面我们创建两个分类:UIAlertController的分类UIAlertController+WMAlertSetting.h"和UIAlertAction的分类"UIAlertAction+WMActionSetting.h".然后通过runtime绑定属性来设置.
UIAlertController的分类
-
UIAlertController+WMAlertSetting.h声明
#import <UIKit/UIKit.h> @interface UIAlertController (WMAlertSetting) //标题颜色 @property (nonatomic ,strong) UIColor *titleColor; //标题字体 @property (nonatomic ,strong) UIFont *titleFont; //设置标题的富文本属性 @property (nonatomic ,strong) NSAttributedString *titleAttributedString; //message颜色 @property (nonatomic ,strong) UIColor *messageColor; //message字体 @property (nonatomic ,strong) UIFont *messageFont; //设置message的富文本属性 @property (nonatomic ,strong) NSAttributedString *messageAttributedString; @end
-
UIAlertController+WMAlertSetting.m实现
#import "UIAlertController+WMAlertSetting.h" #import <objc/runtime.h> @implementation UIAlertController (WMAlertSetting) //自己来实现 setter 和 getter @dynamic titleColor; @dynamic titleFont; @dynamic messageColor; @dynamic messageFont; /** titleColor 标题颜色的 getter 和 setter */ static char *titleColorKey = "titleColorKey"; - (UIColor *)titleColor { id color = objc_getAssociatedObject(self, titleColorKey); if (color == nil) { return [UIColor darkTextColor]; } else { return color; } } - (void)setTitleColor:(UIColor *)titleColor { objc_setAssociatedObject(self, titleColorKey, titleColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC); NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self.title attributes:@{NSFontAttributeName:self.titleFont,NSForegroundColorAttributeName:titleColor}]; [self setValue:attributedString forKey:@"attributedTitle"]; } /** titleFont 标题字体的 getter 和 setter */ static char *titleFontKey = "titleFontKey"; - (UIFont *)titleFont { id font = objc_getAssociatedObject(self, titleFontKey); if (font == nil) { return [UIFont systemFontOfSize:15]; } else { return font; } } - (void)setTitleFont:(UIFont *)titleFont { objc_setAssociatedObject(self, titleFontKey, titleFont, OBJC_ASSOCIATION_RETAIN_NONATOMIC); NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self.title attributes:@{NSFontAttributeName:titleFont,NSForegroundColorAttributeName:self.titleColor}]; [self setValue:attributedString forKey:@"attributedTitle"]; } /** messageColor message颜色的 getter 和 setter */ static char *messageColorKey = "messageColorKey"; - (UIColor *)messageColor { id color = objc_getAssociatedObject(self, messageColorKey); if (color == nil) { return [UIColor darkGrayColor]; } else { return color; } } - (void)setMessageColor:(UIColor *)messageColor { objc_setAssociatedObject(self, messageColorKey, messageColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC); NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self.message attributes:@{NSFontAttributeName:self.messageFont,NSForegroundColorAttributeName:messageColor}]; [self setValue:attributedString forKey:@"attributedMessage"]; } /** messageColor message字体的 getter 和 setter */ static char *messageFontKey = "messageFontKey"; - (UIFont *)messageFont { id font = objc_getAssociatedObject(self, messageFontKey); if (font == nil) { return [UIFont systemFontOfSize:13]; } else { return font; } } - (void)setMessageFont:(UIFont *)messageFont { objc_setAssociatedObject(self, messageFontKey, messageFont, OBJC_ASSOCIATION_RETAIN_NONATOMIC); NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self.message attributes:@{NSFontAttributeName:messageFont,NSForegroundColorAttributeName:self.messageColor}]; [self setValue:attributedString forKey:@"attributedMessage"]; } /** titleAttributedString 为标题赋值的富文本属性 getter 和 setter */ static char *titleAttributedStringKey = "titleAttributedStringKey"; - (NSAttributedString *)titleAttributedString { return [self valueForKey:@"attributedTitle"]; } - (void)setTitleAttributedString:(NSAttributedString *)titleAttributedString { objc_setAssociatedObject(self, titleAttributedStringKey, titleAttributedString, OBJC_ASSOCIATION_RETAIN_NONATOMIC); [self setValue:titleAttributedString forKey:@"attributedTitle"]; } /** messageAttributedString 为message赋值的富文本属性 getter 和 setter */ static char *messageAttributedStringKey = "messageAttributedStringKey"; - (NSAttributedString *)messageAttributedString { return [self valueForKey:@"attributedMessage"]; } - (void)setMessageAttributedString:(NSAttributedString *)messageAttributedString { objc_setAssociatedObject(self, messageAttributedStringKey, messageAttributedString, OBJC_ASSOCIATION_RETAIN_NONATOMIC); [self setValue:messageAttributedString forKey:@"attributedMessage"]; } @end
UIAlertAction的分类
-
UIAlertAction+WMActionSetting.h声明
#import <UIKit/UIKit.h> @interface UIAlertAction (WMActionSetting) //按钮title的颜色 @property (nonatomic ,strong) UIColor *titleColor; @end
-
UIAlertAction+WMActionSetting.m实现
#import "UIAlertAction+WMActionSetting.h" #import <objc/runtime.h> @implementation UIAlertAction (WMActionSetting) @dynamic titleColor; static char *titleColorKey = "titleTextColor";///按钮title颜色 - (UIColor *)titleColor { id color = objc_getAssociatedObject(self, titleColorKey); if (color == nil) { return [UIColor lightGrayColor]; } else { return color; } return objc_getAssociatedObject(self, titleColorKey); } - (void)setTitleColor:(UIColor *)titleColor { objc_setAssociatedObject(self, titleColorKey, titleColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC); [self setValue:titleColor forKey:@"titleTextColor"]; } @end
使用分类实现弹窗
-
先看效果
/* UIAlertControllerStyleActionSheet //底部 UIAlertControllerStyleAlert //中间弹窗 */ UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"通知" message:@"你好吗!你好吗!你好吗!你好吗!你好吗!你好吗!你好吗!你好吗!你好吗!你好吗!你好吗!你好吗!你好吗!你好吗!你好吗!你好吗!" preferredStyle:UIAlertControllerStyleAlert]; alertVC.titleFont = [UIFont systemFontOfSize:16]; alertVC.titleColor = [UIColor redColor]; alertVC.messageFont = [UIFont systemFontOfSize:14]; alertVC.messageColor = [UIColor purpleColor]; /* UIAlertActionStyleDefault, UIAlertActionStyleCancel, UIAlertActionStyleDestructive */ UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { NSLog(@"取消"); }]; cancelAction.titleColor = [UIColor greenColor]; UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; confirmAction.titleColor = [UIColor orangeColor]; [alertVC addAction:cancelAction]; [alertVC addAction:confirmAction]; [self presentViewController:alertVC animated:YES completion:nil];
添加TextField输入框
-
UIAlertController类中有一个对象方法:
//此方法只能使用UIAlertControllerStyleAlert样式时使用,否则崩溃 - (void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler; //在此回调中,设置textField的属性及添加监听; //在UIAlertController添加的action回调中,获取用户输入的文字.
-
展示: