前言
在以前的开发中,大多数用的第三方库MBProgressHUD做的App提示信息,本这个库的二次封装使用一大把,但有一次遇到UI设计要在文字的前面如成功加上一个成功的勾图片,成败加上一个失败的X图片。如下这样。

实现
当时心里懵圈了,心想,难道我要自定义一个view,根据文字的多少来算高度,来自适应。最后在MBProgressHUD的基础上来改几行代码就达到设计需求。实现创建一个带图片的富文本就可以了。如下:
//显示文字前面带小图片提示
+ (void)showMessage:(NSString *)message icon:(NSString *)icon toView:(UIView *)view
{
view = [self getWillAddView:view];
[self hideHUDForView:view];
dispatch_async(dispatch_get_main_queue(), ^{
// 快速显示一个提示信息
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
// hud.detailsLabel.font = [UIFont systemFontOfSize:14];
// hud.detailsLabel.text = message;
hud.animationType = MBProgressHUDAnimationZoom;
hud.contentColor = [UIColor whiteColor];
hud.mode = MBProgressHUDModeText;
hud.offset = CGPointMake(0, -50); //偏移
// 隐藏时候从父控件中移除
hud.removeFromSuperViewOnHide = YES;
hud.userInteractionEnabled = NO;
NSString * textTemp = [NSString stringWithFormat:@" %@",message];
//2.初始化富文本对象
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textTemp];
//2.1修改富文本中的不同文字的样式
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, textTemp.length)];//字体颜色
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, textTemp.length)];//字体大小
NSTextAttachment * attach = [[NSTextAttachment alloc] init];
attach.image = [UIImage imageNamed:icon];
attach.bounds = CGRectMake(0, -4, 19, 19);
//4.创建带有图片的富文本
NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:(NSTextAttachment *)(attach)];
[attributedString insertAttributedString:string atIndex:0];
hud.detailsLabel.attributedText = attributedString;
// hud.margin = 10; //hub与label之间的间距
hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
hud.bezelView.backgroundColor = kHudBackgroundColor;
[hud hideAnimated:YES afterDelay:kHiddenDelayTime];
});
}
总结
想到以后还要使用MBProgressHud作提示,于是用工厂模式封装了一个类文件,后期可用,不实现Hud的所有显示样式,因为网上很多。只写一个自己使用的。勿喷。用定义宏的方式使使用更方便
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#define CM_SHOWMESSAGE(msg) [CMTipsView showTips:msg];
#define CM_REMOVESHOW [CMTipsView hideHUD];
NS_ASSUME_NONNULL_BEGIN
@interface CMTipsView : NSObject
///////////////////
//常规型 in window
///////////////////
//显示提示的message 只显示文字
+(void)showTips:(NSString *)message;
//显示一个转转 + 文字
+(void)showProgressHud:(NSString *)title;
//隐藏所有的MBProgressHud
+(void)hideHUD;
////////////////////////
//常规型 在指定的view上
////////////////////////
//显示提示的message 只显示文字
+(void)showTips:(NSString *)message addToView:(UIView *)view;
//显示一个转转 + 文字
+(void)showProgressHud:(NSString *)title addToView:(UIView *)view;
// 隐藏所有的MBProgressHud
+(void)hideHUDForView:(UIView *)view;
//文字前面带了一个小图片的那种。
+(void)showSuccess:(NSString *)msg icon:(NSString *)icon;
+(void)showFail:(NSString *)msg icon:(NSString *)icon;
+(void)showWarning:(NSString *)msg icon:(NSString *)icon;
@end
NS_ASSUME_NONNULL_END
只展示重要方法
#pragma mark - 私有方法
/**
功能:这里对Add view进行判断;
如果是nil,就返回UIWindow
@param addView view description
@return return value description
*/
+(UIView *)getWillAddView:(UIView *)addView
{
if(addView) return addView;
addView = [[UIApplication sharedApplication].delegate window];
if (!addView) {
addView = [UIApplication sharedApplication].windows.lastObject;
}
return addView;
}
//没有转转的 只显示文字
+ (void)showMessage:(NSString *)message toView:(UIView *)view
{
view = [self getWillAddView:view];
[self hideHUDForView:view];
dispatch_async(dispatch_get_main_queue(), ^{
// 快速显示一个提示信息
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
// hud.detailsLabel.font = [UIFont systemFontOfSize:14];
hud.detailsLabel.text = message;
hud.animationType = MBProgressHUDAnimationZoom;
hud.contentColor = [UIColor whiteColor];
hud.mode = MBProgressHUDModeText;
hud.offset = CGPointMake(0, -50); //偏移
// 隐藏时候从父控件中移除
hud.removeFromSuperViewOnHide = YES;
hud.userInteractionEnabled = NO;
// hud.margin = 10; //hub与label之间的间距
hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
hud.bezelView.backgroundColor = kHudBackgroundColor;
[hud hideAnimated:YES afterDelay:kHiddenDelayTime];
});
}
//显示 转转 与 文字
+ (void)showProgressHudMessage:(NSString *)message toView:(UIView *)view
{
view = [self getWillAddView:view];
[self hideHUDForView:view];
dispatch_async(dispatch_get_main_queue(), ^{
// 快速显示一个提示信息
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
// hud.label.font = [UIFont systemFontOfSize:14];
hud.label.text = message;
hud.label.textColor = [UIColor whiteColor];
hud.contentColor = [UIColor whiteColor];
hud.mode = MBProgressHUDModeIndeterminate;
hud.animationType = MBProgressHUDAnimationZoom;
hud.offset = CGPointMake(0, -50);
// 隐藏时候从父控件中移除
hud.removeFromSuperViewOnHide = YES;
hud.userInteractionEnabled = NO;
// hud.margin = 10;
hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
hud.bezelView.backgroundColor = kHudBackgroundColor;
// [hud hideAnimated:YES afterDelay:kHiddenDelayTime];
});
}
//显示文字前面带小图片提示
+ (void)showMessage:(NSString *)message icon:(NSString *)icon toView:(UIView *)view
{
view = [self getWillAddView:view];
[self hideHUDForView:view];
dispatch_async(dispatch_get_main_queue(), ^{
// 快速显示一个提示信息
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
// hud.detailsLabel.font = [UIFont systemFontOfSize:14];
// hud.detailsLabel.text = message;
hud.animationType = MBProgressHUDAnimationZoom;
hud.contentColor = [UIColor whiteColor];
hud.mode = MBProgressHUDModeText;
hud.offset = CGPointMake(0, -50); //偏移
// 隐藏时候从父控件中移除
hud.removeFromSuperViewOnHide = YES;
hud.userInteractionEnabled = NO;
NSString * textTemp = [NSString stringWithFormat:@" %@",message];
//2.初始化富文本对象
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textTemp];
//2.1修改富文本中的不同文字的样式
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, textTemp.length)];//字体颜色
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, textTemp.length)];//字体大小
NSTextAttachment * attach = [[NSTextAttachment alloc] init];
attach.image = [UIImage imageNamed:icon];
attach.bounds = CGRectMake(0, -4, 19, 19);
//4.创建带有图片的富文本
NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:(NSTextAttachment *)(attach)];
[attributedString insertAttributedString:string atIndex:0];
hud.detailsLabel.attributedText = attributedString;
// hud.margin = 10; //hub与label之间的间距
hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
hud.bezelView.backgroundColor = kHudBackgroundColor;
[hud hideAnimated:YES afterDelay:kHiddenDelayTime];
});
}
网友评论