diff --git a/README.md b/README.md index f5194fb..dc7b0e1 100644 --- a/README.md +++ b/README.md @@ -132,8 +132,8 @@ Toast.js is brought in automatically. There is no need to change or add anything ### Showing a Toast You have two choices to make when showing a Toast: where to show it and for how long. * show(message, duration, position) - * duration: 'short', 'long' - * position: 'top', 'center', 'bottom' +* duration: 'short', 'long' +* position: 'top', 'center', 'bottom' You can also use any of these convenience methods: * showShortTop(message) @@ -150,6 +150,24 @@ You can copy-paste these lines of code for a quick test: ``` +#### Tweaking the vertical position (iOS only for now) +Since 2.1.0 you can add pixels to move the toast up or down. +Note that `showWithOptions` can be used instead of the functions above, but it's not useful unless you want to pass `addPixelsY`. +```js +function showTop() { + window.plugins.toast.showWithOptions( + { + message: "hey there", + duration: "short", + position: "bottom", + addPixelsY: -40 // added a negative value to move it up a bit (default 0) + }, + onSuccess, // optional + onError // optional + ); +} +``` + ### Hiding a Toast In case you want to hide a Toast manually, call this: ```js diff --git a/plugin.xml b/plugin.xml index e666ad0..b18d4e5 100755 --- a/plugin.xml +++ b/plugin.xml @@ -2,7 +2,7 @@ + version="2.1.0"> Toast diff --git a/src/ios/Toast+UIView.h b/src/ios/Toast+UIView.h index a3369e9..08ed54a 100644 --- a/src/ios/Toast+UIView.h +++ b/src/ios/Toast+UIView.h @@ -5,6 +5,7 @@ // each makeToast method creates a view and displays it as toast - (void)makeToast:(NSString *)message; - (void)makeToast:(NSString *)message duration:(NSTimeInterval)interval position:(id)position; +- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position addPixelsY:(int)addPixelsY; - (void)makeToast:(NSString *)message duration:(NSTimeInterval)interval position:(id)position image:(UIImage *)image; - (void)makeToast:(NSString *)message duration:(NSTimeInterval)interval position:(id)position title:(NSString *)title; - (void)makeToast:(NSString *)message duration:(NSTimeInterval)interval position:(id)position title:(NSString *)title image:(UIImage *)image; @@ -18,6 +19,7 @@ // the showToast methods display any view as toast - (void)showToast:(UIView *)toast; -- (void)showToast:(UIView *)toast duration:(NSTimeInterval)interval position:(id)point; +- (void)showToast:(UIView *)toast duration:(NSTimeInterval)interval position:(id)point ; +- (void)showToast:(UIView *)toast duration:(NSTimeInterval)interval position:(id)point addedPixelsY:(int)addPixelsY; @end diff --git a/src/ios/Toast+UIView.m b/src/ios/Toast+UIView.m index d84584d..56fd119 100644 --- a/src/ios/Toast+UIView.m +++ b/src/ios/Toast+UIView.m @@ -53,7 +53,7 @@ static UIView *prevToast = NULL; - (void)hideToast:(UIView *)toast; - (void)toastTimerDidFinish:(NSTimer *)timer; - (void)handleToastTapped:(UITapGestureRecognizer *)recognizer; -- (CGPoint)centerPointForPosition:(id)position withToast:(UIView *)toast; +- (CGPoint)centerPointForPosition:(id)position withToast:(UIView *)toast withAddedPixelsY:(int) addPixelsY; - (UIView *)viewForMessage:(NSString *)message title:(NSString *)title image:(UIImage *)image; - (CGSize)sizeForString:(NSString *)string font:(UIFont *)font constrainedToSize:(CGSize)constrainedSize lineBreakMode:(NSLineBreakMode)lineBreakMode; @@ -73,6 +73,11 @@ static UIView *prevToast = NULL; [self showToast:toast duration:duration position:position]; } +- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position addPixelsY:(int)addPixelsY { + UIView *toast = [self viewForMessage:message title:nil image:nil]; + [self showToast:toast duration:duration position:position addedPixelsY:addPixelsY]; +} + - (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position title:(NSString *)title { UIView *toast = [self viewForMessage:message title:title image:nil]; [self showToast:toast duration:duration position:position]; @@ -93,9 +98,13 @@ static UIView *prevToast = NULL; } - (void)showToast:(UIView *)toast duration:(NSTimeInterval)duration position:(id)point { + [self showToast:toast duration:CSToastDefaultDuration position:CSToastDefaultPosition addedPixelsY:0]; +} + +- (void)showToast:(UIView *)toast duration:(NSTimeInterval)duration position:(id)point addedPixelsY:(int) addPixelsY { [self hideToast]; prevToast = toast; - toast.center = [self centerPointForPosition:point withToast:toast]; + toast.center = [self centerPointForPosition:point withToast:toast withAddedPixelsY:addPixelsY]; toast.alpha = 0.0; if (CSToastHidesOnTap) { @@ -163,7 +172,7 @@ static UIView *prevToast = NULL; if (existingActivityView != nil) return; UIView *activityView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CSToastActivityWidth, CSToastActivityHeight)]; - activityView.center = [self centerPointForPosition:position withToast:activityView]; + activityView.center = [self centerPointForPosition:position withToast:activityView withAddedPixelsY:0]; activityView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:CSToastOpacity]; activityView.alpha = 0.0; activityView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin); @@ -211,22 +220,22 @@ static UIView *prevToast = NULL; #pragma mark - Helpers -- (CGPoint)centerPointForPosition:(id)point withToast:(UIView *)toast { +- (CGPoint)centerPointForPosition:(id)point withToast:(UIView *)toast withAddedPixelsY:(int) addPixelsY { if([point isKindOfClass:[NSString class]]) { // convert string literals @"top", @"bottom", @"center", or any point wrapped in an NSValue object into a CGPoint if([point caseInsensitiveCompare:@"top"] == NSOrderedSame) { - return CGPointMake(self.bounds.size.width/2, (toast.frame.size.height / 2) + CSToastVerticalPadding + CSToastTopBottomOffset); + return CGPointMake(self.bounds.size.width/2, (toast.frame.size.height / 2) + addPixelsY + CSToastVerticalPadding + CSToastTopBottomOffset); } else if([point caseInsensitiveCompare:@"bottom"] == NSOrderedSame) { - return CGPointMake(self.bounds.size.width/2, (self.bounds.size.height - (toast.frame.size.height / 2)) - CSToastVerticalPadding - CSToastTopBottomOffset); + return CGPointMake(self.bounds.size.width/2, (self.bounds.size.height - (toast.frame.size.height / 2)) - CSToastVerticalPadding - CSToastTopBottomOffset + addPixelsY); } else if([point caseInsensitiveCompare:@"center"] == NSOrderedSame) { - return CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2); + return CGPointMake(self.bounds.size.width / 2, (self.bounds.size.height / 2) + addPixelsY); } } else if ([point isKindOfClass:[NSValue class]]) { return [point CGPointValue]; } NSLog(@"Warning: Invalid position for toast."); - return [self centerPointForPosition:CSToastDefaultPosition withToast:toast]; + return [self centerPointForPosition:CSToastDefaultPosition withToast:toast withAddedPixelsY:addPixelsY]; } - (CGSize)sizeForString:(NSString *)string font:(UIFont *)font constrainedToSize:(CGSize)constrainedSize lineBreakMode:(NSLineBreakMode)lineBreakMode { diff --git a/src/ios/Toast.m b/src/ios/Toast.m index 00200ab..e8c5e0d 100644 --- a/src/ios/Toast.m +++ b/src/ios/Toast.m @@ -11,6 +11,7 @@ NSString *message = [options objectForKey:@"message"]; NSString *duration = [options objectForKey:@"duration"]; NSString *position = [options objectForKey:@"position"]; + NSNumber *addPixelsY = [options objectForKey:@"addPixelsY"]; if (![position isEqual: @"top"] && ![position isEqual: @"center"] && ![position isEqual: @"bottom"]) { CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"invalid position. valid options are 'top', 'center' and 'bottom'"]; @@ -29,7 +30,7 @@ return; } - [self.webView makeToast:message duration:durationInt position:position]; + [self.webView makeToast:message duration:durationInt position:position addPixelsY:addPixelsY == nil ? 0 : [addPixelsY intValue]]; CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; diff --git a/www/Toast.js b/www/Toast.js index fd32e04..7782cc0 100755 --- a/www/Toast.js +++ b/www/Toast.js @@ -7,6 +7,7 @@ Toast.prototype.optionsBuilder = function () { var message = null; var duration = "short"; var position = "center"; + var addPixelsY = 0; return { withMessage: function(m) { @@ -24,11 +25,17 @@ Toast.prototype.optionsBuilder = function () { return this; }, + withAddPixelsY: function(y) { + addPixelsY = y; + return this; + }, + build: function() { return { message: message, duration: duration, - position: position + position: position, + addPixelsY: addPixelsY } } }