Created
January 14, 2016 15:00
-
-
Save paramaggarwal/b9baa00fd7bee9a2948f to your computer and use it in GitHub Desktop.
React Native Rigid Aspect Ratio Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "RCTShadowView.h" | |
@interface RNTRigidShadowView : RCTShadowView | |
/** | |
* Makes the box rigid, and sizes itself in flex with the | |
* defined aspect ratio of the CGSize rectangle. | |
*/ | |
@property (nonatomic, assign) CGSize box; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "RNTRigidShadowView.h" | |
@implementation RNTRigidShadowView | |
/* | |
* Sets the measure function on the cssNode, when the `box` rectangle is | |
* set. This helps calculate the height of the node based on width of the parent | |
* and maintains the aspect ratio of the node. | |
*/ | |
static css_dim_t RCTBoxMeasure(void *context, float width) | |
{ | |
RNTRigidShadowView *shadowView = (__bridge RNTRigidShadowView *)context; | |
if (isnan(width)) { | |
width = shadowView.box.width; | |
} | |
CGFloat computedHeight = 0.0f; | |
if (shadowView.box.width > 0) { | |
computedHeight = width * (shadowView.box.height / shadowView.box.width); | |
} | |
css_dim_t result; | |
result.dimensions[CSS_WIDTH] = width; | |
result.dimensions[CSS_HEIGHT] = computedHeight; | |
return result; | |
} | |
- (void)setBox:(CGSize)box | |
{ | |
_box = box; | |
self.cssNode->measure = (box.width && box.height) ? RCTBoxMeasure : nil; | |
[self dirtyLayout]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "RCTView.h" | |
@interface RNTRigidView : RCTView | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "RNTRigidView.h" | |
@implementation RNTRigidView | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "RCTViewManager.h" | |
@interface RNTRigidViewManager : RCTViewManager | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "RNTRigidViewManager.h" | |
#import "RNTRigidView.h" | |
#import "RNTRigidShadowView.h" | |
@implementation RNTRigidViewManager | |
RCT_EXPORT_MODULE(); | |
- (RNTRigidView *)view | |
{ | |
return [[RNTRigidView alloc] initWithFrame:CGRectZero]; | |
} | |
- (RNTRigidShadowView *)shadowView | |
{ | |
return [[RNTRigidShadowView alloc] init]; | |
} | |
- (dispatch_queue_t)methodQueue | |
{ | |
return dispatch_get_main_queue(); | |
} | |
RCT_EXPORT_SHADOW_PROPERTY(box, CGSize) | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm not familiar with the codebase, did you specify to only have access to width or is that a limitation? If you could get height of the element passed in too, then you could do something like the following to support sizing the opposite dimension based on only one being defined.
Here is some JS pseudo-code: