引言:

关于提示框, 系统自带的提示框有时可能满足不了我们的需求, 比如一个提示框的取消按钮我需要灰色字体显示, 这时候就需要自定义提示框的样式了。

示例图

苹果自iOS8开始,就已经废弃了之前用于界面提醒的UIAlertView类以及UIActionSheet,取而代之的是UIAlertController以及UIAlertAction,从实际使用情况来看,苹果把之前不同类型/样式的通知实现方法进行了统一,简化了有关提醒功能的实现。

UIAlertController的基本使用

一个简单的提示框:

    UIAlertController *alert    = [UIAlertController alertControllerWithTitle:@"标题" message:@"正文" preferredStyle:(UIAlertControllerStyleAlert)];

    UIAlertAction *okAction     = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
// 点击确定按钮时 要进行的操作可以写到这里
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
// 点击取消按钮时 要进行的操作可以写到这里
}]; [alert addAction:cancelAction];
[alert addAction:okAction]; [self presentViewController:alert animated:YES completion:nil];

自定义UIAlertController

主要是使用kvc的方式来自定义UIAlertController的样式:

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"内容" preferredStyle:UIAlertControllerStyleAlert];

    // 使用富文本来改变alert的title字体大小和颜色
NSMutableAttributedString *titleText = [[NSMutableAttributedString alloc] initWithString:@"这里是标题"];
[titleText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:24] range:NSMakeRange(0, 2)];
[titleText addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
[alert setValue:titleText forKey:@"attributedTitle"]; // 使用富文本来改变alert的message字体大小和颜色
// NSMakeRange(0, 2) 代表:从0位置开始 两个字符
NSMutableAttributedString *messageText = [[NSMutableAttributedString alloc] initWithString:@"这里是正文信息"];
[messageText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10] range:NSMakeRange(0, 6)];
[messageText addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
[messageText addAttribute:NSForegroundColorAttributeName value:[UIColor brownColor] range:NSMakeRange(3, 3)];
[alert setValue:messageText forKey:@"attributedMessage"]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; // 设置按钮背景图片
UIImage *accessoryImage = [[UIImage imageNamed:@"selectRDImag.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[cancelAction setValue:accessoryImage forKey:@"image"]; // 设置按钮的title颜色
[cancelAction setValue:[UIColor lightGrayColor] forKey:@"titleTextColor"]; // 设置按钮的title的对齐方式
[cancelAction setValue:[NSNumber numberWithInteger:NSTextAlignmentLeft] forKey:@"titleTextAlignment"]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil]; [alert addAction:okAction];
[alert addAction:cancelAction]; [self presentViewController:alert animated:YES completion:nil];

效果图:

效果图

demo下载地址:CustomAlertControllerDemo

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

最新文章

  1. AngularJs之五
  2. 使用免费组件view pdf 文档
  3. SSH基于Hibernate eventListener 事件侦听器的操作日志自动保存到数据库
  4. 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing
  5. GAT2.0使用文档(组合接口测试)
  6. Win7版IE10浏览器正式版官方下载地址
  7. Unity-Animator深入系列---剪辑播放后位置预判(Animator.Target)
  8. 项目分析 NGPcontext
  9. UVa120 - Stacks of Flapjacks
  10. 消除热块(hot block)
  11. Spring 类构造器初始化实例
  12. Python2和Python3的一些语法区别
  13. java 操作hbase1.2
  14. Codeforces 626E Simple Skewness(暴力枚举+二分)
  15. android 开发 View _13 绘制图片与BitmapShader位图的图像渲染器
  16. python中的多进程与多线程(一)
  17. 标准C库函数
  18. 第三周(JAVA编写的 wordcount)
  19. [Windows Azure] Learn SQL Reporting on Windows Azure (9-Step Tutorial)
  20. 递归的几个demo

热门文章

  1. C# 语言规范_版本5.0 (第10章 类)
  2. CUDA编程入门,Dim3变量
  3. c# 索引器方法
  4. yield 学习笔记
  5. react视频入门
  6. openstack私有云布署实践【1 网络拓扑说明】
  7. JS兼容性总结
  8. HDU 5718 Oracle(高精度)
  9. $_SERVER 等超全局数组的用法 $_COOKIE $_GET $_SESSION
  10. Java 反射 分析类和对象