UIAlertController弹窗

开发过程中,我们经常用到弹窗界面,各式各样的弹窗,五花八门,眼花缭乱,个人感觉,还是系统的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
    
使用分类实现弹窗
  • 先看效果
    自定义UIAlertControllerStyleAlert样式
    自定义UIAlertControllerStyleActionSheet样式

      /*
      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回调中,获取用户输入的文字.
    
  • 展示:
    带文本输入框的弹窗

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UIAlertController一般用于提示用户某些信息或进行一些简单的操作。但是,它并不支持在内容中包含超链接。因此,我们需要自定义UIAlertController,并在其中添加一个可点击的UILabel来实现超链接跳转。 以下是示例代码: ``` UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"这是一个带超链接的提示框" preferredStyle:UIAlertControllerStyleAlert]; // 创建富文本字符串,并添加超链接 NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:@"点击这里跳转到百度"]; [attributedStr addAttribute:NSLinkAttributeName value:@"http://www.baidu.com" range:NSMakeRange(0, attributedStr.length)]; // 创建一个可点击的UILabel UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 30)]; label.attributedText = attributedStr; label.textAlignment = NSTextAlignmentCenter; label.userInteractionEnabled = YES; [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapLabel:)]]; // 将UILabel添加到UIAlertController的view中 [alertController.view addSubview:label]; // 创建并添加UIAlertAction UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"点击了确定按钮"); }]; [alertController addAction:okAction]; // 显示UIAlertController [self presentViewController:alertController animated:YES completion:nil]; ``` 在以上代码中,我们创建了一个UIAlertController,并在其中添加了一个UILabel,将UILabel的attributedText设置为带有超链接的富文本字符串,然后添加了一个手势识别器,当用户点击UILabel时,会调用tapLabel方法,在该方法中使用UIApplication打开URL来跳转到指定的网页。最后,我们添加了一个UIAlertAction,将UIAlertController显示出来。 以下是tapLabel方法的示例实现: ``` - (void)tapLabel:(UITapGestureRecognizer *)gestureRecognizer { UILabel *label = (UILabel *)gestureRecognizer.view; NSRange range = [label.attributedText.string rangeOfString:@"点击这里跳转到百度"]; if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { if (range.location != NSNotFound) { NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil]; } } } ``` 在tapLabel方法中,我们首先获取被点击的UILabel,然后查找UILabel的attributedText中是否包含指定的文本,如果包含,则使用UIApplication打开URL来跳转到指定的网页。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值