UIAlertController和UIAlertView

2018年4月17日
1.修改系统提示框对齐方式

系统自带原来效果:
image.png

修改后效果:
image.png

实现:

1.原理:通过扩展获取内容UILabel,修改其对应的对齐方式和换行方式

2.实现:(参考:https://stackoverflow.com/questions/25962559/uialertcontroller-text-alignment)

#import "UIAlertController+HuAlert.h"
@implementation UIAlertController (HuAlert)
@dynamic hu_titleLabel;
@dynamic hu_messageLabel;
-(UILabel *)hu_titleLabel{
    return [self hu_viewArray:self.view][0];
}
-(UILabel *)hu_messageLabel{
    return [self hu_viewArray:self.view][1];
}
- (NSArray *)hu_viewArray:(UIView *)rootView{
    static NSArray *_subviews = nil;
    _subviews = nil;
    for (UIView *v in rootView.subviews) {
        if (_subviews) {
            break;
        }
        if ([v isKindOfClass:[UILabel class]]) {
            _subviews = rootView.subviews;
            return _subviews;
        }
        [self hu_viewArray:v];
    }
    return _subviews;
}
@end

3.使用:

+ (void)showAlertControllerAt:(UIViewController *)vc withMessage:(NSString *)message withTextAlignment:(NSTextAlignment)textAlignment{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
    }];
    alertController.hu_messageLabel.textAlignment = textAlignment;
    alertController.hu_messageLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [alertController addAction:okAction];
    [vc presentViewController:alertController animated:YES completion:nil];
}

2018年1月17日

1.有输入框的提示框
image.png
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"输入学员卡号" message:nil preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                }];
                WS(weakSelf)
                UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                    //
                    [alertController dismissViewControllerAnimated:YES completion:nil];
                    UITextField *textField = alertController.textFields[0];
                    weakSelf.perInfoModel.cadetCardNumber = textField.text;
                    [weakSelf tableViewFerfreshCell:indexPath.row section:indexPath.section];
                    [weakSelf updateServerCadetCardNumber:weakSelf.perInfoModel.cadetCardNumber];
                }];
                
                [alertController addAction:cancelAction];
                [alertController addAction:okAction];
                [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
                    textField.placeholder = @"国家继教网卡号";
                }];
                [self presentViewController:alertController animated:YES completion:nil];

2017年11月22日
1.UIActionSheet


image.png
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从照片库选择", @"拍照", nil];
        sheet.delegate = self;
        [sheet showInView:self.tableView];
        
        [HuAlilog alilogWithBrowseActionWithPageId:@"30101"];

2017年7月27日
1.修改默认弹款布局

/**
 * @brief   格式化弹出框消息内容,默认左对齐方式
 * @param  -offset  内容左边距
 */
+ (void)formatMessageContent:(UIAlertView *)alertView withOffset: (int)offset
{
    BOOL bFirst = YES;
    for (int i = 0; i < [alertView.subviews count]; i++) {
        UIView *subs = [alertView.subviews objectAtIndex:i];
        if ([subs isKindOfClass:[UILabel class]]) {
            UILabel *tempLabel = (UILabel *)subs;
            if (bFirst) {
                tempLabel.font = [UIFont systemFontOfSize:20];
                bFirst = NO;
            } else{
                float x = offset, y = tempLabel.frame.origin.y;
                float width = tempLabel.frame.size.width - x;
                float height = tempLabel.frame.size.height;
                tempLabel.frame = CGRectMake(x, y, width, height);
                tempLabel.textAlignment = UITextAlignmentLeft;
            }
        }
    }
}

2017年4月25日
一. UIAlertController使用例子

- (void)quitButtonClick{
   
    NSString *message = @"是否退出登录";
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert];
   
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    }];
   
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
       //
        [HuAlilog alilogWithBrowseActionWithPageId:@"30804"];
       
    }];
   
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

二.UIAlertView使用例子

UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请先绑定您的支付宝账户,资金将提现到您的支付宝账户" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"前往绑定", nil];
            [alertView show];

UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil message:@"小护只接受原创课程提交哦,么么哒~" delegate:nil  cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
        [alert show];

如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。

你可能感兴趣的:(UIAlertController和UIAlertView)