Skip to content

Commit

Permalink
Merge pull request #10 from molon/dev
Browse files Browse the repository at this point in the history
improve common measure and assert for sublayouts
  • Loading branch information
molon authored Aug 5, 2016
2 parents 63b8a75 + 6fd2d97 commit b20eb03
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 12 deletions.
19 changes: 15 additions & 4 deletions Classes/MLLayout.m
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,19 @@ - (void)applyTagViewsWithRootView:(UIView*)rootView {
}
}

+ (void)checkWhetherIsSubviewsOfSuperview:(UIView*)superview withSublayouts:(NSArray*)sublayouts {
NSParameterAssert(superview);
NSParameterAssert(sublayouts);

for (MLLayout *sublayout in sublayouts) {
if (sublayout.view) {
NSAssert([superview.subviews containsObject:sublayout->_view], @"\n\n`setSublayouts:`\nA sublayout's view(%@):----------------\n%@\n----------------\nmust be subview of view(%@):----------------\n%@\n----------------\nor nil\n\n",NSStringFromClass(sublayout->_view.class),sublayout->_view,NSStringFromClass(superview.class),superview);
}else if (sublayout.sublayouts.count>0) {
[MLLayout checkWhetherIsSubviewsOfSuperview:superview withSublayouts:sublayout.sublayouts];
}
}
}

#pragma mark - debug description
- (NSString*)debugDescriptionWithMode:(MLLayoutDebugMode)mode depth:(NSInteger)depth {
NSMutableString *description = [NSMutableString stringWithFormat:@"%@(%ld):",_view?NSStringFromClass(_view.class):@"MLLayoutHelper",(long)_tag];
Expand Down Expand Up @@ -736,16 +749,14 @@ - (void)setMeasure:(CGSize (^)(MLLayout * _Nonnull, CGFloat, MLLayoutMeasureMode

- (void)setSublayouts:(NSArray * _Nullable)sublayouts {
#ifdef DEBUG
// whether the sublayout's views is descendant of ancestors'view
// whether the sublayout's views is subview of superlayout's view
MLLayout *closeAncestor = self;
while (closeAncestor&&closeAncestor->_view==nil) {
closeAncestor = closeAncestor->_superlayout;
}

if (closeAncestor) {
for (MLLayout *sublayout in sublayouts) {
NSAssert(!sublayout->_view||(sublayout->_view!=closeAncestor->_view&&[sublayout->_view isDescendantOfView:closeAncestor->_view]), @"\n\n`setSublayouts:`\nA sublayout's view(%@):----------------\n%@\n----------------\nmust be descendant of close ancestor'view(%@):----------------\n%@\n----------------\nor nil\n\n",NSStringFromClass(sublayout->_view.class),sublayout->_view,NSStringFromClass(closeAncestor->_view.class),closeAncestor->_view);
}
[MLLayout checkWhetherIsSubviewsOfSuperview:closeAncestor.view withSublayouts:sublayouts];
}

NSMutableArray *views = [NSMutableArray array];
Expand Down
4 changes: 4 additions & 0 deletions Classes/MLTagViewFrameRecord/MLAutoRecordFrameTableViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ - (UITableView*)currentTableView {

+ (CGFloat)heightForRowAtIndexPath:(NSIndexPath*)indexPath tableView:(MLAutoRecordFrameTableView*)tableView beforeLayout:(nullable void (^)(UITableViewCell *protypeCell))beforeLayout {

NSAssert([tableView isKindOfClass:[MLAutoRecordFrameTableView class]], @"tableView must be `MLAutoRecordFrameTableView`");

if (tableView.frame.size.width<=0.0f) {
return 0.0f;
}
Expand Down Expand Up @@ -113,6 +115,8 @@ + (CGFloat)heightForRowAtIndexPath:(NSIndexPath*)indexPath tableView:(MLAutoReco

+ (CGFloat)heightForRowUsingPureMLLayoutAtIndexPath:(NSIndexPath*)indexPath tableView:(MLAutoRecordFrameTableView*)tableView beforeLayout:(nullable void (^)(UITableViewCell *protypeCell))beforeLayout {

NSAssert([tableView isKindOfClass:[MLAutoRecordFrameTableView class]], @"tableView must be `MLAutoRecordFrameTableView`");

if (tableView.frame.size.width<=0.0f) {
return 0.0f;
}
Expand Down
12 changes: 9 additions & 3 deletions Classes/Measurer/UIButton+MLLayout.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,17 @@ - (CGSize)measureWithMLLayout:(MLLayout*)layout width:(CGFloat)width widthMode:(
see: http://stackoverflow.com/a/28287773/2847401
*/
size = [self sizeThatFits:size];
//TODO: maybe the size will be narrowed because of `roundPixelValue`
CGSize fitSize = [self sizeThatFits:size];

//maybe the fit result is bigger than measure size
fitSize.width = fmin(size.width, fitSize.width);
fitSize.height = fmin(size.height, fitSize.height);

//TODO: maybe the size will be narrowed because of `roundPixelValue`, is it will cannot fit size after narrowed?
// size.width = ceil(size.width);
// size.height = ceil(size.height);
return size;

return fitSize;
}

@end
12 changes: 11 additions & 1 deletion Classes/Measurer/UIImageView+MLLayout.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ - (CGSize)measureWithMLLayout:(MLLayout*)layout width:(CGFloat)width widthMode:(
size.width = dimensionClamp(layout.minWidth,layout.maxWidth,size.width);
size.height = dimensionClamp(layout.minHeight,layout.maxHeight,size.height);

return [self sizeThatFits:size];
CGSize fitSize = [self sizeThatFits:size];

//maybe the fit result is bigger than measure size
fitSize.width = fmin(size.width, fitSize.width);
fitSize.height = fmin(size.height, fitSize.height);

//TODO: maybe the size will be narrowed because of `roundPixelValue`, is it will cannot fit size after narrowed?
// size.width = ceil(size.width);
// size.height = ceil(size.height);

return fitSize;
}

@end
12 changes: 9 additions & 3 deletions Classes/Measurer/UILabel+MLLayout.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@ - (CGSize)measureWithMLLayout:(MLLayout*)layout width:(CGFloat)width widthMode:(
//TODO: I think it's a bug of css-layout(min/max), we must fix it ourself now.
size.width = dimensionClamp(layout.minWidth,layout.maxWidth,size.width);
size.height = dimensionClamp(layout.minHeight,layout.maxHeight,size.height);
size = [self sizeThatFits:size];
//TODO: maybe the size will be narrowed because of `roundPixelValue`
CGSize fitSize = [self sizeThatFits:size];

//maybe the fit result is bigger than measure size
fitSize.width = fmin(size.width, fitSize.width);
fitSize.height = fmin(size.height, fitSize.height);

//TODO: maybe the size will be narrowed because of `roundPixelValue`, is it will cannot fit size after narrowed?
// size.width = ceil(size.width);
// size.height = ceil(size.height);
return size;

return fitSize;
}

@end
2 changes: 1 addition & 1 deletion MLLayout.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "MLLayout"
s.version = "0.2.1"
s.version = "0.2.2"
s.summary = "Flexbox in Objective-C, using Facebook's css-layout."

s.homepage = 'https://github.com/molon/MLLayout'
Expand Down

0 comments on commit b20eb03

Please sign in to comment.