mirror of
https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin.git
synced 2025-04-11 02:53:08 +08:00
#36 control the vertical position
This commit is contained in:
parent
39dd68c7c0
commit
6208c60449
18
README.md
18
README.md
@ -150,6 +150,24 @@ You can copy-paste these lines of code for a quick test:
|
|||||||
<button onclick="window.plugins.toast.show('Hello there!', 'long', 'center', function(a){console.log('toast success: ' + a)}, function(b){alert('toast error: ' + b)})">Toast show long center</button>
|
<button onclick="window.plugins.toast.show('Hello there!', 'long', 'center', function(a){console.log('toast success: ' + a)}, function(b){alert('toast error: ' + b)})">Toast show long center</button>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### 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
|
### Hiding a Toast
|
||||||
In case you want to hide a Toast manually, call this:
|
In case you want to hide a Toast manually, call this:
|
||||||
```js
|
```js
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
|
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
id="nl.x-services.plugins.toast"
|
id="nl.x-services.plugins.toast"
|
||||||
version="2.0.6">
|
version="2.1.0">
|
||||||
|
|
||||||
<name>Toast</name>
|
<name>Toast</name>
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
// each makeToast method creates a view and displays it as toast
|
// each makeToast method creates a view and displays it as toast
|
||||||
- (void)makeToast:(NSString *)message;
|
- (void)makeToast:(NSString *)message;
|
||||||
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)interval position:(id)position;
|
- (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 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;
|
||||||
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)interval position:(id)position title:(NSString *)title image:(UIImage *)image;
|
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)interval position:(id)position title:(NSString *)title image:(UIImage *)image;
|
||||||
@ -19,5 +20,6 @@
|
|||||||
// the showToast methods display any view as toast
|
// the showToast methods display any view as toast
|
||||||
- (void)showToast:(UIView *)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
|
@end
|
||||||
|
@ -53,7 +53,7 @@ static UIView *prevToast = NULL;
|
|||||||
- (void)hideToast:(UIView *)toast;
|
- (void)hideToast:(UIView *)toast;
|
||||||
- (void)toastTimerDidFinish:(NSTimer *)timer;
|
- (void)toastTimerDidFinish:(NSTimer *)timer;
|
||||||
- (void)handleToastTapped:(UITapGestureRecognizer *)recognizer;
|
- (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;
|
- (UIView *)viewForMessage:(NSString *)message title:(NSString *)title image:(UIImage *)image;
|
||||||
- (CGSize)sizeForString:(NSString *)string font:(UIFont *)font constrainedToSize:(CGSize)constrainedSize lineBreakMode:(NSLineBreakMode)lineBreakMode;
|
- (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];
|
[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 {
|
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position title:(NSString *)title {
|
||||||
UIView *toast = [self viewForMessage:message title:title image:nil];
|
UIView *toast = [self viewForMessage:message title:title image:nil];
|
||||||
[self showToast:toast duration:duration position:position];
|
[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 {
|
- (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];
|
[self hideToast];
|
||||||
prevToast = toast;
|
prevToast = toast;
|
||||||
toast.center = [self centerPointForPosition:point withToast:toast];
|
toast.center = [self centerPointForPosition:point withToast:toast withAddedPixelsY:addPixelsY];
|
||||||
toast.alpha = 0.0;
|
toast.alpha = 0.0;
|
||||||
|
|
||||||
if (CSToastHidesOnTap) {
|
if (CSToastHidesOnTap) {
|
||||||
@ -163,7 +172,7 @@ static UIView *prevToast = NULL;
|
|||||||
if (existingActivityView != nil) return;
|
if (existingActivityView != nil) return;
|
||||||
|
|
||||||
UIView *activityView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CSToastActivityWidth, CSToastActivityHeight)];
|
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.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:CSToastOpacity];
|
||||||
activityView.alpha = 0.0;
|
activityView.alpha = 0.0;
|
||||||
activityView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
|
activityView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
|
||||||
@ -211,22 +220,22 @@ static UIView *prevToast = NULL;
|
|||||||
|
|
||||||
#pragma mark - Helpers
|
#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]]) {
|
if([point isKindOfClass:[NSString class]]) {
|
||||||
// convert string literals @"top", @"bottom", @"center", or any point wrapped in an NSValue object into a CGPoint
|
// convert string literals @"top", @"bottom", @"center", or any point wrapped in an NSValue object into a CGPoint
|
||||||
if([point caseInsensitiveCompare:@"top"] == NSOrderedSame) {
|
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) {
|
} 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) {
|
} 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]]) {
|
} else if ([point isKindOfClass:[NSValue class]]) {
|
||||||
return [point CGPointValue];
|
return [point CGPointValue];
|
||||||
}
|
}
|
||||||
|
|
||||||
NSLog(@"Warning: Invalid position for toast.");
|
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 {
|
- (CGSize)sizeForString:(NSString *)string font:(UIFont *)font constrainedToSize:(CGSize)constrainedSize lineBreakMode:(NSLineBreakMode)lineBreakMode {
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
NSString *message = [options objectForKey:@"message"];
|
NSString *message = [options objectForKey:@"message"];
|
||||||
NSString *duration = [options objectForKey:@"duration"];
|
NSString *duration = [options objectForKey:@"duration"];
|
||||||
NSString *position = [options objectForKey:@"position"];
|
NSString *position = [options objectForKey:@"position"];
|
||||||
|
NSNumber *addPixelsY = [options objectForKey:@"addPixelsY"];
|
||||||
|
|
||||||
if (![position isEqual: @"top"] && ![position isEqual: @"center"] && ![position isEqual: @"bottom"]) {
|
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'"];
|
CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"invalid position. valid options are 'top', 'center' and 'bottom'"];
|
||||||
@ -29,7 +30,7 @@
|
|||||||
return;
|
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];
|
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
|
||||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||||
|
@ -7,6 +7,7 @@ Toast.prototype.optionsBuilder = function () {
|
|||||||
var message = null;
|
var message = null;
|
||||||
var duration = "short";
|
var duration = "short";
|
||||||
var position = "center";
|
var position = "center";
|
||||||
|
var addPixelsY = 0;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
withMessage: function(m) {
|
withMessage: function(m) {
|
||||||
@ -24,11 +25,17 @@ Toast.prototype.optionsBuilder = function () {
|
|||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
withAddPixelsY: function(y) {
|
||||||
|
addPixelsY = y;
|
||||||
|
return this;
|
||||||
|
},
|
||||||
|
|
||||||
build: function() {
|
build: function() {
|
||||||
return {
|
return {
|
||||||
message: message,
|
message: message,
|
||||||
duration: duration,
|
duration: duration,
|
||||||
position: position
|
position: position,
|
||||||
|
addPixelsY: addPixelsY
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user