IOS 利用 MBProgressHUD 实现仿Android Toast 提示弹窗,带状态的Toast提示框

1:MBProgressHUD 是简单易用的第三方库,能够方便的调用各种提示框,加载框。
使用cocoapods安装,在Podfile文件中添加,(默认大家都是会使用cocoapods的哈),然后运行pod install来安装MBProgressHUD。

 pod 'MBProgressHUD'

2:创建MyProgressHUD文件

MyProgressHUD.h

#import <MyProgressHUD.h>

#define LENGTH_SHORT (2)
#define LENGTH_LONG  (3.5)

#define TIP_STATUS_NORMAL (0)
#define TIP_STATUS_SUCCESS (1)
#define TIP_STATUS_INFO (2)
#define TIP_STATUS_WARNING (3)
#define TIP_STATUS_ERROR (4)

CG_EXTERN  MBProgressHUD * _showStatusView(NSString* text, float duration_time, UIView* view, int *tip_status);

#define ShowNormalTip(text) ShowNormalTipView(text, LENGTH_SHORT, self.view)
#define ShowNormalTipView(text, duration_time, view) _showStatusView(text, duration_time, view, TIP_STATUS_NORMAL)

#define ShowSuccessTip(text) ShowSuccessTipView(text, LENGTH_SHORT, self.view)
#define ShowSuccessTipView(text, duration_time, view) _showStatusView(text, duration_time, view, TIP_STATUS_SUCCESS)

#define ShowInfoTip(text) ShowInfoTipView(text, LENGTH_SHORT, self.view)
#define ShowInfoTipView(text, duration_time, view) _showStatusView(text, duration_time, view, TIP_STATUS_INFO)

#define ShowWarningTip(text) ShowWarningTipiew(text, LENGTH_SHORT, self.view)
#define ShowWarningTipiew(text, duration_time, view) _showStatusView(text, duration_time, view, TIP_STATUS_WARNING)

#define ShowErrorTip(text) ShowErrorTipiew(text, LENGTH_SHORT, self.view)
#define ShowErrorTipiew(text, duration_time, view) _showStatusView(text, duration_time, view, TIP_STATUS_ERROR)

MyProgressHUD.m

#import "CSProgressHUD.h"


MBProgressHUD* _showStatusView(NSString* text, float duration_time, UIView* view, int *tip_status) {
    [MBProgressHUD hideHUDForView:view animated:NO];
    MBProgressHUD *hud = [[MBProgressHUD alloc]initWithView:view];
    [view addSubview:hud];
    hud.offset = CGPointMake(0.f, kScreenHeight/2-(kScreenHeight >= 812.f?114:90));//距离中心点位移
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;//修改样式,否则等待框背景色将为半透明
    hud.mode = MBProgressHUDModeText;
    hud.label.font = [UIFont systemFontOfSize:15];
    hud.label.textColor = [UIColor whiteColor];
    hud.label.numberOfLines = 0;
    if(tip_status==0){//普通toast弹窗
        //设置等待框背景色为黑色
        hud.bezelView.backgroundColor = [UIColor colorWithHexString:@"777777" andAlpha:0.9f];
        hud.label.text = text;
        
    }else if(tip_status==1){//成功toast弹窗
        //设置等待框背景色为绿色
        hud.bezelView.backgroundColor = [UIColor colorWithHexString:@"388E3C" andAlpha:0.9f];
        NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"  %@",text]];
        NSTextAttachment *attch = [[NSTextAttachment alloc] init];
        attch.image = [UIImage imageNamed:@"icon_success"];
        // 设置图片大小
        attch.bounds = CGRectMake(0, [UIFont systemFontOfSize:38].descender, 30, 30);
        
        // 创建带有图片的富文本
        NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
       
        
        [attri insertAttributedString:string atIndex:0];
        hud.label.attributedText = attri;
    }else if(tip_status==2){//提示toast弹窗
        //设置等待框背景色为黑色
        hud.bezelView.backgroundColor = [UIColor colorWithHexString:@"3F51B5" andAlpha:0.9f];
        NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"  %@",text]];
        NSTextAttachment *attch = [[NSTextAttachment alloc] init];
        attch.image = [UIImage imageNamed:@"icon_info"];
        // 设置图片大小
        attch.bounds = CGRectMake(0, [UIFont systemFontOfSize:38].descender, 30, 30);
        
        
        // 创建带有图片的富文本
        NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
       
        
        [attri insertAttributedString:string atIndex:0];
        hud.label.attributedText = attri;
    }else if(tip_status==3){//警告toast弹窗
        //设置等待框背景色为黑色
        hud.bezelView.backgroundColor = [UIColor colorWithHexString:@"FFA900" andAlpha:0.9f];
        NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"  %@",text]];
        NSTextAttachment *attch = [[NSTextAttachment alloc] init];
        attch.image = [UIImage imageNamed:@"icon_info"];
        // 设置图片大小
        attch.bounds = CGRectMake(0, [UIFont systemFontOfSize:38].descender, 30, 30);
        
        
        // 创建带有图片的富文本
        NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
        [attri insertAttributedString:string atIndex:0];
        hud.label.attributedText = attri;
    }else{//错误toast弹窗
        //设置等待框背景色为黑色
        hud.bezelView.backgroundColor = [UIColor colorWithHexString:@"FD4C5B" andAlpha:0.9f];
        NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"  %@",text]];
        NSTextAttachment *attch = [[NSTextAttachment alloc] init];
        attch.image = [UIImage imageNamed:@"icon_error"];
        // 设置图片大小
        attch.bounds = CGRectMake(0, [UIFont systemFontOfSize:38].descender, 30, 30);
        
        
        // 创建带有图片的富文本
        NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
       
        
        [attri insertAttributedString:string atIndex:0];
        hud.label.attributedText = attri;
    }
    hud.bezelView.layer.masksToBounds = YES;
    hud.bezelView.layer.cornerRadius = 15.0;
    hud.bezelView.alpha = 1.f;
    hud.removeFromSuperViewOnHide = YES;
    hud.margin = 10;
    [hud showAnimated:YES];
    [hud hideAnimated:YES afterDelay:duration_time];
    
    return hud;
}

其中一些自写的方法
colorWithHexString

+ (UIColor *)colorWithHexString:(NSString *)stringToConvert andAlpha:(CGFloat)alpha;
{
    // 转换成标准16进制数
    NSRange range = [stringToConvert rangeOfString:@"#"];
    if (range.location != NSNotFound) {
        stringToConvert = [stringToConvert stringByReplacingCharactersInRange:range withString:@"0x"];
    }
    if ([stringToConvert rangeOfString:@"0x"].location == NSNotFound) {
        stringToConvert = [@"0x" stringByAppendingString:stringToConvert];
    }
    // 十六进制字符串转成整形。
    long colorLong = strtoul([stringToConvert cStringUsingEncoding:NSUTF8StringEncoding], 0, 16);
    // 通过位与方法获取三色值
    int R = (colorLong & 0xFF0000 )>>16;
    int G = (colorLong & 0x00FF00 )>>8;
    int B =  colorLong & 0x0000FF;
    
    //string转color
    UIColor *wordColor = [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:alpha];
    return wordColor;
}

3:在项目的PrefixHeader.pch文件中添加,方便全局调用

#define kScreenWidth  [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#ifdef __OBJC__
#import "MyProgressHUD.h"
#endif

4调用

ShowNormalTip(@"这是普通Toast提示框");
ShowSuccessTip(@"这是成功Toast提示框");
ShowInfoTip(@"这是提示Toast提示框");
ShowWarningTip(@"这是警告Toast提示框");
ShowErrorTip(@"这是错误Toast提示框");

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值