Repository: incubator-weex Updated Branches: refs/heads/master b1a1dfa13 -> 868c1c71e
[WEEX-137][iOS] Weex support css value for safe-area-inset-left, safe-area-inset-right, safe-area-inset-top, and safe-area-inset-bottom you may specify safe-area-inset-left, safe-area-inset-right, safe-area-inset-top, and safe-area-inset-bottom for element and for iPhone X, it will be the safe area and 0 for other model machine. Bug:137 Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/868c1c71 Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/868c1c71 Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/868c1c71 Branch: refs/heads/master Commit: 868c1c71ec5e1482499cee327c3e1cd38eb0c4c3 Parents: b1a1dfa Author: acton393 <zhangxing610...@gmail.com> Authored: Tue Nov 28 12:14:25 2017 +0800 Committer: acton393 <zhangxing610...@gmail.com> Committed: Thu Nov 30 19:42:03 2017 +0800 ---------------------------------------------------------------------- ios/sdk/WeexSDK/Sources/Utility/WXConvert.m | 54 +++++++++++++++++++++++- 1 file changed, 52 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/868c1c71/ios/sdk/WeexSDK/Sources/Utility/WXConvert.m ---------------------------------------------------------------------- diff --git a/ios/sdk/WeexSDK/Sources/Utility/WXConvert.m b/ios/sdk/WeexSDK/Sources/Utility/WXConvert.m index 959631a..f7e82f6 100644 --- a/ios/sdk/WeexSDK/Sources/Utility/WXConvert.m +++ b/ios/sdk/WeexSDK/Sources/Utility/WXConvert.m @@ -22,6 +22,7 @@ #import "WXBoxShadow.h" #import "WXLength.h" #import "WXAssert.h" +#import "WXSDKEngine.h" @implementation WXConvert @@ -65,12 +66,61 @@ WX_NUMBER_CONVERT(NSUInteger, unsignedIntegerValue) if ([valueString hasSuffix:@"px"] || [valueString hasSuffix:@"wx"]) { valueString = [valueString substringToIndex:(valueString.length - 2)]; } + if ([value hasPrefix:@"env(safe-area-inset-"] &&[value hasSuffix:@")"]){ + NSUInteger start = [value rangeOfString:@"env(safe-area-inset-"].location +@"env(safe-area-inset-".length; + NSUInteger end = [value rangeOfString:@")" options:NSBackwardsSearch].location; + value = [value substringWithRange:NSMakeRange(start, end-start)]; + return [self safeAreaInset:value]; + } return [valueString doubleValue]; } return [self double:value]; } ++ (CGFloat)safeAreaInset:(NSString*)value +{ + static NSArray * directionArray = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + directionArray = [NSArray arrayWithObjects:@"top",@"right",@"bottom",@"left", nil]; + }); + if ([directionArray containsObject:value]) { + __block UIEdgeInsets safeAreaInsets = UIEdgeInsetsZero; +#if __IPHONE_11_0 + if (@available(iOS 11.0, *)) { + WXSDKInstance * topInstance = [WXSDKEngine topInstance]; + WXPerformBlockSyncOnMainThread(^{ + safeAreaInsets = topInstance.rootView.safeAreaInsets; + }); + + } else { + // Fallback on earlier versions + } +#endif + NSUInteger key = [directionArray indexOfObject:value]; + CGFloat retValue = 0; + switch (key) { + case 0: + retValue = safeAreaInsets.top; + break; + case 1: + retValue = safeAreaInsets.right; + break; + case 2: + retValue = safeAreaInsets.bottom; + break; + case 3: + retValue = safeAreaInsets.left; + break; + default: + break; + } + return retValue; + } + return 0; +} + + (NSString *)NSString:(id)value { if([value isKindOfClass:[NSString class]]){ @@ -88,7 +138,7 @@ WX_NUMBER_CONVERT(NSUInteger, unsignedIntegerValue) { CGFloat pixel = [self CGFloat:value]; - if ([value isKindOfClass:[NSString class]] && [value hasSuffix:@"wx"]) { + if ([value isKindOfClass:[NSString class]] && ([value hasSuffix:@"wx"]|| [value hasPrefix:@"env(safe-area-inset-"])) { return pixel; } return pixel * scaleFactor; @@ -854,7 +904,7 @@ WX_NUMBER_CONVERT(NSUInteger, unsignedIntegerValue) + (WXPixelType)WXPixelType:(id)value { - CGFloat pixel = [self CGFloat:value]; + CGFloat pixel = [self WXPixelType:value scaleFactor:1.0]; return pixel * WXScreenResizeRadio(); }