最近项目需求让做一个界面提醒,样式和系统一样,但是字体,颜色,大小却和系统的不太一样。这里总结一下我的做法,希望能够帮助大家。
先来说说系统的界面提醒吧。
一、系统UIAlertController
苹果自iOS8开始,就已经废弃了之前用于界面提醒的UIAlertView类以及UIActionSheet,取而代之的是UIAlertController以及UIAlertAction,从实际使用情况来看,苹果把之前不同类型/样式的通知实现方法进行了统一,简化了有关提醒功能的实现。
1.UIAlertController的使用
-
常用方法和属性
+ (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle; //初始化 - (void)addAction:(UIAlertAction *)action;//添加一个按钮选项 - (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion NS_AVAILABLE_IOS(5_0);//显示提醒
提醒的样式把弹出提醒和底部提醒进行了统一,使用preferredStyle来区分
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0,
UIAlertControllerStyleAlert
}
2.UIAlertAction的使用
UIAlertAction是定义提醒中每个按钮的样式以及用户点击后所执行的操作
+ (instancetype)actionWithTitle:(nullable NSString *)title style:(UIAlertActionStyle)style handler:(void (^ __nullable)(UIAlertAction *action))handler;
提醒按钮的样式是通过UIAlertActionStyle参数决定的
typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
UIAlertActionStyleDefault = 0,
UIAlertActionStyleCancel,
UIAlertActionStyleDestructive
} NS_ENUM_AVAILABLE_IOS(8_0);
3.代码示例
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否要重新开始?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
self.timeLabel.text = @"5";
}];
UIAlertAction* Action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
}];
[alert addAction:defaultAction];
[alert addAction:Action1];
[self presentViewController:alert animated:YES completion:nil];
二、自定义UIAlertC0ntroller使用
使用KVC的方式改变UIAlertController的样式
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"内容" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
//Add image 这里可以给button添加图片
UIImage *accessoryImage = [UIImage imageNamed:@"selectRDImag.png"];
accessoryImage = [accessoryImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[cancelAction setValue:accessoryImage forKey:@"image"];
//设置cancelAction的title颜色
[cancelAction setValue:[UIColor lightGrayColor] forKey:@"titleTextColor"];
//设置cancelAction的title的对齐方式
[cancelAction setValue:[NSNumber numberWithInteger:NSTextAlignmentLeft] forKey:@"titleTextAlignment"];
[alertController addAction:cancelAction];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
//设置okAction的title颜色
[okAction setValue:[UIColor greenColor] forKey:@"titleTextColor"];
[alertController addAction:okAction];
//Custom Title,使用富文本来改变title的字体大小
NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Presenting the Hulk Hogan!"];
[hogan addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(13, 11)];
[alertController setValue:hogan forKey:@"attributedTitle"];
NSMutableAttributedString *hogan1 = [[NSMutableAttributedString alloc] initWithString:@"hjasdghjdfsgkfdghfdgsgdsfgdsfg"];
[hogan1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(13, 11)];
[alertController setValue:hogan1 forKey:@"attributedMessage"];
[self presentViewController:alertController animated:YES completion:nil];
最终效果:
原文链接:http://huanghaiyan.96.lt/ios/ios-自定义uialertcontroller的字体、颜色、大小/
demo下载地址:https://github.com/huanghaiyan/CustomAlertView-demo