美文网首页
iOS 自定义UIAlertController的字体、颜色、大

iOS 自定义UIAlertController的字体、颜色、大

作者: Simon_Liang | 来源:发表于2018-12-13 17:31 被阅读0次

    先来说说系统的界面提醒吧。

    一、系统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 = [UIAlertControlleralertControllerWithTitle:@"提示"message:@"是否要重新开始?"preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* defaultAction = [UIAlertActionactionWithTitle:@"是"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction* action) {self.timeLabel.text =@"5";}];

    UIAlertAction* Action1 = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction*action) {

    }];

    [alert addAction:defaultAction];

    [alert addAction:Action1];

    [selfpresentViewController:alert animated:YEScompletion: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];

    最终效果图

    相关文章

      网友评论

          本文标题:iOS 自定义UIAlertController的字体、颜色、大

          本文链接:https://www.haomeiwen.com/subject/sapbhqtx.html