diff --git a/CSSLayoutKit/Tests/CSSLayoutTests.m b/CSSLayoutKit/Tests/CSSLayoutKitTests.m similarity index 70% rename from CSSLayoutKit/Tests/CSSLayoutTests.m rename to CSSLayoutKit/Tests/CSSLayoutKitTests.m index 076161af..29071e77 100644 --- a/CSSLayoutKit/Tests/CSSLayoutTests.m +++ b/CSSLayoutKit/Tests/CSSLayoutKitTests.m @@ -11,10 +11,10 @@ #import "UIView+CSSLayout.h" -@interface CSSLayoutTests : XCTestCase +@interface CSSLayoutKitTests : XCTestCase @end -@implementation CSSLayoutTests +@implementation CSSLayoutKitTests #ifndef TRAVIS_CI @@ -65,21 +65,35 @@ - (void)testSizeThatFitsAsserts { UIView *view = [[UIView alloc] initWithFrame:CGRectZero]; - XCTAssertThrows([view css_sizeThatFits:CGSizeZero]); - dispatch_sync(dispatch_queue_create("com.facebook.CSSLayout.testing", DISPATCH_QUEUE_SERIAL), ^(void){ - XCTAssertThrows([view css_sizeThatFits:CGSizeZero]); + XCTAssertThrows([view css_intrinsicSize]); }); } - (void)testSizeThatFitsSmoke { - UIView *view = [[UIView alloc] initWithFrame:CGRectZero]; - [view css_setUsesFlexbox:YES]; + UIView *container = [[UIView alloc] initWithFrame:CGRectZero]; + [container css_setUsesFlexbox:YES]; + [container css_setFlexDirection:CSSFlexDirectionRow]; + [container css_setAlignItems:CSSAlignFlexStart]; - const CGSize constrainedSize = CGSizeMake(50, 50); - const CGSize actualSize = [view css_sizeThatFits:constrainedSize]; - XCTAssertTrue(CGSizeEqualToSize(constrainedSize, actualSize), @"Actual Size: %@", NSStringFromCGSize(actualSize)); + UILabel *longTextLabel = [[UILabel alloc] initWithFrame:CGRectZero]; + longTextLabel.text = @"This is a very very very very very very very very long piece of text."; + longTextLabel.lineBreakMode = NSLineBreakByTruncatingTail; + longTextLabel.numberOfLines = 1; + [longTextLabel css_setUsesFlexbox:YES]; + [longTextLabel css_setFlexShrink:1]; + [container addSubview:longTextLabel]; + + UIView *textBadgeView = [[UIView alloc] initWithFrame:CGRectZero]; + [textBadgeView css_setUsesFlexbox:YES]; + [textBadgeView css_setMargin:3.0 forEdge:CSSEdgeLeft]; + [textBadgeView css_setWidth:10]; + [textBadgeView css_setHeight:10]; + [container addSubview:textBadgeView]; + + const CGSize containerSize = [container css_intrinsicSize]; + XCTAssertTrue(CGSizeEqualToSize(CGSizeMake(514,21), containerSize), @"Size is actually %@", NSStringFromCGSize(containerSize)); } - (void)testFrameAndOriginPlacement diff --git a/CSSLayoutKit/UIView+CSSLayout.h b/CSSLayoutKit/UIView+CSSLayout.h index 394c58c1..5f875687 100644 --- a/CSSLayoutKit/UIView+CSSLayout.h +++ b/CSSLayoutKit/UIView+CSSLayout.h @@ -52,8 +52,8 @@ - (void)css_applyLayout; /** - Compute the size of a layout with a constrained size. + Returns the size of the view if no constraints were given. This could equivalent to calling [self sizeThatFits:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)]; */ -- (CGSize)css_sizeThatFits:(CGSize)constrainedSize; +- (CGSize)css_intrinsicSize; @end diff --git a/CSSLayoutKit/UIView+CSSLayout.m b/CSSLayoutKit/UIView+CSSLayout.m index 1c0530df..c5934840 100644 --- a/CSSLayoutKit/UIView+CSSLayout.m +++ b/CSSLayoutKit/UIView+CSSLayout.m @@ -157,32 +157,21 @@ return CSSNodeLayoutGetDirection([self cssNode]); } -- (CGSize)css_sizeThatFits:(CGSize)constrainedSize -{ - NSAssert([NSThread isMainThread], @"CSS Layout calculation must be done on main."); - NSAssert([self css_usesFlexbox], @"CSS Layout is not enabled for this view."); - - CSSAttachNodesFromViewHierachy(self); - - const CSSNodeRef node = [self cssNode]; - CSSNodeCalculateLayout( - node, - constrainedSize.width, - constrainedSize.height, - CSSNodeStyleGetDirection(node)); - - return (CGSize) { - .width = CSSNodeLayoutGetWidth(node), - .height = CSSNodeLayoutGetHeight(node), - }; -} - - (void)css_applyLayout { - [self css_sizeThatFits:self.bounds.size]; + [self calculateLayoutWithSize:self.bounds.size]; CSSApplyLayoutToViewHierarchy(self); } +- (CGSize)css_intrinsicSize +{ + const CGSize constrainedSize = { + .width = CSSUndefined, + .height = CSSUndefined, + }; + return [self calculateLayoutWithSize:constrainedSize]; +} + #pragma mark - Private - (CSSNodeRef)cssNode @@ -197,6 +186,26 @@ return node.cnode; } +- (CGSize)calculateLayoutWithSize:(CGSize)size +{ + NSAssert([NSThread isMainThread], @"CSS Layout calculation must be done on main."); + NSAssert([self css_usesFlexbox], @"CSS Layout is not enabled for this view."); + + CSSAttachNodesFromViewHierachy(self); + + const CSSNodeRef node = [self cssNode]; + CSSNodeCalculateLayout( + node, + size.width, + size.height, + CSSNodeStyleGetDirection(node)); + + return (CGSize) { + .width = CSSNodeLayoutGetWidth(node), + .height = CSSNodeLayoutGetHeight(node), + }; +} + static CSSSize CSSMeasureView( CSSNodeRef node, float width,