feat:iOS support toast gravity

This commit is contained in:
pengfei.zhou 2019-11-20 18:15:04 +08:00
parent 748bac8bc1
commit 2d0570c09f
4 changed files with 44 additions and 16 deletions

View File

@ -14,7 +14,7 @@ class ModalDemo extends Panel {
textAlignment: Gravity.Center, textAlignment: Gravity.Center,
height: 50, height: 50,
}), }),
label('toast'), label('toast on bottom'),
label('Click me').apply({ label('Click me').apply({
width: 200, width: 200,
height: 50, height: 50,
@ -26,6 +26,31 @@ class ModalDemo extends Panel {
modal(context).toast('This is a toast.') modal(context).toast('This is a toast.')
} }
} as IText), } as IText),
label('toast on top'),
label('Click me').apply({
width: 200,
height: 50,
bgColor: colors[0],
textSize: 30,
textColor: Color.WHITE,
layoutConfig: layoutConfig().exactly(),
onClick: () => {
modal(context).toast('This is a toast.', Gravity.Top)
}
} as IText),
label('toast on center'),
label('Click me').apply({
width: 200,
height: 50,
bgColor: colors[0],
textSize: 30,
textColor: Color.WHITE,
layoutConfig: layoutConfig().exactly(),
onClick: () => {
modal(context).toast('This is a toast.', Gravity.Center)
}
} as IText),
]).apply({ ]).apply({
layoutConfig: layoutConfig().atmost().h(LayoutSpec.WRAP_CONTENT), layoutConfig: layoutConfig().atmost().h(LayoutSpec.WRAP_CONTENT),
gravity: Gravity.Center, gravity: Gravity.Center,

View File

@ -20,6 +20,7 @@
// Created by pengfei.zhou on 2019/7/29. // Created by pengfei.zhou on 2019/7/29.
// //
#import <Doric/Doric.h>
#import "DoricModalPlugin.h" #import "DoricModalPlugin.h"
#import "DoricUtil.h" #import "DoricUtil.h"
@ -27,7 +28,11 @@ @implementation DoricModalPlugin
- (void)toast:(NSDictionary *)dic withPromise:(DoricPromise *)promise { - (void)toast:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
showToast(dic[@"msg"]); __block DoricGravity gravity = BOTTOM;
[dic[@"gravity"] also:^(NSNumber *it) {
gravity = (DoricGravity) [it integerValue];
}];
showToast(dic[@"msg"], gravity);
}); });
} }

View File

@ -21,6 +21,7 @@
// //
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import "DoricLayouts.h"
void DoricLog(NSString *_Nonnull format, ...); void DoricLog(NSString *_Nonnull format, ...);
@ -36,7 +37,4 @@ NSBundle *_Nonnull DoricBundle(void);
#define DC_UNLOCK(lock) dispatch_semaphore_signal(lock); #define DC_UNLOCK(lock) dispatch_semaphore_signal(lock);
#endif #endif
void showToastInView(NSString *_Nonnull text, UIView *_Nonnull superView); void showToast(NSString *_Nonnull text, DoricGravity gravity);
void showToast(NSString *_Nonnull text);

View File

@ -48,10 +48,8 @@ void DoricLog(NSString *_Nonnull format, ...) {
} }
void showToastInView(NSString *text, UIView *superView) { void showToast(NSString *text, DoricGravity gravity) {
if (!superView) { UIView *superView = [UIApplication sharedApplication].windows.lastObject;
return;
}
UILabel *label = [[UILabel alloc] init]; UILabel *label = [[UILabel alloc] init];
label.font = [UIFont systemFontOfSize:20.f]; label.font = [UIFont systemFontOfSize:20.f];
label.text = text; label.text = text;
@ -63,15 +61,17 @@ void showToastInView(NSString *text, UIView *superView) {
label.width += 30; label.width += 30;
label.height += 10; label.height += 10;
label.layer.cornerRadius = label.height / 2; label.layer.cornerRadius = label.height / 2;
label.bottom = superView.height - 20;
label.centerX = superView.width / 2; label.centerX = superView.width / 2;
if ((gravity & BOTTOM) == BOTTOM) {
label.bottom = superView.height - 20;
} else if ((gravity & TOP) == TOP) {
label.top = 108;
} else {
label.centerY = (superView.height - 88) / 2;
}
[superView addSubview:label]; [superView addSubview:label];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[label removeFromSuperview]; [label removeFromSuperview];
}); });
}
void showToast(NSString *text) {
showToastInView(text, [UIApplication sharedApplication].windows.lastObject);
} }