diff --git a/React/Views/RCTShadowView+Layout.h b/React/Views/RCTShadowView+Layout.h index fe40dca40..c0711a635 100644 --- a/React/Views/RCTShadowView+Layout.h +++ b/React/Views/RCTShadowView+Layout.h @@ -27,6 +27,13 @@ RCT_EXTERN CGFloat RCTCoreGraphicsFloatFromYogaFloat(float value); @property (nonatomic, readonly) UIEdgeInsets compoundInsets; @property (nonatomic, readonly) CGSize availableSize; +#pragma mark - Measuring + +/** + * Measures shadow view without side-effects. + */ +- (CGSize)sizeThatFitsMinimumSize:(CGSize)minimumSize maximumSize:(CGSize)maximumSize; + #pragma mark - Dirty Propagation Control /** diff --git a/React/Views/RCTShadowView+Layout.m b/React/Views/RCTShadowView+Layout.m index 614a53889..e554e23e1 100644 --- a/React/Views/RCTShadowView+Layout.m +++ b/React/Views/RCTShadowView+Layout.m @@ -81,6 +81,39 @@ CGFloat RCTCoreGraphicsFloatFromYogaFloat(float value) return UIEdgeInsetsInsetRect((CGRect){CGPointZero, self.frame.size}, self.compoundInsets).size; } +#pragma mark - Measuring + +- (CGSize)sizeThatFitsMinimumSize:(CGSize)minimumSize maximumSize:(CGSize)maximumSize +{ + YGNodeRef clonnedYogaNode = YGNodeClone(self.yogaNode); + YGNodeRef constraintYogaNode = YGNodeNewWithConfig([[self class] yogaConfig]); + + YGNodeInsertChild(constraintYogaNode, clonnedYogaNode, 0); + + YGNodeStyleSetMinWidth(constraintYogaNode, RCTYogaFloatFromCoreGraphicsFloat(minimumSize.width)); + YGNodeStyleSetMinHeight(constraintYogaNode, RCTYogaFloatFromCoreGraphicsFloat(minimumSize.height)); + YGNodeStyleSetMaxWidth(constraintYogaNode, RCTYogaFloatFromCoreGraphicsFloat(maximumSize.width)); + YGNodeStyleSetMaxHeight(constraintYogaNode, RCTYogaFloatFromCoreGraphicsFloat(maximumSize.height)); + + YGNodeCalculateLayout( + constraintYogaNode, + YGUndefined, + YGUndefined, + self.layoutDirection + ); + + CGSize measuredSize = (CGSize){ + RCTCoreGraphicsFloatFromYogaFloat(YGNodeLayoutGetWidth(constraintYogaNode)), + RCTCoreGraphicsFloatFromYogaFloat(YGNodeLayoutGetHeight(constraintYogaNode)), + }; + + YGNodeRemoveChild(constraintYogaNode, clonnedYogaNode); + YGNodeFree(constraintYogaNode); + YGNodeFree(clonnedYogaNode); + + return measuredSize; +} + #pragma mark - Dirty Propagation Control - (void)dirtyLayout