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;
}];
}