iOS开发 自定义UIAlertController的样式
来源:互联网 发布:财务报账系统 php 编辑:程序博客网 时间:2024/06/16 11:34
字数595 阅读26 评论0 喜欢0
引言:
关于提示框, 系统自带的提示框有时可能满足不了我们的需求, 比如一个提示框的取消按钮我需要灰色字体显示, 这时候就需要自定义提示框的样式了。
示例图苹果自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];
效果图:
![](http://upload-images.jianshu.io/upload_images/1975627-eee1ddd5740eceb3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
效果图
demo下载地址:CustomAlertControllerDemo
1 0
- iOS开发 自定义UIAlertController的样式
- iOS开发--自定义UIAlertController
- 《iOS开发笔记—自定义UIAlertController》
- 关于iOS UIAlertController自定义的那些事儿
- iOS开发 UIAlertController的创建与使用
- iOS开发之UIAlertController新版的使用方法
- iOS开发:使用UIAlertController的各种姿势
- iOS开发之UIAlertController的使用
- iOS开发-UIAlertController
- iOS 开发之UIAlertController
- IOS开发之UIAlertController
- IOS开发之UIAlertController
- Xamarin.IOS UIAlertController 自定义字体
- UIAlertController 两个按钮的样式
- UIAlertController 两个按钮的样式
- iOS开发-UIActionSheet和UIAlertController
- iOS开发进阶-UIAlertController使用
- iOS开发 提示框UIAlertController的略微封装
- 设计模式:单一职责原则
- 定时器的简单用法
- sql server 备份失败[上加载的介质已格式化为支持 1 个介质簇,但根据指定的备份设备,应支持 2 个介质簇]
- L2-002. 链表去重
- webService三要素 SOAP、WSDL、UDDI
- iOS开发 自定义UIAlertController的样式
- java 产生指定范围的随机数
- Go语言圣经(中文版)读书笔记
- redis缓存数据需要指定缓存有效时间范围段的多个解决方案 Calendar+quartz
- eclipse最有用快捷键整理
- 关于Java Final 修饰的局部变量,多线程可以访问
- HDU 5710 Digit-Sum 数学杂题
- DevExpress GridControl使用
- Oracle处理CLOB超过4000入库问题 (2)