美文网首页IOS积累
MBProgressHud 换行显示

MBProgressHud 换行显示

作者: XY_Coder | 来源:发表于2018-06-20 09:02 被阅读0次

    一般来说,app里面的单纯的文本提示信息不会太长,一行肯定可以显示完全,所以用MBProgressHud在加上一个类扩展MBProgressHUD+Extension基本上都可以满足需求


    MBProgressHUD+Extension.h .png

    这样子的类扩展可能很多人都见过,但是在最近的项目里面出现了由于要显示的提示信息过长,被省略,信息显示不全的情况,故添加了几个方法。专门用来显示文本信息,可以做到自适应文本大小。


    MBProgressHUD+Extension.h .png
    不多说,代码如下:
    .h
    
    +(void)showXYMessage:(NSString *)text;
    +(void)showXYMessage:(NSString *)text delay:(NSTimeInterval )time;
    +(void)showXYMessage:(NSString *)text toView:(UIView *)view;
    +(void)showXYMessage:(NSString *)text toView:(UIView *)view delay:(NSTimeInterval )time;
    @end
    

    .m

    
    +(void)showXYMessage:(NSString *)text{
        [self showXYMessage:text toView:nil delay:1.5f];
    }
    
    +(void)showXYMessage:(NSString *)text delay:(NSTimeInterval )time{
        [self showXYMessage:text toView:nil delay:time];
    }
    
    +(void)showXYMessage:(NSString *)text toView:(UIView *)view delay:(NSTimeInterval )time{
        if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
        
        
        CGFloat width = view?view.bounds.size.width - 20:[UIScreen mainScreen].bounds.size.width- 2*30;
        CGFloat height = view?view.bounds.size.height - 20:[UIScreen mainScreen].bounds.size.width- 2*30;
        // 快速显示一个提示信息
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
        
        CGSize size = [text boundingRectWithSize:CGSizeMake(width, height) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:11]} context:nil].size;//修改字体大小(下面同步修改)
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, size.width<width?size.width:width, size.height)];
        label.text = text;
        label.font = [UIFont systemFontOfSize:11];//修改字体大小(上面同步修改)
        label.textColor = [UIColor whiteColor];//修改字体颜色
        label.numberOfLines = 0;
        label.textAlignment = NSTextAlignmentCenter;
        hud.customView = label;
        hud.mode = MBProgressHUDModeCustomView;
        [hud setUserInteractionEnabled:false];
        // 隐藏时候从父控件中移除
        hud.removeFromSuperViewOnHide = YES;
        //    hud.square = true;
        hud.margin = 10.0f;
        // time秒之后再消失
        [hud hideAnimated:YES afterDelay:time];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self hideHUDForView:view];
        });
    }
    
    
    +(void)showXYMessage:(NSString *)text toView:(UIView *)view{
        [self showXYMessage:text toView:view delay:1.5f];
    }
    
    @end
    

    相关文章

      网友评论

        本文标题:MBProgressHud 换行显示

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