From fe292f7b7fce8d9f6992d01762c1d5418b7d6b55 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Wed, 27 Nov 2019 14:16:55 +0800 Subject: [PATCH] feat:Prepare to refact DoricLayouts,because it wont be sized corretly for other views like scrollview or tableview --- iOS/Pod/Classes/Shader/DoricLayouts.h | 2 ++ iOS/Pod/Classes/Shader/DoricLayouts.m | 33 +++++++++++++++++++++++--- iOS/Pod/Classes/Shader/DoricListNode.m | 8 ++++--- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/iOS/Pod/Classes/Shader/DoricLayouts.h b/iOS/Pod/Classes/Shader/DoricLayouts.h index 375435eb..c3ac96b7 100644 --- a/iOS/Pod/Classes/Shader/DoricLayouts.h +++ b/iOS/Pod/Classes/Shader/DoricLayouts.h @@ -98,4 +98,6 @@ typedef NS_ENUM(NSInteger, DoricGravity) { - (CGSize)targetLayoutSize; - (void)layoutSelf; + +- (CGSize)measureSize:(CGSize)targetSize; @end diff --git a/iOS/Pod/Classes/Shader/DoricLayouts.m b/iOS/Pod/Classes/Shader/DoricLayouts.m index 13e82999..b9a609f0 100644 --- a/iOS/Pod/Classes/Shader/DoricLayouts.m +++ b/iOS/Pod/Classes/Shader/DoricLayouts.m @@ -540,17 +540,44 @@ - (CGSize)targetLayoutSize { CGFloat height = self.height; if (self.layoutConfig.widthSpec == DoricLayoutAtMost || self.layoutConfig.widthSpec == DoricLayoutWrapContent) { - width = self.superview.width; + width = self.superview.targetLayoutSize.width; } if (self.layoutConfig.heightSpec == DoricLayoutAtMost || self.layoutConfig.heightSpec == DoricLayoutWrapContent) { - height = self.superview.height; + height = self.superview.targetLayoutSize.height; + } + return CGSizeMake(width, height); +} + +- (CGSize)measureSize:(CGSize)targetSize { + CGFloat width = self.width; + CGFloat height = self.height; + + DoricLayoutConfig *config = self.layoutConfig; + if (!config) { + config = [DoricLayoutConfig new]; + } + if (config.widthSpec == DoricLayoutAtMost + || config.widthSpec == DoricLayoutWrapContent) { + width = targetSize.width - config.margin.left - config.margin.right; + } + if (config.heightSpec == DoricLayoutAtMost + || config.heightSpec == DoricLayoutWrapContent) { + height = targetSize.height - config.margin.top - config.margin.bottom; + } + + CGSize contentSize = [self sizeThatFits:CGSizeMake(width, height)]; + if (config.widthSpec == DoricLayoutWrapContent) { + width = contentSize.width; + } + if (config.heightSpec == DoricLayoutWrapContent) { + height = contentSize.height; } return CGSizeMake(width, height); } - (void)layoutSelf { - CGSize contentSize = [self sizeThatFits:self.targetLayoutSize]; + CGSize contentSize = [self measureSize:self.targetLayoutSize]; if (self.layoutConfig.widthSpec == DoricLayoutAtMost) { self.width = self.superview.width; } else if (self.layoutConfig.widthSpec == DoricLayoutWrapContent) { diff --git a/iOS/Pod/Classes/Shader/DoricListNode.m b/iOS/Pod/Classes/Shader/DoricListNode.m index 4625e6a6..02751c1b 100644 --- a/iOS/Pod/Classes/Shader/DoricListNode.m +++ b/iOS/Pod/Classes/Shader/DoricListNode.m @@ -21,6 +21,7 @@ #import "DoricListNode.h" #import "DoricExtensions.h" #import "DoricListItemNode.h" +#import "DoricLayouts.h" @interface DoricTableViewCell : UITableViewCell @property(nonatomic, strong) DoricListItemNode *doricListItemNode; @@ -39,10 +40,11 @@ - (CGSize)sizeThatFits:(CGSize)size { CGFloat height = 0; for (UIView *child in self.subviews) { - width = MAX(child.width, width); - height += child.height; + CGSize childSize = [child measureSize:size]; + width = MAX(childSize.width, width); + height += childSize.height; } - return CGSizeMake(width, height); + return CGSizeMake(width, MAX(height, size.height)); } return size; }