From 208a431bbf530ca95a0d76e5dd13625a522d2448 Mon Sep 17 00:00:00 2001 From: Andrew Tillman Date: Tue, 4 Nov 2014 14:33:04 -0600 Subject: [PATCH] Added ability to use NSAttributedString --- Demo/Classes/ViewController.m | 13 +++- SVProgressHUD/SVProgressHUD.h | 11 ++++ SVProgressHUD/SVProgressHUD.m | 114 +++++++++++++++++++++++----------- 3 files changed, 101 insertions(+), 37 deletions(-) diff --git a/Demo/Classes/ViewController.m b/Demo/Classes/ViewController.m index f3f60084..0a705c19 100644 --- a/Demo/Classes/ViewController.m +++ b/Demo/Classes/ViewController.m @@ -56,7 +56,14 @@ - (void)show { } - (void)showWithStatus { - [SVProgressHUD showWithStatus:@"Doing Stuff"]; + UIFont *font = [UIFont systemFontOfSize:[UIFont systemFontSize]]; + UIFont *bold = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]]; + + NSMutableAttributedString *message = [[NSMutableAttributedString alloc] initWithString:@"Doing Stuff"]; + NSRange stuffRange = [message.string rangeOfString:@"Stuff"]; + [message addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, stuffRange.location)]; + [message addAttribute:NSFontAttributeName value:bold range:stuffRange]; + [SVProgressHUD showWithAttributedStatus:message]; } static float progress = 0.0f; @@ -90,7 +97,9 @@ - (void)dismissSuccess { } - (void)dismissError { - [SVProgressHUD showErrorWithStatus:@"Failed with Error"]; + NSMutableAttributedString *errorMessage = [[NSMutableAttributedString alloc] initWithString:@"Failed with Error"]; + [errorMessage addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[errorMessage.string rangeOfString:@"Error"]]; + [SVProgressHUD showErrorWithAttributedStatus:errorMessage]; } @end diff --git a/SVProgressHUD/SVProgressHUD.h b/SVProgressHUD/SVProgressHUD.h index f4e8b930..3b1b4a20 100644 --- a/SVProgressHUD/SVProgressHUD.h +++ b/SVProgressHUD/SVProgressHUD.h @@ -41,18 +41,29 @@ typedef NS_ENUM(NSUInteger, SVProgressHUDMaskType) { + (void)show; + (void)showWithMaskType:(SVProgressHUDMaskType)maskType; + (void)showWithStatus:(NSString*)status; ++ (void)showWithAttributedStatus:(NSAttributedString*)attributedStatus; + (void)showWithStatus:(NSString*)status maskType:(SVProgressHUDMaskType)maskType; ++ (void)showWithAttributedStatus:(NSAttributedString*)attributedStatus maskType:(SVProgressHUDMaskType)maskType; + + (void)showProgress:(float)progress; + (void)showProgress:(float)progress status:(NSString*)status; ++ (void)showProgress:(float)progress attributedStatus:(NSAttributedString*)attributedStatus; + (void)showProgress:(float)progress status:(NSString*)status maskType:(SVProgressHUDMaskType)maskType; ++ (void)showProgress:(float)progress attributedStatus:(NSAttributedString*)attributedStatus maskType:(SVProgressHUDMaskType)maskType; + (void)setStatus:(NSString*)string; // change the HUD loading status while it's showing ++ (void)setAttributedStatus:(NSAttributedString *)attributedString; // change the HUD loading status while it's showing with an attributed string // stops the activity indicator, shows a glyph + status, and dismisses HUD 1s later + +//Attributed + (void)showSuccessWithStatus:(NSString*)string; ++ (void)showSuccessWithAttributedStatus:(NSAttributedString*)attributedString; + (void)showErrorWithStatus:(NSString *)string; ++ (void)showErrorWithAttributedStatus:(NSAttributedString *)attributedString; + (void)showImage:(UIImage*)image status:(NSString*)status; // use 28x28 white pngs ++ (void)showImage:(UIImage*)image attributedStatus:(NSAttributedString*)attributedStatus; + (void)setOffsetFromCenter:(UIOffset)offset; + (void)resetOffsetFromCenter; diff --git a/SVProgressHUD/SVProgressHUD.m b/SVProgressHUD/SVProgressHUD.m index 8f7b73fc..1e3ffb6b 100644 --- a/SVProgressHUD/SVProgressHUD.m +++ b/SVProgressHUD/SVProgressHUD.m @@ -53,24 +53,6 @@ @interface SVProgressHUD () @property (nonatomic, readonly) CGFloat visibleKeyboardHeight; @property (nonatomic, assign) UIOffset offsetFromCenter; - -- (void)showProgress:(float)progress - status:(NSString*)string - maskType:(SVProgressHUDMaskType)hudMaskType; - -- (void)showImage:(UIImage*)image - status:(NSString*)status - duration:(NSTimeInterval)duration; - -- (void)dismiss; - -- (void)setStatus:(NSString*)string; -- (void)registerNotifications; -- (NSDictionary *)notificationUserInfo; -- (void)moveToPoint:(CGPoint)newCenter rotateAngle:(CGFloat)angle; -- (void)positionHUD:(NSNotification*)notification; -- (NSTimeInterval)displayDurationForString:(NSString*)string; - @end @@ -86,7 +68,15 @@ + (SVProgressHUD*)sharedView { #pragma mark - Setters + (void)setStatus:(NSString *)string { - [[self sharedView] setStatus:string]; + SVProgressHUD *sharedView = [self sharedView]; + [sharedView setStatus:string]; + [sharedView updatePosition]; +} + ++ (void)setAttributedStatus:(NSAttributedString *)attributedString { + SVProgressHUD *sharedView = [self sharedView]; + [sharedView setAttributedStatus:attributedString]; + [sharedView updatePosition]; } + (void)setBackgroundColor:(UIColor *)color { @@ -129,6 +119,10 @@ + (void)showWithStatus:(NSString *)status { [[self sharedView] showProgress:-1 status:status maskType:SVProgressHUDMaskTypeNone]; } ++ (void)showWithAttributedStatus:(NSAttributedString *)attributedStatus { + [[self sharedView] showProgress:-1 attributedStatus:attributedStatus maskType:SVProgressHUDMaskTypeNone]; +} + + (void)showWithMaskType:(SVProgressHUDMaskType)maskType { [[self sharedView] showProgress:-1 status:nil maskType:maskType]; } @@ -137,6 +131,10 @@ + (void)showWithStatus:(NSString*)status maskType:(SVProgressHUDMaskType)maskTyp [[self sharedView] showProgress:-1 status:status maskType:maskType]; } ++ (void)showWithAttributedStatus:(NSAttributedString *)attributedStatus maskType:(SVProgressHUDMaskType)maskType { + [[self sharedView] showProgress:-1 attributedStatus:attributedStatus maskType:maskType]; +} + + (void)showProgress:(float)progress { [[self sharedView] showProgress:progress status:nil maskType:SVProgressHUDMaskTypeNone]; } @@ -145,10 +143,18 @@ + (void)showProgress:(float)progress status:(NSString *)status { [[self sharedView] showProgress:progress status:status maskType:SVProgressHUDMaskTypeNone]; } ++ (void)showProgress:(float)progress attributedStatus:(NSAttributedString *)attributedStatus { + [[self sharedView] showProgress:progress attributedStatus:attributedStatus maskType:SVProgressHUDMaskTypeNone]; +} + + (void)showProgress:(float)progress status:(NSString *)status maskType:(SVProgressHUDMaskType)maskType { [[self sharedView] showProgress:progress status:status maskType:maskType]; } ++ (void)showProgress:(float)progress attributedStatus:(NSAttributedString *)attributedStatus maskType:(SVProgressHUDMaskType)maskType { + [[self sharedView] showProgress:progress attributedStatus:attributedStatus maskType:maskType]; +} + #pragma mark - Show then dismiss methods + (void)showSuccessWithStatus:(NSString *)string { @@ -156,16 +162,31 @@ + (void)showSuccessWithStatus:(NSString *)string { [self showImage:SVProgressHUDSuccessImage status:string]; } ++ (void)showSuccessWithAttributedStatus:(NSAttributedString *)attributedString { + [self sharedView]; + [self showImage:SVProgressHUDSuccessImage attributedStatus:attributedString]; +} + + (void)showErrorWithStatus:(NSString *)string { [self sharedView]; [self showImage:SVProgressHUDErrorImage status:string]; } ++ (void)showErrorWithAttributedStatus:(NSAttributedString *)attributedString { + [self sharedView]; + [self showImage:SVProgressHUDErrorImage attributedStatus:attributedString]; +} + + (void)showImage:(UIImage *)image status:(NSString *)string { - NSTimeInterval displayInterval = [[SVProgressHUD sharedView] displayDurationForString:string]; + NSTimeInterval displayInterval = [[SVProgressHUD sharedView] displayDurationForStringLength:string.length]; [[self sharedView] showImage:image status:string duration:displayInterval]; } ++(void)showImage:(UIImage *)image attributedStatus:(NSAttributedString *)attributedStatus { + NSTimeInterval displayInterval = [[SVProgressHUD sharedView] displayDurationForStringLength:attributedStatus.length]; + [[self sharedView] showImage:image attributedStatus:attributedStatus duration:displayInterval]; +} + #pragma mark - Dismiss Methods @@ -350,12 +371,12 @@ - (void)updatePosition { } - (void)setStatus:(NSString *)string { - self.stringLabel.text = string; - [self updatePosition]; - } +- (void)setAttributedStatus:(NSAttributedString *)attributedString { + self.stringLabel.attributedText =attributedString; +} - (void)setFadeOutTimer:(NSTimer *)newTimer { if(_fadeOutTimer) @@ -504,8 +525,17 @@ - (void)overlayViewDidReceiveTouchEvent:(id)sender forEvent:(UIEvent *)event { } #pragma mark - Master show/dismiss methods - - (void)showProgress:(float)progress status:(NSString*)string maskType:(SVProgressHUDMaskType)hudMaskType { + [self setStatus:string]; + [self showProgress:progress maskType:hudMaskType]; +} + +- (void)showProgress:(float)progress attributedStatus:(NSAttributedString *)attributedString maskType:(SVProgressHUDMaskType)hudMaskType { + [self setAttributedStatus:attributedString]; + [self showProgress:progress maskType:hudMaskType]; +} + +- (void)showProgress:(float)progress maskType:(SVProgressHUDMaskType)hudMaskType { if(!self.overlayView.superview){ NSEnumerator *frontToBackWindows = [[[UIApplication sharedApplication]windows]reverseObjectEnumerator]; @@ -525,7 +555,6 @@ - (void)showProgress:(float)progress status:(NSString*)string maskType:(SVProgre self.maskType = hudMaskType; self.progress = progress; - self.stringLabel.text = string; [self updatePosition]; if(progress >= 0) { @@ -546,12 +575,12 @@ - (void)showProgress:(float)progress status:(NSString*)string maskType:(SVProgre if(self.maskType != SVProgressHUDMaskTypeNone) { self.overlayView.userInteractionEnabled = YES; - self.accessibilityLabel = string; + self.accessibilityLabel = self.stringLabel.text; self.isAccessibilityElement = YES; } else { self.overlayView.userInteractionEnabled = NO; - self.hudView.accessibilityLabel = string; + self.hudView.accessibilityLabel = self.stringLabel.text; self.hudView.isAccessibilityElement = YES; } @@ -589,7 +618,7 @@ - (void)showProgress:(float)progress status:(NSString*)string maskType:(SVProgre object:nil userInfo:userInfo]; UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, nil); - UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, string); + UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, self.stringLabel.text); }]; [self setNeedsDisplay]; @@ -616,7 +645,23 @@ - (void)showImage:(UIImage *)image status:(NSString *)string duration:(NSTimeInt if(![self.class isVisible]) [self.class show]; - + + [self setStatus:string]; + [self showImage:image duration:duration]; +} + +- (void)showImage:(UIImage *)image attributedStatus:(NSAttributedString *)attributedString duration:(NSTimeInterval)duration { + self.progress = -1; + [self cancelRingLayerAnimation]; + + if(![self.class isVisible]) + [self.class show]; + + [self setAttributedStatus:attributedString]; + [self showImage:image duration:duration]; +} + +- (void)showImage:(UIImage *)image duration:(NSTimeInterval)duration { if ([self.imageView respondsToSelector:@selector(setTintColor:)]) { self.imageView.tintColor = SVProgressHUDForegroundColor; } else { @@ -625,20 +670,19 @@ - (void)showImage:(UIImage *)image status:(NSString *)string duration:(NSTimeInt self.imageView.image = image; self.imageView.hidden = NO; - self.stringLabel.text = string; [self updatePosition]; [self.indefiniteAnimatedView removeFromSuperview]; if(self.maskType != SVProgressHUDMaskTypeNone) { - self.accessibilityLabel = string; + self.accessibilityLabel = self.stringLabel.text; self.isAccessibilityElement = YES; } else { - self.hudView.accessibilityLabel = string; + self.hudView.accessibilityLabel = self.stringLabel.text; self.hudView.isAccessibilityElement = YES; } UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, nil); - UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, string); + UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, self.stringLabel.text); self.fadeOutTimer = [NSTimer timerWithTimeInterval:duration target:self selector:@selector(dismiss) userInfo:nil repeats:NO]; [[NSRunLoop mainRunLoop] addTimer:self.fadeOutTimer forMode:NSRunLoopCommonModes]; @@ -776,8 +820,8 @@ + (BOOL)isVisible { #pragma mark - Getters -- (NSTimeInterval)displayDurationForString:(NSString*)string { - return MIN((float)string.length*0.06 + 0.3, 5.0); +- (NSTimeInterval)displayDurationForStringLength:(NSUInteger)stringLength { + return MIN((float)stringLength*0.06 + 0.3, 5.0); } - (BOOL)isClear { // used for iOS 7