美文网首页搜集知识小知识iOS学习笔记
iOS 自定义UIAlertController的字体、颜色、大

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

作者: huanghy | 来源:发表于2016-07-04 14:21 被阅读5734次

    最近项目需求让做一个界面提醒,样式和系统一样,但是字体,颜色,大小却和系统的不太一样。这里总结一下我的做法,希望能够帮助大家。

    屏幕快照 2016-07-04 下午2.16.16.png

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

    一、系统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];
    

    最终效果:

    屏幕快照 2016-07-04 下午2.16.26.png
    原文链接:http://huanghaiyan.96.lt/ios/ios-自定义uialertcontroller的字体、颜色、大小/
    demo下载地址:https://github.com/huanghaiyan/CustomAlertView-demo

    相关文章

      网友评论

      • 酸三角:那我怎么改变它的大小呢
        huanghy:[hogan addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(13, 11)];
        都是使用富文本实现的
      • 338f3daaa85c:这个KVC,有文档记录吗,这个是否是私有API,能通过苹果的审核吗
        墨狂之逸才:@338f3daaa85c https://yohunl.com/tan-tan-gai-bian-uialertview-he-uiactionsheet-de-yan-se/这里面讲到了这个私有API,是直接通过这个方法- (void)_setTitleTextColor:(id)arg1;来设置会涉及到这个问题,具体我也不是很懂。
        传闻中的饼干君:@338f3daaa85c 可以通过审核的...

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

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