You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by so...@apache.org on 2017/02/28 02:29:49 UTC

[09/50] incubator-weex git commit: + [ios] feature: add box-shadow for ios

+ [ios] feature: add box-shadow for ios


Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/3bf78168
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/3bf78168
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/3bf78168

Branch: refs/heads/0.11-dev
Commit: 3bf7816885e9cf470e6e5c9a71190c6d019ae772
Parents: 99b854a
Author: \u9f50\u5c71 <su...@163.com>
Authored: Wed Feb 22 00:19:01 2017 +0800
Committer: \u9f50\u5c71 <su...@163.com>
Committed: Wed Feb 22 00:19:01 2017 +0800

----------------------------------------------------------------------
 .../Sources/Component/WXComponent_internal.h    |  1 +
 ios/sdk/WeexSDK/Sources/Model/WXComponent.m     |  2 +
 ios/sdk/WeexSDK/Sources/Utility/WXBoxShadow.h   | 20 +++++
 ios/sdk/WeexSDK/Sources/Utility/WXBoxShadow.m   | 85 ++++++++++++++++++++
 ios/sdk/WeexSDK/Sources/Utility/WXConvert.h     |  3 +
 ios/sdk/WeexSDK/Sources/Utility/WXConvert.m     | 17 ++++
 .../Sources/View/WXComponent+ViewManagement.m   |  6 ++
 7 files changed, 134 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/3bf78168/ios/sdk/WeexSDK/Sources/Component/WXComponent_internal.h
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Component/WXComponent_internal.h b/ios/sdk/WeexSDK/Sources/Component/WXComponent_internal.h
index 83d3d44..daa404c 100644
--- a/ios/sdk/WeexSDK/Sources/Component/WXComponent_internal.h
+++ b/ios/sdk/WeexSDK/Sources/Component/WXComponent_internal.h
@@ -40,6 +40,7 @@
     UIView *_view;
     CGFloat _opacity;
     WXVisibility  _visibility;
+    WXBoxShadow *_boxShadow;
     
     /**
      *  PseudoClass

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/3bf78168/ios/sdk/WeexSDK/Sources/Model/WXComponent.m
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Model/WXComponent.m b/ios/sdk/WeexSDK/Sources/Model/WXComponent.m
index 76ca9b0..383366a 100644
--- a/ios/sdk/WeexSDK/Sources/Model/WXComponent.m
+++ b/ios/sdk/WeexSDK/Sources/Model/WXComponent.m
@@ -26,6 +26,7 @@
 #import "WXRoundedRect.h"
 #import <pthread/pthread.h>
 #import "WXComponent+PseudoClassManagement.h"
+#import "WXBoxShadow.h"
 
 #pragma clang diagnostic ignored "-Wincomplete-implementation"
 #pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
@@ -200,6 +201,7 @@
             [self _resetNativeBorderRadius];
             _layer.opacity = _opacity;
             _view.backgroundColor = _backgroundColor;
+            [WXBoxShadow configLayer:self.view boxShadow:_boxShadow];
         }
         
         if (_backgroundImage) {

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/3bf78168/ios/sdk/WeexSDK/Sources/Utility/WXBoxShadow.h
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Utility/WXBoxShadow.h b/ios/sdk/WeexSDK/Sources/Utility/WXBoxShadow.h
new file mode 100644
index 0000000..6959207
--- /dev/null
+++ b/ios/sdk/WeexSDK/Sources/Utility/WXBoxShadow.h
@@ -0,0 +1,20 @@
+/**
+ * Created by Weex.
+ * Copyright (c) 2016, Alibaba, Inc. All rights reserved.
+ *
+ * This source code is licensed under the Apache Licence 2.0.
+ * For the full copyright and license information,please view the LICENSE file in the root directory of this source tree.
+ */
+
+#import <Foundation/Foundation.h>
+
+@interface WXBoxShadow : NSObject
+
+@property(nullable) CGColorRef shadowColor;
+@property CGSize shadowOffset;
+@property CGFloat shadowRadius;
+@property BOOL isInset;
+
++(void)configLayer:(UIView *)view boxShadow:(WXBoxShadow *)boxShadow;
++(WXBoxShadow *)getBoxShadowFromString:(NSString *)string;
+@end

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/3bf78168/ios/sdk/WeexSDK/Sources/Utility/WXBoxShadow.m
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Utility/WXBoxShadow.m b/ios/sdk/WeexSDK/Sources/Utility/WXBoxShadow.m
new file mode 100644
index 0000000..c1516d7
--- /dev/null
+++ b/ios/sdk/WeexSDK/Sources/Utility/WXBoxShadow.m
@@ -0,0 +1,85 @@
+/**
+ * Created by Weex.
+ * Copyright (c) 2016, Alibaba, Inc. All rights reserved.
+ *
+ * This source code is licensed under the Apache Licence 2.0.
+ * For the full copyright and license information,please view the LICENSE file in the root directory of this source tree.
+ */
+
+#import "WXBoxShadow.h"
+#import "WXConvert.h"
+
+@implementation WXBoxShadow
+
++(void)configLayer:(UIView *)view boxShadow:(WXBoxShadow *)boxShadow
+{
+    if(!boxShadow){
+        return;
+    }
+    UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:view.bounds];
+    view.layer.masksToBounds = NO;
+    view.layer.shadowColor = boxShadow.shadowColor;
+    view.layer.shadowOffset = boxShadow.shadowOffset;
+    view.layer.shadowRadius = boxShadow.shadowRadius;
+    view.layer.shadowOpacity = 1.0f;
+    view.layer.shadowPath = shadowPath.CGPath;
+}
+
++(WXBoxShadow *)getBoxShadowFromString:(NSString *)string
+{
+    NSError *error = nil;
+    string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
+    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\s{2,}" options:NSRegularExpressionCaseInsensitive error:&error];
+    
+    NSArray *arr = [regex matchesInString:string options:NSMatchingReportCompletion range:NSMakeRange(0, [string length])];
+    
+    arr = [[arr reverseObjectEnumerator] allObjects];
+    
+    for (NSTextCheckingResult *str in arr) {
+        string = [string stringByReplacingCharactersInRange:[str range] withString:@" "];
+    }
+    NSMutableArray *array = [[string componentsSeparatedByString:@" "] mutableCopy];
+    
+    if (array && [array count] > 0) {
+        WXBoxShadow *boxShadow = [WXBoxShadow new];
+        if ([@"inset" isEqualToString: array[0]])
+        {
+            boxShadow.isInset = YES;
+            [array removeObjectAtIndex:0];
+            
+        }
+        
+        for (int i = 0; i < [array count]-1; i++) {
+            switch (i) {
+                case 0:
+                {
+                    CGSize size = boxShadow.shadowOffset;
+                    size.width = [WXConvert CGFloat:array[0]];
+                    boxShadow.shadowOffset = size;
+                }
+                    break;
+                case 1:
+                {
+                    CGSize size = boxShadow.shadowOffset;
+                    size.height = [WXConvert CGFloat:array[1]];
+                    boxShadow.shadowOffset = size;
+                }
+                    break;
+                case 2:
+                {
+                    boxShadow.shadowRadius = [WXConvert CGFloat:array[2]];
+                }
+                    break;
+                    
+                default:
+                    break;
+            }
+        }
+        boxShadow.shadowColor = [WXConvert UIColor:[array lastObject]].CGColor;
+        return boxShadow;
+    }
+    return nil;
+}
+
+
+@end

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/3bf78168/ios/sdk/WeexSDK/Sources/Utility/WXConvert.h
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Utility/WXConvert.h b/ios/sdk/WeexSDK/Sources/Utility/WXConvert.h
index 75658c6..ecd6c4c 100644
--- a/ios/sdk/WeexSDK/Sources/Utility/WXConvert.h
+++ b/ios/sdk/WeexSDK/Sources/Utility/WXConvert.h
@@ -11,6 +11,7 @@
 #import "WXLog.h"
 #import "WXLayoutDefine.h"
 #import "WXType.h"
+#import "WXBoxShadow.h"
 
 @interface WXConvert : NSObject
 
@@ -68,6 +69,8 @@ typedef BOOL WXClipType;
 
 + (WXGradientType)gradientType:(id)value;
 
++ (WXBoxShadow *)WXBoxShadow:(id)value;
+
 @end
 
 @interface WXConvert (Deprecated)

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/3bf78168/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 15c5f6c..99c3ee3 100644
--- a/ios/sdk/WeexSDK/Sources/Utility/WXConvert.m
+++ b/ios/sdk/WeexSDK/Sources/Utility/WXConvert.m
@@ -723,6 +723,23 @@ WX_NUMBER_CONVERT(NSUInteger, unsignedIntegerValue)
     return type;
 }
 
++ (WXBoxShadow *)WXBoxShadow:(id)value
+{
+    NSString *boxShadow = @"";
+    if([value isKindOfClass:[NSString class]]){
+        boxShadow = value;
+    } else if([value isKindOfClass:[NSNumber class]]){
+        boxShadow =  [((NSNumber *)value) stringValue];
+    } else if (value != nil) {
+        boxShadow = nil;
+        WXLogError(@"Convert Error:%@ can not be converted to boxshadow type", value);
+    }
+    if (boxShadow) {
+        return [WXBoxShadow getBoxShadowFromString:boxShadow];
+    }
+    return nil;
+}
+
 @end
 
 @implementation WXConvert (Deprecated)

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/3bf78168/ios/sdk/WeexSDK/Sources/View/WXComponent+ViewManagement.m
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/View/WXComponent+ViewManagement.m b/ios/sdk/WeexSDK/Sources/View/WXComponent+ViewManagement.m
index fa043f9..541c7d5 100644
--- a/ios/sdk/WeexSDK/Sources/View/WXComponent+ViewManagement.m
+++ b/ios/sdk/WeexSDK/Sources/View/WXComponent+ViewManagement.m
@@ -102,10 +102,16 @@
     _transform = styles[@"transform"] || styles[@"transformOrigin"] ?
     [[WXTransform alloc] initWithCSSValue:[WXConvert NSString:styles[@"transform"]] origin:styles[@"transformOrigin"] instance:self.weexInstance] :
     [[WXTransform alloc] initWithCSSValue:nil origin:nil instance:self.weexInstance];
+    _boxShadow = styles[@"boxShadow"]?[WXConvert WXBoxShadow:styles[@"boxShadow"]]:nil;
 }
 
 - (void)_updateViewStyles:(NSDictionary *)styles
 {
+    if (styles[@"boxShadow"]) {
+        [WXBoxShadow configLayer:self.view boxShadow:[WXConvert WXBoxShadow:styles[@"boxShadow"]]];
+        [self setNeedsDisplay];
+    }
+    
     if (styles[@"backgroundColor"]) {
         _backgroundColor = [WXConvert UIColor:styles[@"backgroundColor"]];
         _layer.backgroundColor = _backgroundColor.CGColor;