From 391060f72ead33d1040c9d85b72c448224ff5536 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Sun, 26 Apr 2020 11:21:19 +0800 Subject: [PATCH] iOS:When set doriccontext's vc,toast will show on it --- .../Pod/Classes/Plugin/DoricModalPlugin.m | 6 +++- doric-iOS/Pod/Classes/Util/DoricUtil.h | 2 ++ doric-iOS/Pod/Classes/Util/DoricUtil.m | 28 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/doric-iOS/Pod/Classes/Plugin/DoricModalPlugin.m b/doric-iOS/Pod/Classes/Plugin/DoricModalPlugin.m index 20c71898..0981ac1c 100644 --- a/doric-iOS/Pod/Classes/Plugin/DoricModalPlugin.m +++ b/doric-iOS/Pod/Classes/Plugin/DoricModalPlugin.m @@ -32,7 +32,11 @@ - (void)toast:(NSDictionary *)dic withPromise:(DoricPromise *)promise { [dic[@"gravity"] also:^(NSNumber *it) { gravity = (DoricGravity) [it integerValue]; }]; - ShowToast(dic[@"msg"], gravity); + if (self.doricContext.vc) { + ShowToastInVC(self.doricContext.vc, dic[@"msg"], gravity); + } else { + ShowToast(dic[@"msg"], gravity); + } }); } diff --git a/doric-iOS/Pod/Classes/Util/DoricUtil.h b/doric-iOS/Pod/Classes/Util/DoricUtil.h index 1ef0e329..cab0fd35 100644 --- a/doric-iOS/Pod/Classes/Util/DoricUtil.h +++ b/doric-iOS/Pod/Classes/Util/DoricUtil.h @@ -45,6 +45,8 @@ NSBundle *_Nonnull DoricBundle(void); void ShowToast(NSString *_Nonnull text, DoricGravity gravity); +void ShowToastInVC(UIViewController *_Nonnull vc, NSString *_Nonnull text, DoricGravity gravity); + UIImage *_Nonnull UIImageWithColor(UIColor *_Nonnull color); BOOL hasNotch(void); diff --git a/doric-iOS/Pod/Classes/Util/DoricUtil.m b/doric-iOS/Pod/Classes/Util/DoricUtil.m index 6769e197..a252375b 100644 --- a/doric-iOS/Pod/Classes/Util/DoricUtil.m +++ b/doric-iOS/Pod/Classes/Util/DoricUtil.m @@ -85,6 +85,34 @@ void ShowToast(NSString *text, DoricGravity gravity) { }); } +void ShowToastInVC(UIViewController *_Nonnull vc, NSString *_Nonnull text, DoricGravity gravity) { + UIView *superView = vc.view; + UILabel *label = [[UILabel alloc] init]; + label.font = [UIFont systemFontOfSize:20.f]; + label.text = text; + label.textAlignment = NSTextAlignmentCenter; + label.layer.masksToBounds = YES; + label.backgroundColor = [UIColor grayColor]; + label.textColor = [UIColor whiteColor]; + [label sizeToFit]; + label.width += 30; + label.height += 10; + label.layer.cornerRadius = label.height / 2; + label.centerX = superView.width / 2; + if ((gravity & DoricGravityBottom) == DoricGravityBottom) { + label.bottom = superView.height - 20; + } else if ((gravity & DoricGravityTop) == DoricGravityTop) { + label.top = 108; + } else { + label.centerY = (superView.height - 88) / 2; + } + + [superView addSubview:label]; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [label removeFromSuperview]; + }); +} + UIImage *UIImageWithColor(UIColor *color) { CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); UIGraphicsBeginImageContext(rect.size);