(转)【iOS】类Android Toast风格提示

 转自:https://my.oschina.net/liyang2l/blog/204767

MBProgressHUD是一个开源项目,实现了很多种样式的提示框

https://github.com/jdg/MBProgressHUD,下载下来后直接把MBProgressHUD.h和MBProgressHUD.m加入即可。

运行效果如下 和android toast 类似

-(void)showAllTextDialog:(NSString *)str
{
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    HUD.labelText = str;
    HUD.mode = MBProgressHUDModeText;
    
    //指定距离中心点的X轴和Y轴的位置,不指定则在屏幕中间显示
    //    HUD.yOffset = 100.0f;
    //    HUD.xOffset = 100.0f;
    
    [HUD showAnimated:YES whileExecutingBlock:^{
        sleep(1);
    } completionBlock:^{
        [HUD removeFromSuperview];
//        [HUD release];
        HUD = nil;
    }];
    
}

附上我自定义的方法:

+(void)showToast:(UIView *)view content:(NSString *)text{
    
    __block MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
    [view addSubview:hud];
    hud.mode = MBProgressHUDModeText;
    
    hud.label.text = text;
    hud.label.textColor = [UIColor whiteColor];
    //换行
    hud.label.lineBreakMode = UILineBreakModeWordWrap;
    hud.label.numberOfLines = 0;

    [hud setColor:[UIColor blackColor]];
    
    CGRect screen = [UIScreen mainScreen].bounds;
    NSLog(@"width:%f,height:%f",screen.size.width,screen.size.height);
    
    hud.yOffset = screen.size.height - 100.0f;
    
    //指定距离中心点的X轴和Y轴的位置,不指定则在屏幕中间显示
//    hud.yOffset = 300.0f;
    //    hud.xOffset = 100.0f;
    
    [hud showAnimated:YES whileExecutingBlock:^{
        sleep(1.5);
    } completionBlock:^{
        [hud removeFromSuperview];
        //        [HUD release];
        hud = nil;
    }];
}

猜你喜欢

转载自jameskaron.iteye.com/blog/2360676