| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- //
- // MBProgressHUD+NHAdd.m
- // NHHUDExtendDemo
- //
- // Created by 肖雨 on 2017/8/18.
- // Copyright © 2017年 肖雨. All rights reserved.
- //
- #import "MBProgressHUD+NHAdd.h"
- #import "MBProgressHUD_NHExtend.h"
- #import "MBProgressHUD.h"
- #import <objc/message.h>
- CGFloat const delayTime = 1.2;
- #define kLoadImage(name) [UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", (name)]]
- @implementation MBProgressHUD (NHAdd)
- static char cancelationKey;
- NS_INLINE MBProgressHUD *createNew(UIView *view) {
- if (view == nil) view = (UIView*)[UIApplication sharedApplication].delegate.window;
- return [MBProgressHUD showHUDAddedTo:view animated:YES];
- }
- NS_INLINE MBProgressHUD *settHUD(UIView *view, NSString *title, BOOL autoHidden) {
- MBProgressHUD *hud = createNew(view);
- //文字
- hud.label.text = title;
- //支持多行
- hud.label.numberOfLines = 0;
- // 隐藏时候从父控件中移除
- hud.removeFromSuperViewOnHide = YES;
-
- //设置默认风格
- if (NHDefaultHudStyle == 1) {
- hud.hudContentStyle(NHHUDContentBlackStyle);
-
- } else if (NHDefaultHudStyle == 2) {
- hud.hudContentStyle(NHHUDContentCustomStyle);
- }
-
- if (autoHidden) {
- // x秒之后消失
- [hud hideAnimated:YES afterDelay:delayTime];
- }
-
- return hud;
- }
- + (MBProgressHUD *)showOnlyLoadToView:(UIView *)view {
- return settHUD(view, nil, NO);
- }
- + (void)showOnlyTextToView:(UIView *)view title:(NSString *)title {
- MBProgressHUD *hud = settHUD(view, title, YES);
- hud.mode = MBProgressHUDModeText;
- }
- + (void)showOnlyTextToView:(UIView *)view title:(NSString *)title detail:(NSString *)detail {
- MBProgressHUD *hud = settHUD(view, title, YES);
- hud.detailsLabel.text = detail;
- hud.mode = MBProgressHUDModeText;
- }
- + (void)showSuccess:(NSString *)success toView:(UIView *)view {
- MBProgressHUD *hud = settHUD(view, success, YES);
- hud.mode = MBProgressHUDModeCustomView;
- hud.customView = [[UIImageView alloc] initWithImage:kLoadImage(@"success.png")];
- }
- + (void)showError:(NSString *)error toView:(UIView *)view {
- MBProgressHUD *hud = settHUD(view, error, YES);
- hud.mode = MBProgressHUDModeCustomView;
- hud.customView = [[UIImageView alloc] initWithImage:kLoadImage(@"error.png")];
- }
- + (void)showTitleToView:(UIView *)view postion:(NHHUDPostion)postion title:(NSString *)title {
- MBProgressHUD *hud = settHUD(view, title, YES);
- hud.mode = MBProgressHUDModeText;
- hud.hudPostion(postion);
- }
- //纯标题 + 详情 + 自定背景风格 - 自动消失
- + (void)showDetailToView:(UIView *)view
- postion:(NHHUDPostion)postion
- title:(NSString *)title
- detail:(NSString *)detail {
- MBProgressHUD *hud = settHUD(view, title, YES);
- hud.detailsLabel.text = detail;
- hud.mode = MBProgressHUDModeText;
- hud.hudPostion(postion);
- }
- + (void)showTitleToView:(UIView *)view
- postion:(NHHUDPostion)postion
- contentStyle:(NHHUDContentStyle)contentStyle
- title:(NSString *)title {
-
- MBProgressHUD *hud = settHUD(view, title, YES);
- hud.mode = MBProgressHUDModeText;
- hud.hudContentStyle(contentStyle);
- hud.hudPostion(postion);
- }
- + (MBProgressHUD *)showLoadToView:(UIView *)view title:(NSString *)title {
- MBProgressHUD *hud = settHUD(view, title, NO);
- hud.mode = MBProgressHUDModeIndeterminate;
- return hud;
- }
- + (MBProgressHUD *)showLoadToView:(UIView *)view contentStyle:(NHHUDContentStyle)contentStyle title:(NSString *)title {
- MBProgressHUD *hud = settHUD(view, title, NO);
- hud.hudContentStyle(contentStyle);
- return hud;
- }
- + (void)showTitleToView:(UIView *)view
- contentStyle:(NHHUDContentStyle)contentStyle
- title:(NSString *)title
- afterDelay:(NSTimeInterval)delay {
- MBProgressHUD *hud = settHUD(view, title, NO);
- hud.mode = MBProgressHUDModeText;
- hud.hudContentStyle(contentStyle);
- [hud hideAnimated:YES afterDelay:delay];
- }
- + (MBProgressHUD *)showDownToView:(UIView *)view
- progressStyle:(NHHUDProgressStyle)progressStyle
- title:(NSString *)title
- progress:(NHCurrentHud)progress {
- MBProgressHUD *hud = settHUD(view, title, NO);
- if (progressStyle == NHHUDProgressDeterminateHorizontalBar) {
- hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
-
- } else if (progressStyle == NHHUDProgressDeterminate) {
- hud.mode = MBProgressHUDModeDeterminate;
-
- } else if (progressStyle == NHHUDProgressAnnularDeterminate) {
- hud.mode = MBProgressHUDModeAnnularDeterminate;
- }
- if (progress) {
- progress(hud);
- }
- return hud;
- }
- + (MBProgressHUD *)showDownToView:(UIView *)view
- progressStyle:(NHHUDProgressStyle)progressStyle
- title:(NSString *)title
- cancelTitle:(NSString *)cancelTitle
- progress:(NHCurrentHud)progress
- cancelation:(NHCancelation)cancelation {
-
- MBProgressHUD *hud = settHUD(view, title, NO);
-
- if (progressStyle == NHHUDProgressDeterminateHorizontalBar) {
- hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
-
- } else if (progressStyle == NHHUDProgressDeterminate) {
- hud.mode = MBProgressHUDModeDeterminate;
-
- } else if (progressStyle == NHHUDProgressAnnularDeterminate) {
- hud.mode = MBProgressHUDModeAnnularDeterminate;
- }
-
- [hud.button setTitle:cancelTitle ?: NSLocalizedString(@"Cancel", @"HUD cancel button title") forState:UIControlStateNormal];
- [hud.button addTarget:hud action:@selector(didClickCancelButton) forControlEvents:UIControlEventTouchUpInside];
- hud.cancelation = cancelation;
- if (progress) {
- progress(hud);
- }
- return hud;
- }
- + (void)showCustomView:(UIImage *)image toView:(UIView *)toView title:(NSString *)title {
- MBProgressHUD *hud = settHUD(toView, title, YES);
- // Set the custom view mode to show any view.
- hud.mode = MBProgressHUDModeCustomView;
- // Set an image view with a checkmark.
- hud.customView = [[UIImageView alloc] initWithImage:image];
- // Looks a bit nicer if we make it square.
- hud.square = YES;
- }
- + (MBProgressHUD *)showModelSwitchToView:(UIView *)toView
- title:(NSString *)title
- configHud:(NHCurrentHud)configHud {
- MBProgressHUD *hud = settHUD(toView, title, NO);
- // Will look best, if we set a minimum size.
- hud.minSize = CGSizeMake(150.f, 100.f);
- if (configHud) {
- configHud(hud);
- }
- return hud;
- }
- + (MBProgressHUD *)showDownNSProgressToView:(UIView *)view title:(NSString *)title {
- MBProgressHUD *hud = settHUD(view, title, NO);
- hud.mode = MBProgressHUDModeDeterminate;
- return hud;
- }
- + (MBProgressHUD *)showDownWithNSProgress:(NSProgress *)Progress
- toView:(UIView *)view title:(NSString *)title
- configHud:(NHCurrentHud)configHud {
- MBProgressHUD *hud = settHUD(view, title, NO);
- if (configHud) {
- configHud(hud);
- }
- return hud;
- }
- + (MBProgressHUD *)showLoadToView:(UIView *)view
- backgroundColor:(UIColor *)backgroundColor
- title:(NSString *)title {
- MBProgressHUD *hud = settHUD(view, title, NO);
- hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
- hud.backgroundView.backgroundColor = backgroundColor;
- return hud;
- }
- + (MBProgressHUD *)showLoadToView:(UIView *)view
- contentColor:(UIColor *)contentColor
- title:(NSString *)title {
- MBProgressHUD *hud = settHUD(view, title, NO);
- hud.contentColor = contentColor;
- return hud;
- }
- + (MBProgressHUD *)showLoadToView:(UIView *)view
- contentColor:(UIColor *)contentColor
- backgroundColor:(UIColor *)backgroundColor
- title:(NSString *)title {
-
- MBProgressHUD *hud = settHUD(view, title, NO);
- hud.contentColor = contentColor;
- hud.backgroundView.color = backgroundColor;
- return hud;
- }
- + (MBProgressHUD *)showLoadToView:(UIView *)view
- titleColor:(UIColor *)titleColor
- bezelViewColor:(UIColor *)bezelViewColor
- backgroundColor:(UIColor *)backgroundColor
- title:(NSString *)title {
-
- MBProgressHUD *hud = settHUD(view, title, NO);
- hud.label.textColor = titleColor;
- hud.bezelView.backgroundColor = bezelViewColor;
- hud.backgroundView.color = backgroundColor;
- return hud;
- }
- + (MBProgressHUD *)createHudToView:(UIView *)view title:(NSString *)title configHud:(NHCurrentHud)configHud {
- MBProgressHUD *hud = settHUD(view, title, YES);
- if (configHud) {
- configHud(hud);
- }
- return hud;
- }
- + (void)hideHUDForView:(UIView *)view {
- if (view == nil) view = (UIView*)[UIApplication sharedApplication].delegate.window;
- [self hideHUDForView:view animated:YES];
- }
- + (void)hideHUD {
- [self hideHUDForView:nil];
- }
- #pragma mark -- sett // gett
- - (void)didClickCancelButton {
- if (self.cancelation) {
- self.cancelation(self);
- }
- }
- - (void)setCancelation:(NHCancelation)cancelation {
- objc_setAssociatedObject(self, &cancelationKey, cancelation, OBJC_ASSOCIATION_COPY);
- }
- - (NHCancelation)cancelation {
- return objc_getAssociatedObject(self, &cancelationKey);
- }
- - (MBProgressHUD *(^)(UIColor *))hudBackgroundColor {
- return ^(UIColor *hudBackgroundColor) {
- self.backgroundView.color = hudBackgroundColor;
- return self;
- };
- }
- - (MBProgressHUD *(^)(UIView *))toView {
- return ^(UIView *view){
- return self;
- };
- }
- - (MBProgressHUD *(^)(NSString *))title {
- return ^(NSString *title){
- self.label.text = title;
- return self;
- };
- }
- - (MBProgressHUD *(^)(NSString *))details {
- return ^(NSString *details){
- self.detailsLabel.text = details;
- return self;
- };
- }
- - (MBProgressHUD *(^)(NSString *))customIcon {
- return ^(NSString *customIcon) {
- self.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:customIcon]];
- return self;
- };
- }
- - (MBProgressHUD *(^)(UIColor *))titleColor {
- return ^(UIColor *titleColor){
- self.label.textColor = titleColor;
- self.detailsLabel.textColor = titleColor;
- return self;
- };
- }
- - (MBProgressHUD *(^)(UIColor *))progressColor {
- return ^(UIColor *progressColor) {
- UIColor *titleColor = self.label.textColor;
- self.contentColor = progressColor;
- self.label.textColor = titleColor;
- self.detailsLabel.textColor = titleColor;
- return self;
- };
- }
- - (MBProgressHUD *(^)(UIColor *))allContentColors {
- return ^(UIColor *allContentColors) {
- self.contentColor = allContentColors;
- return self;
- };
- }
- - (MBProgressHUD *(^)(UIColor *))bezelBackgroundColor {
- return ^(UIColor *bezelViewColor){
- self.bezelView.backgroundColor = bezelViewColor;
- return self;
- };
- }
- - (MBProgressHUD *(^)(NHHUDContentStyle))hudContentStyle {
- return ^(NHHUDContentStyle hudContentStyle){
- if (hudContentStyle == NHHUDContentBlackStyle) {
- self.contentColor = [UIColor whiteColor];
- self.bezelView.backgroundColor = [UIColor blackColor];
- self.bezelView.style = MBProgressHUDBackgroundStyleBlur;
-
- } else if (hudContentStyle == NHHUDContentCustomStyle) {
- self.contentColor = NHCustomHudStyleContentColor;
- self.bezelView.backgroundColor = NHCustomHudStyleBackgrandColor;
- self.bezelView.style = MBProgressHUDBackgroundStyleBlur;
-
- } else if (hudContentStyle == NHHUDContentDefaultStyle){
- self.contentColor = [UIColor blackColor];
- self.bezelView.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
- self.bezelView.style = MBProgressHUDBackgroundStyleBlur;
-
- }
- return self;
- };
- }
- - (MBProgressHUD *(^)(NHHUDPostion))hudPostion {
- return ^(NHHUDPostion hudPostion){
- if (hudPostion == NHHUDPostionTop) {
- self.offset = CGPointMake(0.f, -MBProgressMaxOffset);
- } else if (hudPostion == NHHUDPostionCenten) {
- self.offset = CGPointMake(0.f, 0.f);
- } else {
- self.offset = CGPointMake(0.f, MBProgressMaxOffset);
- }
- return self;
- };
- }
- - (MBProgressHUD *(^)(NHHUDProgressStyle))hudProgressStyle {
- return ^(NHHUDProgressStyle hudProgressStyle){
- if (hudProgressStyle == NHHUDProgressDeterminate) {
- self.mode = MBProgressHUDModeDeterminate;
-
- } else if (hudProgressStyle == NHHUDProgressAnnularDeterminate) {
- self.mode = MBProgressHUDModeAnnularDeterminate;
- } else if (hudProgressStyle == NHHUDProgressCancelationDeterminate) {
- self.mode = MBProgressHUDModeDeterminate;
- } else if (hudProgressStyle == NHHUDProgressDeterminateHorizontalBar) {
- self.mode = MBProgressHUDModeDeterminateHorizontalBar;
- }
- return self;
- };
- }
- @end
|