美文网首页
定制AlertController提示

定制AlertController提示

作者: 胖红Red | 来源:发表于2018-04-15 17:57 被阅读19次
    /**
    *  定制弹窗alertVC
    *  @param title   标题
    *  @param message 信息
    *  @param  cancel 取消标题(如果需要取消按钮 该值不能为空;不需要取消按钮该值设为@"")
    *  @param completion 点击事件
    *  用法士例
    
    [self showAlertWithTitle:@"hahfh" message:@"哈哈哈" cancelTitle:@"取消" completion:^(BOOL cancelled) {
    
     if (cancelled == NO) {
    NSLog(@"确定按钮的点击事件");
    }else {
    NSLog(@"取消按钮的点击的时间");
    
    }
    
    }];
    
    */
    
    
    - (void)showAlertWithTitle:(NSString *)title
                   message:(NSString *)message cancelTitle:(NSString *)cancel
                     completion:(void(^) (BOOL cancelled))completion;
    

    具体实现:

       if (completion) {
        _completion = completion;
    }
    
    //设置 message title 的属性
     UIAlertController *alerVC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    NSMutableAttributedString *Message = [[NSMutableAttributedString alloc] initWithString:message];
    [Message addAttribute:NSForegroundColorAttributeName value:Color_Alert_Message range:NSMakeRange(0, [Message length])];
    [Message addAttribute:NSFontAttributeName value:FONT(14) range:NSMakeRange(0, [Message length])];
    
    [ alerVC setValue:Message forKey:@"attributedMessage"];
    
    NSMutableAttributedString *Title = [[NSMutableAttributedString alloc] initWithString:title];
    [Title addAttribute:NSForegroundColorAttributeName value:Color_Alert_Message range:NSMakeRange(0, [Title length])];
    [Title addAttribute:NSFontAttributeName value:FONT(14) range:NSMakeRange(0, [Title length])];
    
    [ alerVC setValue:Title forKey:@"attributedTitle"];
    
    // 2.创建并添加按钮
    __weak typeof(self) wSelf = self;
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"OK Action");
    
    }];
    [okAction setValue:Color_Alert_Blue forKey:@"_titleTextColor"];
    
    
    alerVC.view.backgroundColor = Color_White;
    alerVC.view.layer.cornerRadius = 4;
    alerVC.view.clipsToBounds = YES;
    UILabel *appearanceLabel = [UILabel appearanceWhenContainedIn:UIAlertController.class, nil];
    UIFont *font = [UIFont systemFontOfSize:14];
    appearanceLabel.font = font;
    [self presentViewController:alerVC animated:YES completion:nil];
    
    
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancel style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"Cancel Action");
    
    }];
    [cancelAction setValue:Color_Alert_Gray forKey:@"_titleTextColor"];
    
    
    [alerVC addAction:okAction];
    
    if (cancel.length > 0 ) {
        [alerVC  addAction:cancelAction];
        
    }
    

    提示:
    如果提示是分行显示的比如2行:
    可以分别设置title 和message

    相关文章

      网友评论

          本文标题:定制AlertController提示

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