2019-12-04 13:29:26 +08:00
|
|
|
/*
|
|
|
|
* Copyright [2019] [Doric.Pub]
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
//
|
|
|
|
// Created by pengfei.zhou on 2019/10/23.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "DoricLayouts.h"
|
|
|
|
#import <objc/runtime.h>
|
|
|
|
#import "UIView+Doric.h"
|
2020-04-03 16:36:43 +08:00
|
|
|
#import "DoricExtensions.h"
|
2021-11-17 10:23:21 +08:00
|
|
|
#import <QuartzCore/QuartzCore.h>
|
2019-12-04 13:29:26 +08:00
|
|
|
|
2021-04-07 17:24:33 +08:00
|
|
|
void DoricAddEllipticArcPath(CGMutablePathRef path,
|
|
|
|
CGPoint origin,
|
|
|
|
CGFloat radius,
|
|
|
|
CGFloat startAngle,
|
|
|
|
CGFloat endAngle) {
|
|
|
|
CGAffineTransform t = CGAffineTransformMakeTranslation(origin.x, origin.y);
|
|
|
|
CGPathAddArc(path, &t, 0, 0, radius, startAngle, endAngle, NO);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CGPathRef DoricCreateRoundedRectPath(CGRect bounds,
|
|
|
|
CGFloat leftTop,
|
|
|
|
CGFloat rightTop,
|
|
|
|
CGFloat rightBottom,
|
|
|
|
CGFloat leftBottom) {
|
|
|
|
const CGFloat minX = CGRectGetMinX(bounds);
|
|
|
|
const CGFloat minY = CGRectGetMinY(bounds);
|
|
|
|
const CGFloat maxX = CGRectGetMaxX(bounds);
|
|
|
|
const CGFloat maxY = CGRectGetMaxY(bounds);
|
|
|
|
|
|
|
|
CGMutablePathRef path = CGPathCreateMutable();
|
|
|
|
DoricAddEllipticArcPath(path, (CGPoint) {
|
|
|
|
minX + leftTop, minY + leftTop
|
|
|
|
}, leftTop, M_PI, 3 * M_PI_2);
|
|
|
|
DoricAddEllipticArcPath(path, (CGPoint) {
|
|
|
|
maxX - rightTop, minY + rightTop
|
|
|
|
}, rightTop, 3 * M_PI_2, 0);
|
|
|
|
DoricAddEllipticArcPath(path, (CGPoint) {
|
|
|
|
maxX - rightBottom, maxY - rightBottom
|
|
|
|
}, rightBottom, 0, M_PI_2);
|
|
|
|
DoricAddEllipticArcPath(path, (CGPoint) {
|
|
|
|
minX + leftBottom, maxY - leftBottom
|
|
|
|
}, leftBottom, M_PI_2, M_PI);
|
|
|
|
CGPathCloseSubpath(path);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2019-12-04 13:29:26 +08:00
|
|
|
static const void *kLayoutConfig = &kLayoutConfig;
|
|
|
|
|
2021-11-17 10:23:21 +08:00
|
|
|
@interface DoricShapeLayer : CAShapeLayer
|
|
|
|
@property CGRect viewBounds;
|
|
|
|
@property UIEdgeInsets corners;
|
|
|
|
@end
|
2021-11-16 20:47:18 +08:00
|
|
|
|
2021-11-17 10:23:21 +08:00
|
|
|
@implementation DoricShapeLayer
|
2021-11-16 20:47:18 +08:00
|
|
|
@end
|
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
@implementation UIView (DoricLayout)
|
|
|
|
@dynamic doricLayout;
|
2019-12-04 13:29:26 +08:00
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
- (void)setDoricLayout:(DoricLayout *)doricLayout {
|
|
|
|
objc_setAssociatedObject(self, kLayoutConfig, doricLayout, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
- (DoricLayout *)doricLayout {
|
|
|
|
DoricLayout *layout = objc_getAssociatedObject(self, kLayoutConfig);
|
|
|
|
if (!layout) {
|
|
|
|
layout = [DoricLayout new];
|
|
|
|
layout.width = self.width;
|
|
|
|
layout.height = self.height;
|
|
|
|
layout.view = self;
|
|
|
|
self.doricLayout = layout;
|
|
|
|
}
|
|
|
|
return layout;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2022-08-24 14:19:10 +08:00
|
|
|
typedef NS_ENUM(NSInteger, DoricMeasureSpecMode) {
|
|
|
|
DoricMeasureUnspecified = 0,
|
|
|
|
DoricMeasureExactly = 1,
|
|
|
|
DoricMeasureAtMost = 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DoricMeasureSpec {
|
|
|
|
DoricMeasureSpecMode mode;
|
|
|
|
CGFloat size;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct DoricMeasureSpec DoricMeasureSpec;
|
|
|
|
|
2022-08-24 16:09:31 +08:00
|
|
|
struct DoricSizeAndState {
|
|
|
|
NSUInteger state;
|
|
|
|
CGFloat size;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct DoricSizeAndState DoricSizeAndState;
|
|
|
|
|
2022-08-24 14:19:10 +08:00
|
|
|
DoricMeasureSpec DoricMeasureSpecMake(DoricMeasureSpecMode mode, CGFloat size) {
|
|
|
|
DoricMeasureSpec spec;
|
|
|
|
spec.mode = mode;
|
|
|
|
spec.size = size;
|
|
|
|
return spec;
|
|
|
|
}
|
|
|
|
|
2022-08-24 16:09:31 +08:00
|
|
|
NSUInteger DORIC_MEASURED_STATE_MASK = 0x11;
|
|
|
|
|
|
|
|
NSUInteger DORIC_MEASURED_HEIGHT_STATE_SHIFT = 8;
|
|
|
|
|
|
|
|
NSUInteger DORIC_MEASURED_STATE_TOO_SMALL = 0x01;
|
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
@interface DoricLayout ()
|
2022-08-24 16:09:31 +08:00
|
|
|
/**
|
|
|
|
* width-height
|
|
|
|
* 0xff--ff
|
|
|
|
* */
|
|
|
|
@property(nonatomic, assign) NSUInteger measuredState;
|
2019-12-04 19:02:15 +08:00
|
|
|
@end
|
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
@implementation DoricLayout
|
|
|
|
- (instancetype)init {
|
|
|
|
if (self = [super init]) {
|
|
|
|
_widthSpec = DoricLayoutJust;
|
|
|
|
_heightSpec = DoricLayoutJust;
|
2020-04-11 09:38:27 +08:00
|
|
|
_maxWidth = CGFLOAT_MAX;
|
|
|
|
_maxHeight = CGFLOAT_MAX;
|
2022-08-18 19:57:22 +08:00
|
|
|
_minWidth = -1;
|
|
|
|
_minHeight = -1;
|
2022-08-24 16:09:31 +08:00
|
|
|
_measuredState = 0;
|
2020-04-03 16:36:43 +08:00
|
|
|
}
|
|
|
|
return self;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
2021-07-29 11:36:49 +08:00
|
|
|
- (void)setMeasuredWidth:(CGFloat)measuredWidth {
|
|
|
|
_measuredWidth = MAX(0, measuredWidth);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setMeasuredHeight:(CGFloat)measuredHeight {
|
|
|
|
_measuredHeight = MAX(0, measuredHeight);
|
|
|
|
}
|
|
|
|
|
2020-04-03 18:15:43 +08:00
|
|
|
- (void)apply:(CGSize)frameSize {
|
2020-04-03 16:36:43 +08:00
|
|
|
self.resolved = NO;
|
2020-04-03 18:15:43 +08:00
|
|
|
[self measure:frameSize];
|
2020-04-03 16:36:43 +08:00
|
|
|
[self setFrame];
|
2020-04-08 19:30:08 +08:00
|
|
|
self.resolved = YES;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
2020-04-03 18:15:43 +08:00
|
|
|
- (void)apply {
|
|
|
|
[self apply:self.view.frame.size];
|
|
|
|
}
|
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
- (void)measure:(CGSize)targetSize {
|
2022-08-24 14:19:10 +08:00
|
|
|
[self measureWidth:DoricMeasureSpecMake(DoricMeasureExactly, targetSize.width)
|
|
|
|
height:DoricMeasureSpecMake(DoricMeasureExactly, targetSize.height)];
|
2020-04-13 11:57:31 +08:00
|
|
|
[self layout];
|
|
|
|
}
|
|
|
|
|
2022-08-17 17:54:10 +08:00
|
|
|
#pragma helper
|
|
|
|
|
|
|
|
- (CGFloat)takenWidth {
|
|
|
|
return self.measuredWidth + self.marginLeft + self.marginRight;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CGFloat)takenHeight {
|
|
|
|
return self.measuredHeight + self.marginTop + self.marginBottom;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (BOOL)rect:(CGRect)rect1 equalTo:(CGRect)rect2 {
|
|
|
|
return ABS(rect1.origin.x - rect2.origin.x) < 0.00001f
|
|
|
|
&& ABS(rect1.origin.y - rect2.origin.y) < 0.00001f
|
|
|
|
&& ABS(rect1.size.width - rect2.size.width) < 0.00001f
|
|
|
|
&& ABS(rect1.size.height - rect2.size.height) < 0.00001f;
|
|
|
|
}
|
|
|
|
|
2022-08-18 19:57:22 +08:00
|
|
|
- (NSString *)getLayoutType {
|
|
|
|
switch (self.layoutType) {
|
|
|
|
case DoricVLayout:
|
|
|
|
return @"VLayout";
|
|
|
|
case DoricHLayout:
|
|
|
|
return @"HLayout";
|
|
|
|
case DoricStack:
|
|
|
|
return @"Stack";
|
|
|
|
default:
|
|
|
|
return [NSString stringWithFormat:@"Undefined:%@", self.view.class];
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
2022-08-18 19:57:22 +08:00
|
|
|
}
|
2022-08-17 17:10:25 +08:00
|
|
|
|
2022-08-18 19:57:22 +08:00
|
|
|
|
|
|
|
- (NSString *)getSpecType:(DoricLayoutSpec)spec {
|
|
|
|
switch (spec) {
|
|
|
|
case DoricLayoutJust:
|
|
|
|
return @"JUST";
|
|
|
|
case DoricLayoutMost:
|
|
|
|
return @"MOST";
|
|
|
|
default:
|
|
|
|
return @"FIT";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)toString {
|
|
|
|
return [NSString stringWithFormat:@"%@[width:%@,height:%@]",
|
|
|
|
[self getLayoutType],
|
|
|
|
[self getSpecType:self.widthSpec],
|
|
|
|
[self getSpecType:self.heightSpec]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-08-24 14:19:10 +08:00
|
|
|
#pragma measure
|
|
|
|
|
|
|
|
- (DoricMeasureSpec)getChildMeasureSpec:(DoricMeasureSpec)spec
|
|
|
|
padding:(CGFloat)padding
|
|
|
|
childLayoutSpec:(DoricLayoutSpec)childLayoutSpec
|
|
|
|
childSize:(CGFloat)childSize {
|
|
|
|
DoricMeasureSpecMode specMode = spec.mode;
|
|
|
|
CGFloat specSize = spec.size;
|
|
|
|
CGFloat size = MAX(0, specSize - padding);
|
|
|
|
|
|
|
|
CGFloat resultSize = 0;
|
|
|
|
DoricMeasureSpecMode resultMode = DoricMeasureUnspecified;
|
|
|
|
switch (specMode) {
|
|
|
|
// Parent has imposed an exact size on us
|
|
|
|
case DoricMeasureExactly:
|
|
|
|
if (childLayoutSpec == DoricLayoutJust) {
|
|
|
|
resultSize = childSize;
|
|
|
|
resultMode = DoricMeasureExactly;
|
|
|
|
} else if (childLayoutSpec == DoricLayoutMost) {
|
|
|
|
// Child wants to be our size. So be it.
|
|
|
|
resultSize = size;
|
|
|
|
resultMode = DoricMeasureExactly;
|
|
|
|
} else if (childLayoutSpec == DoricLayoutFit) {
|
|
|
|
// Child wants to determine its own size. It can't be
|
|
|
|
// bigger than us.
|
|
|
|
resultSize = size;
|
|
|
|
resultMode = DoricMeasureAtMost;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// Parent has imposed a maximum size on us
|
|
|
|
case DoricMeasureAtMost:
|
|
|
|
if (childLayoutSpec == DoricLayoutJust) {
|
|
|
|
// Child wants a specific size... so be it
|
|
|
|
resultSize = childSize;
|
|
|
|
resultMode = DoricMeasureExactly;
|
|
|
|
} else if (childLayoutSpec == DoricLayoutMost) {
|
|
|
|
// Child wants to be our size, but our size is not fixed.
|
|
|
|
// Constrain child to not be bigger than us.
|
|
|
|
resultSize = size;
|
|
|
|
resultMode = DoricMeasureAtMost;
|
|
|
|
} else if (childLayoutSpec == DoricLayoutFit) {
|
|
|
|
// Child wants to determine its own size. It can't be
|
|
|
|
// bigger than us.
|
|
|
|
resultSize = size;
|
|
|
|
resultMode = DoricMeasureAtMost;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// Parent asked to see how big we want to be
|
|
|
|
case DoricMeasureUnspecified:
|
|
|
|
if (childLayoutSpec == DoricLayoutJust) {
|
|
|
|
// Child wants a specific size... let them have it
|
|
|
|
resultSize = childSize;
|
|
|
|
resultMode = DoricMeasureExactly;
|
|
|
|
} else if (childLayoutSpec == DoricLayoutMost) {
|
|
|
|
// Child wants to be our size... find out how big it should
|
|
|
|
// be
|
|
|
|
resultSize = size;
|
|
|
|
resultMode = DoricMeasureUnspecified;
|
|
|
|
} else if (childLayoutSpec == DoricLayoutFit) {
|
|
|
|
// Child wants to determine its own size. It can't be
|
|
|
|
// bigger than us.
|
|
|
|
resultSize = size;
|
|
|
|
resultMode = DoricMeasureUnspecified;
|
|
|
|
}
|
|
|
|
break;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
2022-08-24 14:19:10 +08:00
|
|
|
return DoricMeasureSpecMake(resultMode, resultSize);
|
2022-08-18 19:57:22 +08:00
|
|
|
}
|
|
|
|
|
2022-08-24 14:19:10 +08:00
|
|
|
- (void)measureChild:(DoricLayout *)child
|
|
|
|
widthSpec:(DoricMeasureSpec)widthSpec
|
|
|
|
usedWidth:(CGFloat)usedWidth
|
|
|
|
heightSpec:(DoricMeasureSpec)heightSpec
|
|
|
|
usedHeight:(CGFloat)usedHeight {
|
|
|
|
|
2022-08-24 16:09:31 +08:00
|
|
|
DoricMeasureSpec childWidthSpec =
|
|
|
|
[self getChildMeasureSpec:widthSpec
|
|
|
|
padding:self.paddingLeft + self.paddingRight
|
|
|
|
+ child.marginLeft + child.marginRight
|
|
|
|
+ usedWidth
|
|
|
|
childLayoutSpec:child.widthSpec
|
|
|
|
childSize:child.width];
|
|
|
|
|
|
|
|
DoricMeasureSpec childHeightSpec =
|
|
|
|
[self getChildMeasureSpec:heightSpec
|
|
|
|
padding:self.paddingTop + self.paddingBottom
|
|
|
|
+ child.marginTop + child.marginBottom
|
|
|
|
+ usedHeight
|
|
|
|
childLayoutSpec:child.heightSpec
|
|
|
|
childSize:child.height];
|
2022-08-24 14:19:10 +08:00
|
|
|
[child measureWidth:childWidthSpec height:childHeightSpec];
|
2022-08-18 19:57:22 +08:00
|
|
|
}
|
|
|
|
|
2022-08-24 16:09:31 +08:00
|
|
|
|
|
|
|
- (DoricSizeAndState)resolveSizeAndState:(CGFloat)size
|
|
|
|
spec:(DoricMeasureSpec)measureSpec
|
|
|
|
childMeasuredState:(NSUInteger)state {
|
|
|
|
DoricSizeAndState result;
|
2022-08-24 14:19:10 +08:00
|
|
|
DoricMeasureSpecMode specMode = measureSpec.mode;
|
|
|
|
CGFloat specSize = measureSpec.size;
|
2022-08-24 16:09:31 +08:00
|
|
|
result.state = 0;
|
2022-08-24 14:19:10 +08:00
|
|
|
switch (specMode) {
|
|
|
|
case DoricMeasureAtMost:
|
|
|
|
if (specSize < size) {
|
2022-08-24 16:09:31 +08:00
|
|
|
result.size = specSize;
|
|
|
|
result.state = DORIC_MEASURED_STATE_TOO_SMALL;
|
2022-08-24 14:19:10 +08:00
|
|
|
} else {
|
2022-08-24 16:09:31 +08:00
|
|
|
result.size = size;
|
2022-08-24 14:19:10 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DoricMeasureExactly:
|
2022-08-24 16:09:31 +08:00
|
|
|
result.size = specSize;
|
2022-08-24 14:19:10 +08:00
|
|
|
break;
|
|
|
|
case DoricMeasureUnspecified:
|
|
|
|
default:
|
2022-08-24 16:09:31 +08:00
|
|
|
result.size = size;
|
2022-08-24 14:19:10 +08:00
|
|
|
break;
|
2022-08-17 17:10:25 +08:00
|
|
|
}
|
2022-08-24 16:09:31 +08:00
|
|
|
result.state = result.state | (state & DORIC_MEASURED_STATE_MASK);
|
2022-08-24 14:19:10 +08:00
|
|
|
return result;
|
|
|
|
}
|
2022-08-17 17:10:25 +08:00
|
|
|
|
2022-08-24 14:19:10 +08:00
|
|
|
- (CGFloat)getDefaultSize:(CGFloat)size spec:(DoricMeasureSpec)measureSpec {
|
|
|
|
DoricMeasureSpecMode specMode = measureSpec.mode;
|
|
|
|
CGFloat specSize = measureSpec.size;
|
|
|
|
CGFloat result = size;
|
|
|
|
switch (specMode) {
|
|
|
|
case DoricMeasureUnspecified:
|
|
|
|
result = size;
|
|
|
|
break;
|
|
|
|
case DoricMeasureAtMost:
|
|
|
|
case DoricMeasureExactly:
|
|
|
|
result = specSize;
|
|
|
|
break;
|
2022-08-17 17:10:25 +08:00
|
|
|
}
|
2022-08-24 14:19:10 +08:00
|
|
|
return result;
|
|
|
|
}
|
2022-08-17 17:10:25 +08:00
|
|
|
|
2022-08-24 14:19:10 +08:00
|
|
|
- (void)forceUniformWidth:(DoricMeasureSpec)heightMeasureSpec {
|
2022-08-24 16:09:31 +08:00
|
|
|
DoricMeasureSpec uniformMeasureSpec = DoricMeasureSpecMake(DoricMeasureExactly,
|
|
|
|
self.measuredWidth);
|
2022-08-24 14:19:10 +08:00
|
|
|
for (__kindof UIView *subview in self.view.subviews) {
|
|
|
|
DoricLayout *childLayout = subview.doricLayout;
|
|
|
|
if (childLayout.disabled) {
|
|
|
|
continue;
|
2022-08-19 14:52:43 +08:00
|
|
|
}
|
2022-08-24 14:19:10 +08:00
|
|
|
if (childLayout.widthSpec == DoricLayoutMost) {
|
|
|
|
// Temporarily force children to reuse their old measured height
|
|
|
|
// FIXME: this may not be right for something like wrapping text?
|
|
|
|
CGFloat oldHeight = childLayout.height;
|
|
|
|
DoricLayoutSpec oldHeightSpec = childLayout.heightSpec;
|
|
|
|
|
|
|
|
childLayout.height = childLayout.measuredHeight;
|
|
|
|
childLayout.heightSpec = DoricLayoutJust;
|
|
|
|
|
|
|
|
// Remeasure with new dimensions
|
|
|
|
[self measureChild:childLayout
|
|
|
|
widthSpec:uniformMeasureSpec
|
|
|
|
usedWidth:0
|
|
|
|
heightSpec:heightMeasureSpec
|
|
|
|
usedHeight:0];
|
|
|
|
|
|
|
|
childLayout.height = oldHeight;
|
|
|
|
childLayout.heightSpec = oldHeightSpec;
|
2022-08-19 14:52:43 +08:00
|
|
|
}
|
|
|
|
}
|
2022-08-24 14:19:10 +08:00
|
|
|
}
|
2022-08-19 14:52:43 +08:00
|
|
|
|
2022-08-24 14:19:10 +08:00
|
|
|
- (void)forceUniformHeight:(DoricMeasureSpec)widthMeasureSpec {
|
|
|
|
// Pretend that the linear layout has an exact size. This is the measured height of
|
|
|
|
// ourselves. The measured height should be the max height of the children, changed
|
|
|
|
// to accommodate the heightMeasureSpec from the parent
|
2022-08-24 16:09:31 +08:00
|
|
|
DoricMeasureSpec uniformMeasureSpec
|
|
|
|
= DoricMeasureSpecMake(DoricMeasureExactly, self.measuredHeight);
|
2022-08-24 14:19:10 +08:00
|
|
|
for (__kindof UIView *subview in self.view.subviews) {
|
|
|
|
DoricLayout *childLayout = subview.doricLayout;
|
|
|
|
if (childLayout.disabled) {
|
|
|
|
continue;
|
2022-08-19 14:52:43 +08:00
|
|
|
}
|
2022-08-24 14:19:10 +08:00
|
|
|
if (childLayout.heightSpec == DoricLayoutMost) {
|
|
|
|
// Temporarily force children to reuse their old measured width
|
|
|
|
// FIXME: this may not be right for something like wrapping text?
|
|
|
|
CGFloat oldWidth = childLayout.width;
|
|
|
|
DoricLayoutSpec oldWidthSpec = childLayout.widthSpec;
|
|
|
|
|
|
|
|
childLayout.width = childLayout.measuredWidth;
|
|
|
|
childLayout.widthSpec = DoricLayoutJust;
|
|
|
|
|
|
|
|
// Remeasure with new dimensions
|
|
|
|
[self measureChild:childLayout
|
|
|
|
widthSpec:widthMeasureSpec
|
|
|
|
usedWidth:0
|
|
|
|
heightSpec:uniformMeasureSpec
|
|
|
|
usedHeight:0];
|
|
|
|
|
|
|
|
childLayout.width = oldWidth;
|
|
|
|
childLayout.widthSpec = oldWidthSpec;
|
2022-08-19 14:52:43 +08:00
|
|
|
}
|
2022-08-19 12:04:22 +08:00
|
|
|
}
|
2020-04-11 12:20:43 +08:00
|
|
|
}
|
|
|
|
|
2022-08-24 14:19:10 +08:00
|
|
|
- (void)measureWidth:(DoricMeasureSpec)widthSpec height:(DoricMeasureSpec)heightSpec {
|
2020-04-03 16:36:43 +08:00
|
|
|
switch (self.layoutType) {
|
|
|
|
case DoricStack: {
|
2022-08-24 14:19:10 +08:00
|
|
|
[self stackMeasureWidth:widthSpec height:heightSpec];
|
2020-04-03 16:36:43 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DoricVLayout: {
|
2022-08-24 14:19:10 +08:00
|
|
|
[self verticalMeasureWidth:widthSpec height:heightSpec];
|
2020-04-03 16:36:43 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DoricHLayout: {
|
2022-08-24 14:19:10 +08:00
|
|
|
[self horizontalMeasureWidth:widthSpec height:heightSpec];
|
2020-04-03 16:36:43 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2022-08-24 14:19:10 +08:00
|
|
|
[self undefinedMeasureWidth:widthSpec height:heightSpec];
|
2020-04-03 16:36:43 +08:00
|
|
|
break;
|
2020-03-20 15:27:09 +08:00
|
|
|
}
|
|
|
|
}
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
2022-08-24 16:09:31 +08:00
|
|
|
- (void)verticalMeasureWidth:(DoricMeasureSpec)widthMeasureSpec
|
|
|
|
height:(DoricMeasureSpec)heightMeasureSpec {
|
2022-08-24 14:19:10 +08:00
|
|
|
CGFloat maxWidth = 0, totalLength = 0;
|
|
|
|
NSUInteger totalWeight = 0;
|
|
|
|
BOOL hadExtraSpace = NO;
|
2021-11-16 20:47:18 +08:00
|
|
|
|
2022-08-24 14:19:10 +08:00
|
|
|
DoricMeasureSpecMode widthMode = widthMeasureSpec.mode;
|
|
|
|
DoricMeasureSpecMode heightMode = heightMeasureSpec.mode;
|
2021-05-13 16:18:21 +08:00
|
|
|
|
2022-08-24 14:19:10 +08:00
|
|
|
BOOL skippedMeasure = NO;
|
|
|
|
BOOL matchWidth = NO;
|
|
|
|
BOOL allFillParent = YES;
|
|
|
|
|
|
|
|
CGFloat weightedMaxWidth = 0;
|
|
|
|
CGFloat alternativeMaxWidth = 0;
|
2022-08-24 16:09:31 +08:00
|
|
|
NSUInteger childState = 0;
|
2022-08-24 14:19:10 +08:00
|
|
|
// See how tall everyone is. Also remember max width.
|
2019-12-04 13:29:26 +08:00
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
for (__kindof UIView *subview in self.view.subviews) {
|
2022-08-24 14:19:10 +08:00
|
|
|
DoricLayout *childLayout = subview.doricLayout;
|
|
|
|
if (childLayout.disabled) {
|
2020-04-03 16:36:43 +08:00
|
|
|
continue;
|
|
|
|
}
|
2022-08-24 14:19:10 +08:00
|
|
|
hadExtraSpace = YES;
|
|
|
|
totalLength += self.spacing;
|
|
|
|
totalWeight += childLayout.weight;
|
|
|
|
|
|
|
|
if (heightMode == DoricMeasureExactly
|
|
|
|
&& childLayout.heightSpec == DoricLayoutJust
|
|
|
|
&& childLayout.height == 0
|
|
|
|
&& childLayout.weight > 0) {
|
|
|
|
// Optimization: don't bother measuring children who are going to use
|
|
|
|
// leftover space. These views will get measured again down below if
|
|
|
|
// there is any leftover space.
|
2022-08-24 16:09:31 +08:00
|
|
|
totalLength = MAX(totalLength, totalLength
|
|
|
|
+childLayout.marginTop + childLayout.marginBottom);
|
2022-08-24 14:19:10 +08:00
|
|
|
skippedMeasure = YES;
|
|
|
|
} else {
|
|
|
|
CGFloat oldHeight = CGFLOAT_MIN;
|
|
|
|
if (childLayout.heightSpec == DoricLayoutJust
|
|
|
|
&& childLayout.height == 0
|
|
|
|
&& childLayout.weight > 0) {
|
|
|
|
// heightMode is either UNSPECIFIED or AT_MOST, and this
|
|
|
|
// child wanted to stretch to fill available space.
|
|
|
|
// Translate that to WRAP_CONTENT so that it does not end up
|
|
|
|
// with a height of 0
|
|
|
|
oldHeight = 0;
|
|
|
|
childLayout.heightSpec = DoricLayoutFit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine how big this child would like to be. If this or
|
|
|
|
// previous children have given a weight, then we allow it to
|
|
|
|
// use all available space (and we will shrink things later
|
|
|
|
// if needed).
|
|
|
|
[self measureChild:childLayout
|
|
|
|
widthSpec:widthMeasureSpec
|
|
|
|
usedWidth:0
|
|
|
|
heightSpec:heightMeasureSpec
|
|
|
|
usedHeight:totalWeight == 0 ? totalLength : 0];
|
|
|
|
|
|
|
|
if (oldHeight != CGFLOAT_MIN) {
|
|
|
|
childLayout.heightSpec = DoricLayoutJust;
|
|
|
|
childLayout.height = oldHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGFloat childHeight = childLayout.measuredHeight;
|
|
|
|
totalLength = MAX(totalLength, totalLength + childHeight +
|
|
|
|
childLayout.marginTop + childLayout.marginBottom);
|
2022-08-19 14:52:43 +08:00
|
|
|
}
|
2022-08-24 14:19:10 +08:00
|
|
|
|
|
|
|
BOOL matchWidthLocally = NO;
|
|
|
|
if (widthMode != DoricMeasureExactly && childLayout.widthSpec == DoricLayoutMost) {
|
|
|
|
// The width of the linear layout will scale, and at least one
|
|
|
|
// child said it wanted to match our width. Set a flag
|
|
|
|
// indicating that we need to remeasure at least that view when
|
|
|
|
// we know our width.
|
|
|
|
matchWidth = YES;
|
|
|
|
matchWidthLocally = YES;
|
2022-08-19 14:52:43 +08:00
|
|
|
}
|
2022-08-24 14:19:10 +08:00
|
|
|
|
|
|
|
CGFloat margin = childLayout.marginLeft + childLayout.marginRight;
|
|
|
|
CGFloat measuredWidth = childLayout.measuredWidth + margin;
|
|
|
|
maxWidth = MAX(maxWidth, measuredWidth);
|
|
|
|
|
2022-08-24 16:09:31 +08:00
|
|
|
childState = childState | childLayout.measuredState;
|
|
|
|
|
2022-08-24 14:19:10 +08:00
|
|
|
allFillParent = allFillParent && childLayout.widthSpec == DoricLayoutMost;
|
|
|
|
if (childLayout.weight > 0) {
|
|
|
|
/*
|
|
|
|
* Widths of weighted Views are bogus if we end up
|
|
|
|
* remeasuring, so keep them separate.
|
|
|
|
*/
|
2022-08-24 16:09:31 +08:00
|
|
|
weightedMaxWidth
|
|
|
|
= MAX(weightedMaxWidth, matchWidthLocally ? margin : measuredWidth);
|
2022-08-24 14:19:10 +08:00
|
|
|
} else {
|
2022-08-24 16:09:31 +08:00
|
|
|
alternativeMaxWidth
|
|
|
|
= MAX(alternativeMaxWidth, matchWidthLocally ? margin : measuredWidth);
|
2022-08-19 14:52:43 +08:00
|
|
|
}
|
|
|
|
}
|
2022-08-24 14:19:10 +08:00
|
|
|
|
|
|
|
if (hadExtraSpace) {
|
|
|
|
totalLength -= self.spacing;
|
|
|
|
}
|
|
|
|
// Add in our padding
|
|
|
|
totalLength += self.paddingTop + self.paddingBottom;
|
|
|
|
|
|
|
|
CGFloat heightSize = totalLength;
|
|
|
|
|
|
|
|
// Check against our minimum height
|
|
|
|
heightSize = MAX(heightSize, self.minHeight);
|
|
|
|
|
|
|
|
// Reconcile our calculated size with the heightMeasureSpec
|
2022-08-24 16:09:31 +08:00
|
|
|
DoricSizeAndState heightSizeAndState = [self resolveSizeAndState:heightSize
|
|
|
|
spec:heightMeasureSpec
|
|
|
|
childMeasuredState:0];
|
|
|
|
heightSize = heightSizeAndState.size;
|
2022-08-24 14:19:10 +08:00
|
|
|
|
|
|
|
// Either expand children with weight to take up available space or
|
|
|
|
// shrink them if they extend beyond our current bounds. If we skipped
|
|
|
|
// measurement on any children, we need to measure them now.
|
|
|
|
CGFloat delta = heightSize - totalLength;
|
|
|
|
if (skippedMeasure || (delta != 0 && totalWeight > 0)) {
|
|
|
|
NSUInteger weightSum = totalWeight;
|
|
|
|
totalLength = 0;
|
2022-08-19 14:52:43 +08:00
|
|
|
for (__kindof UIView *subview in self.view.subviews) {
|
2022-08-24 14:19:10 +08:00
|
|
|
DoricLayout *childLayout = subview.doricLayout;
|
|
|
|
if (childLayout.disabled) {
|
2022-08-19 14:52:43 +08:00
|
|
|
continue;
|
|
|
|
}
|
2022-08-24 14:19:10 +08:00
|
|
|
NSUInteger childExtra = childLayout.weight;
|
|
|
|
if (childExtra > 0) {
|
|
|
|
// Child said it could absorb extra space -- give him his share
|
|
|
|
CGFloat share = childExtra * delta / weightSum;
|
|
|
|
weightSum -= childExtra;
|
|
|
|
delta -= share;
|
2022-08-24 16:09:31 +08:00
|
|
|
DoricMeasureSpec childWidthMeasureSpec =
|
|
|
|
[self getChildMeasureSpec:widthMeasureSpec
|
|
|
|
padding:self.paddingLeft + self.paddingRight
|
|
|
|
+ childLayout.marginLeft
|
|
|
|
+ childLayout.marginRight
|
|
|
|
childLayoutSpec:childLayout.widthSpec
|
|
|
|
childSize:childLayout.width];
|
2022-08-24 14:19:10 +08:00
|
|
|
// TODO: Use a field like lp.isMeasured to figure out if this
|
|
|
|
// child has been previously measured
|
|
|
|
if (!(childLayout.heightSpec == DoricLayoutJust && childLayout.height == 0)
|
|
|
|
|| heightMode != DoricMeasureExactly) {
|
|
|
|
// child was measured once already above...
|
|
|
|
// base new measurement on stored values
|
|
|
|
CGFloat childHeight = childLayout.measuredHeight + share;
|
|
|
|
if (childHeight < 0) {
|
|
|
|
childHeight = 0;
|
|
|
|
}
|
|
|
|
[childLayout measureWidth:childWidthMeasureSpec
|
2022-08-24 16:09:31 +08:00
|
|
|
height:DoricMeasureSpecMake(DoricMeasureExactly,
|
|
|
|
childHeight)];
|
2022-08-24 14:19:10 +08:00
|
|
|
} else {
|
|
|
|
// child was skipped in the loop above.
|
|
|
|
// Measure for this first time here
|
|
|
|
[childLayout measureWidth:childWidthMeasureSpec
|
2022-08-24 16:09:31 +08:00
|
|
|
height:DoricMeasureSpecMake(DoricMeasureExactly,
|
|
|
|
share > 0 ? share : 0)];
|
2022-08-24 14:19:10 +08:00
|
|
|
}
|
2022-08-24 16:09:31 +08:00
|
|
|
|
|
|
|
// Child may now not fit in vertical dimension.
|
|
|
|
childState = childState | (childLayout.measuredState
|
|
|
|
& (DORIC_MEASURED_STATE_MASK >> DORIC_MEASURED_HEIGHT_STATE_SHIFT));
|
2022-08-24 14:19:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CGFloat margin = childLayout.marginLeft + childLayout.marginRight;
|
|
|
|
CGFloat measuredWidth = childLayout.measuredWidth + margin;
|
|
|
|
maxWidth = MAX(maxWidth, measuredWidth);
|
|
|
|
|
2022-08-24 16:09:31 +08:00
|
|
|
BOOL matchWidthLocally = widthMode != DoricMeasureExactly
|
|
|
|
&& childLayout.widthSpec == DoricLayoutMost;
|
2022-08-24 14:19:10 +08:00
|
|
|
|
2022-08-24 16:09:31 +08:00
|
|
|
alternativeMaxWidth = MAX(alternativeMaxWidth,
|
|
|
|
matchWidthLocally ? margin : measuredWidth);
|
2022-08-24 14:19:10 +08:00
|
|
|
|
|
|
|
allFillParent = allFillParent && childLayout.widthSpec == DoricLayoutMost;
|
|
|
|
|
|
|
|
totalLength = MAX(totalLength, totalLength + childLayout.measuredHeight
|
|
|
|
+ childLayout.marginTop + childLayout.marginBottom);
|
|
|
|
|
|
|
|
totalLength += self.spacing;
|
|
|
|
}
|
|
|
|
if (hadExtraSpace) {
|
|
|
|
totalLength -= self.spacing;
|
2022-08-19 14:52:43 +08:00
|
|
|
}
|
2022-08-24 14:19:10 +08:00
|
|
|
totalLength += self.paddingTop + self.paddingBottom;
|
|
|
|
// TODO: Should we recompute the heightMeasureSpec based on the new total length?
|
|
|
|
} else {
|
|
|
|
alternativeMaxWidth = MAX(alternativeMaxWidth, weightedMaxWidth);
|
|
|
|
};
|
|
|
|
if (!allFillParent && widthMode != DoricMeasureExactly) {
|
|
|
|
maxWidth = alternativeMaxWidth;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
2022-08-24 14:19:10 +08:00
|
|
|
maxWidth += self.paddingLeft + self.paddingRight;
|
|
|
|
|
|
|
|
// Check against our minimum width
|
|
|
|
maxWidth = MAX(maxWidth, self.minWidth);
|
2019-12-04 13:29:26 +08:00
|
|
|
|
2022-08-24 16:09:31 +08:00
|
|
|
DoricSizeAndState widthSizeAndState = [self resolveSizeAndState:maxWidth
|
|
|
|
spec:widthMeasureSpec
|
|
|
|
childMeasuredState:childState];
|
|
|
|
|
|
|
|
self.measuredWidth = widthSizeAndState.size;
|
|
|
|
|
2022-08-24 14:19:10 +08:00
|
|
|
self.measuredHeight = heightSize;
|
|
|
|
|
2022-08-24 16:09:31 +08:00
|
|
|
self.measuredState = (widthSizeAndState.state
|
|
|
|
<< DORIC_MEASURED_HEIGHT_STATE_SHIFT) | heightSizeAndState.state;
|
2022-08-24 14:19:10 +08:00
|
|
|
if (matchWidth) {
|
|
|
|
[self forceUniformWidth:heightMeasureSpec];
|
|
|
|
}
|
|
|
|
self.totalLength = totalLength;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
2022-08-24 14:19:10 +08:00
|
|
|
|
2022-08-24 16:09:31 +08:00
|
|
|
- (void)horizontalMeasureWidth:(DoricMeasureSpec)widthMeasureSpec
|
|
|
|
height:(DoricMeasureSpec)heightMeasureSpec {
|
2022-08-24 14:19:10 +08:00
|
|
|
CGFloat maxHeight = 0, totalLength = 0;
|
|
|
|
NSUInteger totalWeight = 0;
|
|
|
|
BOOL hadExtraSpace = NO;
|
|
|
|
|
|
|
|
DoricMeasureSpecMode widthMode = widthMeasureSpec.mode;
|
|
|
|
DoricMeasureSpecMode heightMode = heightMeasureSpec.mode;
|
|
|
|
|
|
|
|
BOOL skippedMeasure = NO;
|
|
|
|
BOOL matchHeight = NO;
|
|
|
|
BOOL allFillParent = YES;
|
|
|
|
|
|
|
|
CGFloat weightedMaxHeight = 0;
|
|
|
|
CGFloat alternativeMaxHeight = 0;
|
|
|
|
BOOL isExactly = widthMode == DoricMeasureExactly;
|
2022-08-24 16:09:31 +08:00
|
|
|
|
|
|
|
NSUInteger childState = 0;
|
|
|
|
|
2022-08-24 14:19:10 +08:00
|
|
|
// See how wide everyone is. Also remember max height.
|
2020-04-03 16:36:43 +08:00
|
|
|
for (__kindof UIView *subview in self.view.subviews) {
|
2022-08-24 14:19:10 +08:00
|
|
|
DoricLayout *childLayout = subview.doricLayout;
|
|
|
|
if (childLayout.disabled) {
|
2020-04-03 16:36:43 +08:00
|
|
|
continue;
|
|
|
|
}
|
2022-08-24 14:19:10 +08:00
|
|
|
hadExtraSpace = YES;
|
|
|
|
totalLength += self.spacing;
|
|
|
|
totalWeight += childLayout.weight;
|
|
|
|
|
|
|
|
if (widthMode == DoricMeasureExactly
|
|
|
|
&& childLayout.widthSpec == DoricLayoutJust
|
|
|
|
&& childLayout.width == 0
|
|
|
|
&& childLayout.weight > 0) {
|
|
|
|
// Optimization: don't bother measuring children who are going to use
|
|
|
|
// leftover space. These views will get measured again down below if
|
|
|
|
// there is any leftover space.
|
|
|
|
if (isExactly) {
|
|
|
|
totalLength += childLayout.marginLeft + childLayout.marginRight;
|
2022-08-23 17:50:46 +08:00
|
|
|
} else {
|
2022-08-24 16:09:31 +08:00
|
|
|
totalLength = MAX(totalLength, totalLength
|
|
|
|
+childLayout.marginLeft + childLayout.marginRight);
|
2022-08-23 17:50:46 +08:00
|
|
|
}
|
2022-08-24 14:19:10 +08:00
|
|
|
skippedMeasure = YES;
|
|
|
|
} else {
|
|
|
|
CGFloat oldWidth = CGFLOAT_MIN;
|
|
|
|
if (childLayout.widthSpec == DoricLayoutJust
|
|
|
|
&& childLayout.width == 0
|
|
|
|
&& childLayout.weight > 0) {
|
|
|
|
// widthMode is either UNSPECIFIED or AT_MOST, and this
|
|
|
|
// child
|
|
|
|
// wanted to stretch to fill available space. Translate that to
|
|
|
|
// WRAP_CONTENT so that it does not end up with a width of 0
|
|
|
|
oldWidth = 0;
|
|
|
|
childLayout.widthSpec = DoricLayoutFit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine how big this child would like to be. If this or
|
|
|
|
// previous children have given a weight, then we allow it to
|
|
|
|
// use all available space (and we will shrink things later
|
|
|
|
// if needed).
|
|
|
|
[self measureChild:childLayout
|
|
|
|
widthSpec:widthMeasureSpec
|
|
|
|
usedWidth:totalWeight == 0 ? totalLength : 0
|
|
|
|
heightSpec:heightMeasureSpec
|
|
|
|
usedHeight:0];
|
|
|
|
|
|
|
|
if (oldWidth != CGFLOAT_MIN) {
|
|
|
|
childLayout.widthSpec = DoricLayoutJust;
|
|
|
|
childLayout.width = oldWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGFloat childWidth = childLayout.measuredHeight;
|
|
|
|
if (isExactly) {
|
|
|
|
totalLength += childWidth + childLayout.marginLeft + childLayout.marginRight;
|
|
|
|
} else {
|
|
|
|
totalLength = MAX(totalLength, totalLength + childWidth +
|
|
|
|
childLayout.marginLeft + childLayout.marginRight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL matchHeightLocally = NO;
|
|
|
|
if (heightMode != DoricMeasureExactly && childLayout.heightSpec == DoricLayoutMost) {
|
|
|
|
// The height of the linear layout will scale, and at least one
|
|
|
|
// child said it wanted to match our height. Set a flag indicating that
|
|
|
|
// we need to remeasure at least that view when we know our height.
|
|
|
|
matchHeight = YES;
|
|
|
|
matchHeightLocally = YES;
|
2022-08-23 17:50:46 +08:00
|
|
|
}
|
|
|
|
|
2022-08-24 14:19:10 +08:00
|
|
|
CGFloat margin = childLayout.marginTop + childLayout.marginBottom;
|
|
|
|
CGFloat childHeight = childLayout.measuredHeight + margin;
|
2022-08-24 16:09:31 +08:00
|
|
|
|
|
|
|
childState = childState | childLayout.measuredState;
|
|
|
|
|
2022-08-24 14:19:10 +08:00
|
|
|
maxHeight = MAX(maxHeight, childHeight);
|
|
|
|
|
|
|
|
allFillParent = allFillParent && childLayout.heightSpec == DoricLayoutMost;
|
|
|
|
|
|
|
|
if (childLayout.weight > 0) {
|
|
|
|
/*
|
|
|
|
* Heights of weighted Views are bogus if we end up
|
|
|
|
* remeasuring, so keep them separate.
|
|
|
|
*/
|
|
|
|
weightedMaxHeight = MAX(weightedMaxHeight, matchHeightLocally ? margin : childHeight);
|
|
|
|
} else {
|
2022-08-24 16:09:31 +08:00
|
|
|
alternativeMaxHeight = MAX(alternativeMaxHeight,
|
|
|
|
matchHeightLocally ? margin : childHeight);
|
2022-08-23 17:50:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 14:19:10 +08:00
|
|
|
if (hadExtraSpace) {
|
|
|
|
totalLength -= self.spacing;
|
|
|
|
}
|
|
|
|
// Add in our padding
|
|
|
|
totalLength += self.paddingLeft + self.paddingRight;
|
|
|
|
|
|
|
|
CGFloat widthSize = totalLength;
|
|
|
|
|
|
|
|
// Check against our minimum width
|
|
|
|
widthSize = MAX(widthSize, self.minWidth);
|
|
|
|
|
|
|
|
// Reconcile our calculated size with the widthMeasureSpec
|
2022-08-24 16:09:31 +08:00
|
|
|
DoricSizeAndState widthSizeAndState = [self resolveSizeAndState:widthSize
|
|
|
|
spec:widthMeasureSpec
|
|
|
|
childMeasuredState:0];
|
|
|
|
widthSize = widthSizeAndState.size;
|
2022-08-24 14:19:10 +08:00
|
|
|
|
|
|
|
// Either expand children with weight to take up available space or
|
|
|
|
// shrink them if they extend beyond our current bounds. If we skipped
|
|
|
|
// measurement on any children, we need to measure them now.
|
|
|
|
CGFloat delta = widthSize - totalLength;
|
|
|
|
if (skippedMeasure || (delta != 0 && totalWeight > 0)) {
|
|
|
|
NSUInteger weightSum = totalWeight;
|
|
|
|
totalLength = 0;
|
2020-04-03 17:17:40 +08:00
|
|
|
for (__kindof UIView *subview in self.view.subviews) {
|
2022-08-24 14:19:10 +08:00
|
|
|
DoricLayout *childLayout = subview.doricLayout;
|
|
|
|
if (childLayout.disabled) {
|
2020-04-03 17:17:40 +08:00
|
|
|
continue;
|
|
|
|
}
|
2022-08-24 14:19:10 +08:00
|
|
|
NSUInteger childExtra = childLayout.weight;
|
|
|
|
if (childExtra > 0) {
|
|
|
|
// Child said it could absorb extra space -- give him his share
|
|
|
|
CGFloat share = childExtra * delta / weightSum;
|
|
|
|
weightSum -= childExtra;
|
|
|
|
delta -= share;
|
|
|
|
|
2022-08-24 16:09:31 +08:00
|
|
|
DoricMeasureSpec childHeightMeasureSpec
|
|
|
|
= [self getChildMeasureSpec:heightMeasureSpec
|
|
|
|
padding:self.paddingTop + self.paddingBottom
|
|
|
|
+ childLayout.marginTop
|
|
|
|
+ childLayout.marginBottom
|
|
|
|
childLayoutSpec:childLayout.heightSpec
|
|
|
|
childSize:childLayout.height];
|
2022-08-24 14:19:10 +08:00
|
|
|
|
|
|
|
// TODO: Use a field like lp.isMeasured to figure out if this
|
|
|
|
// child has been previously measured
|
|
|
|
if (!(childLayout.widthSpec == DoricLayoutJust && childLayout.width == 0)
|
|
|
|
|| widthMode != DoricMeasureExactly) {
|
|
|
|
// child was measured once already above ... base new measurement
|
|
|
|
// on stored values
|
|
|
|
CGFloat childWidth = childLayout.measuredWidth + share;
|
|
|
|
if (childWidth < 0) {
|
|
|
|
childWidth = 0;
|
|
|
|
}
|
|
|
|
[childLayout measureWidth:DoricMeasureSpecMake(DoricMeasureExactly, childWidth)
|
|
|
|
height:childHeightMeasureSpec];
|
|
|
|
} else {
|
|
|
|
// child was skipped in the loop above. Measure for this first time here
|
2022-08-24 16:09:31 +08:00
|
|
|
[childLayout measureWidth:DoricMeasureSpecMake(DoricMeasureExactly,
|
|
|
|
share > 0 ? share : 0)
|
2022-08-24 14:19:10 +08:00
|
|
|
height:childHeightMeasureSpec];
|
|
|
|
}
|
2022-08-24 16:09:31 +08:00
|
|
|
// Child may now not fit in horizontal dimension.
|
|
|
|
childState = childState | (childLayout.measuredState & DORIC_MEASURED_STATE_MASK);
|
2022-08-24 14:19:10 +08:00
|
|
|
}
|
|
|
|
if (isExactly) {
|
2022-08-24 16:09:31 +08:00
|
|
|
totalLength += childLayout.measuredWidth
|
|
|
|
+ childLayout.marginLeft + childLayout.marginRight;
|
2022-08-24 14:19:10 +08:00
|
|
|
} else {
|
|
|
|
totalLength = MAX(totalLength, totalLength + childLayout.measuredWidth
|
|
|
|
+ childLayout.marginLeft + childLayout.marginRight);
|
2022-08-19 14:52:43 +08:00
|
|
|
}
|
2022-08-24 14:19:10 +08:00
|
|
|
totalLength += self.spacing;
|
|
|
|
|
2022-08-24 16:09:31 +08:00
|
|
|
BOOL matchHeightLocally = heightMode != DoricMeasureExactly
|
|
|
|
&& childLayout.heightSpec == DoricLayoutMost;
|
2022-08-24 14:19:10 +08:00
|
|
|
|
|
|
|
CGFloat margin = childLayout.marginTop + childLayout.marginBottom;
|
|
|
|
|
|
|
|
CGFloat childHeight = childLayout.measuredHeight + margin;
|
|
|
|
|
|
|
|
maxHeight = MAX(maxHeight, childHeight);
|
|
|
|
|
2022-08-24 16:09:31 +08:00
|
|
|
alternativeMaxHeight = MAX(alternativeMaxHeight,
|
|
|
|
matchHeightLocally ? margin : childHeight);
|
2022-08-24 14:19:10 +08:00
|
|
|
|
|
|
|
allFillParent = allFillParent && childLayout.heightSpec == DoricLayoutMost;
|
2021-07-29 11:36:49 +08:00
|
|
|
}
|
2022-08-24 14:19:10 +08:00
|
|
|
|
|
|
|
if (hadExtraSpace) {
|
|
|
|
totalLength -= self.spacing;
|
2020-04-03 17:17:40 +08:00
|
|
|
}
|
2022-08-24 14:19:10 +08:00
|
|
|
|
|
|
|
// Add in our padding
|
|
|
|
totalLength += self.paddingLeft + self.paddingRight;
|
|
|
|
// TODO: Should we update widthSize with the new total length?
|
|
|
|
} else {
|
|
|
|
alternativeMaxHeight = MAX(alternativeMaxHeight, weightedMaxHeight);
|
2020-04-03 17:17:40 +08:00
|
|
|
}
|
2022-08-24 14:19:10 +08:00
|
|
|
|
|
|
|
if (!allFillParent && heightMode != DoricMeasureExactly) {
|
|
|
|
maxHeight = alternativeMaxHeight;
|
2022-08-23 18:15:38 +08:00
|
|
|
}
|
2020-04-03 16:36:43 +08:00
|
|
|
|
2022-08-24 14:19:10 +08:00
|
|
|
maxHeight += self.paddingTop + self.paddingBottom;
|
|
|
|
|
|
|
|
// Check against our minimum height
|
|
|
|
maxHeight = MAX(maxHeight, self.minHeight);
|
|
|
|
|
|
|
|
self.measuredWidth = widthSize;
|
2022-08-24 16:09:31 +08:00
|
|
|
DoricSizeAndState heightSizeAndState = [self resolveSizeAndState:maxHeight
|
|
|
|
spec:heightMeasureSpec
|
|
|
|
childMeasuredState:childState
|
|
|
|
<< DORIC_MEASURED_HEIGHT_STATE_SHIFT];
|
|
|
|
|
|
|
|
self.measuredHeight = heightSizeAndState.size;
|
|
|
|
|
|
|
|
self.measuredState = ((widthSizeAndState.state | (childState & DORIC_MEASURED_STATE_MASK))
|
|
|
|
<< DORIC_MEASURED_HEIGHT_STATE_SHIFT) | heightSizeAndState.state;
|
2022-08-24 14:19:10 +08:00
|
|
|
|
|
|
|
if (matchHeight) {
|
|
|
|
[self forceUniformHeight:widthMeasureSpec];
|
|
|
|
}
|
|
|
|
self.totalLength = totalLength;
|
2020-04-03 16:36:43 +08:00
|
|
|
}
|
|
|
|
|
2022-08-24 16:09:31 +08:00
|
|
|
- (void)stackMeasureWidth:(DoricMeasureSpec)widthMeasureSpec
|
|
|
|
height:(DoricMeasureSpec)heightMeasureSpec {
|
2022-08-24 14:19:10 +08:00
|
|
|
CGFloat maxWidth = 0;
|
|
|
|
CGFloat maxHeight = 0;
|
|
|
|
BOOL measureMatchParentChildren = widthMeasureSpec.mode != DoricMeasureExactly
|
|
|
|
|| heightMeasureSpec.mode != DoricMeasureExactly;
|
|
|
|
NSMutableArray *matchParentChildren = [NSMutableArray new];
|
2022-08-24 16:09:31 +08:00
|
|
|
NSUInteger childState = 0;
|
2020-04-03 16:36:43 +08:00
|
|
|
for (__kindof UIView *subview in self.view.subviews) {
|
2022-08-24 14:19:10 +08:00
|
|
|
DoricLayout *childLayout = subview.doricLayout;
|
|
|
|
if (childLayout.disabled) {
|
2019-12-04 13:29:26 +08:00
|
|
|
continue;
|
|
|
|
}
|
2022-08-24 14:19:10 +08:00
|
|
|
[self measureChild:childLayout
|
|
|
|
widthSpec:widthMeasureSpec usedWidth:0
|
|
|
|
heightSpec:heightMeasureSpec usedHeight:0];
|
2022-08-24 16:09:31 +08:00
|
|
|
maxWidth = MAX(maxWidth, childLayout.measuredWidth
|
|
|
|
+ childLayout.marginLeft + childLayout.marginRight);
|
|
|
|
maxHeight = MAX(maxHeight, childLayout.measuredHeight
|
|
|
|
+ childLayout.marginTop + childLayout.marginBottom);
|
|
|
|
childState = childState | childLayout.measuredState;
|
2022-08-24 14:19:10 +08:00
|
|
|
if (measureMatchParentChildren) {
|
2022-08-24 16:09:31 +08:00
|
|
|
if (childLayout.widthSpec == DoricLayoutMost
|
|
|
|
|| childLayout.heightSpec == DoricLayoutMost) {
|
2022-08-24 14:19:10 +08:00
|
|
|
[matchParentChildren addObject:childLayout];
|
2022-08-23 17:50:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-24 14:19:10 +08:00
|
|
|
|
|
|
|
// Account for padding too
|
|
|
|
maxWidth += self.paddingLeft + self.paddingRight;
|
|
|
|
maxHeight += self.paddingTop + self.paddingBottom;
|
|
|
|
|
|
|
|
// Check against our minimum height and width
|
|
|
|
maxWidth = MAX(maxWidth, self.minWidth);
|
|
|
|
maxHeight = MAX(maxHeight, self.minHeight);
|
|
|
|
|
2022-08-24 16:09:31 +08:00
|
|
|
DoricSizeAndState widthSizeAndState = [self resolveSizeAndState:maxWidth
|
|
|
|
spec:widthMeasureSpec
|
|
|
|
childMeasuredState:childState];
|
|
|
|
DoricSizeAndState heightSizeAndState = [self resolveSizeAndState:maxHeight
|
|
|
|
spec:heightMeasureSpec
|
|
|
|
childMeasuredState:childState
|
|
|
|
<< DORIC_MEASURED_HEIGHT_STATE_SHIFT];
|
|
|
|
self.measuredWidth = widthSizeAndState.size;
|
|
|
|
self.measuredHeight = heightSizeAndState.size;
|
|
|
|
|
|
|
|
self.measuredState = (widthSizeAndState.state
|
|
|
|
<< DORIC_MEASURED_HEIGHT_STATE_SHIFT) | heightSizeAndState.state;
|
2022-08-24 14:19:10 +08:00
|
|
|
|
|
|
|
if (matchParentChildren.count > 1) {
|
|
|
|
for (DoricLayout *child in matchParentChildren) {
|
|
|
|
DoricMeasureSpec childWidthMeasureSpec;
|
|
|
|
if (child.widthSpec == DoricLayoutMost) {
|
2022-08-24 16:09:31 +08:00
|
|
|
childWidthMeasureSpec.size = MAX(0,
|
|
|
|
self.measuredWidth - self.paddingLeft - self.paddingRight
|
|
|
|
- child.marginLeft - child.marginRight);
|
2022-08-24 14:19:10 +08:00
|
|
|
childWidthMeasureSpec.mode = DoricMeasureExactly;
|
|
|
|
} else {
|
2022-08-24 16:09:31 +08:00
|
|
|
childWidthMeasureSpec
|
|
|
|
= [self getChildMeasureSpec:widthMeasureSpec
|
|
|
|
padding:self.paddingLeft + self.paddingRight
|
|
|
|
+ child.marginLeft + child.marginRight
|
|
|
|
childLayoutSpec:child.widthSpec
|
|
|
|
childSize:child.width];
|
2022-08-19 14:52:43 +08:00
|
|
|
}
|
2022-08-24 14:19:10 +08:00
|
|
|
DoricMeasureSpec childHeightMeasureSpec;
|
|
|
|
if (child.heightSpec == DoricLayoutMost) {
|
2022-08-24 16:09:31 +08:00
|
|
|
childHeightMeasureSpec.size
|
|
|
|
= MAX(0, self.measuredHeight - self.paddingTop - self.paddingBottom
|
2022-08-24 14:19:10 +08:00
|
|
|
- child.marginTop - child.marginBottom);
|
|
|
|
childHeightMeasureSpec.mode = DoricMeasureExactly;
|
|
|
|
} else {
|
2022-08-24 16:09:31 +08:00
|
|
|
childHeightMeasureSpec
|
|
|
|
= [self getChildMeasureSpec:heightMeasureSpec
|
|
|
|
padding:self.paddingTop + self.paddingBottom
|
|
|
|
+ child.marginTop + child.marginBottom
|
|
|
|
childLayoutSpec:child.heightSpec
|
|
|
|
childSize:child.height];
|
2022-08-23 18:15:38 +08:00
|
|
|
}
|
2022-08-24 14:19:10 +08:00
|
|
|
[child measureWidth:childWidthMeasureSpec height:childHeightMeasureSpec];
|
2022-08-23 18:15:38 +08:00
|
|
|
}
|
|
|
|
}
|
2022-08-24 14:19:10 +08:00
|
|
|
}
|
|
|
|
|
2022-08-24 16:09:31 +08:00
|
|
|
- (void)undefinedMeasureWidth:(DoricMeasureSpec)widthMeasureSpec
|
|
|
|
height:(DoricMeasureSpec)heightMeasureSpec {
|
2022-08-24 14:19:10 +08:00
|
|
|
CGSize targetSize = CGSizeMake(widthMeasureSpec.size, heightMeasureSpec.size);
|
|
|
|
//TODO should check this
|
|
|
|
CGSize measuredSize = [self.view sizeThatFits:targetSize];
|
2022-08-24 16:09:31 +08:00
|
|
|
DoricSizeAndState widthSizeAndState = [self resolveSizeAndState:measuredSize.width
|
|
|
|
spec:widthMeasureSpec
|
|
|
|
childMeasuredState:0];
|
|
|
|
DoricSizeAndState heightSizeAndState = [self resolveSizeAndState:measuredSize.height
|
|
|
|
spec:heightMeasureSpec
|
|
|
|
childMeasuredState:0];
|
|
|
|
self.measuredWidth = widthSizeAndState.size;
|
|
|
|
self.measuredHeight = heightSizeAndState.size;
|
|
|
|
self.measuredState = (widthSizeAndState.state
|
|
|
|
<< DORIC_MEASURED_HEIGHT_STATE_SHIFT) | heightSizeAndState.state;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
2022-08-17 17:54:10 +08:00
|
|
|
#pragma layout
|
|
|
|
|
|
|
|
- (void)layout {
|
|
|
|
switch (self.layoutType) {
|
|
|
|
case DoricStack: {
|
|
|
|
[self layoutStack];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DoricVLayout: {
|
|
|
|
[self layoutVLayout];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DoricHLayout: {
|
|
|
|
[self layoutHLayout];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma setFrame
|
|
|
|
|
|
|
|
- (void)setFrame {
|
|
|
|
if (self.layoutType != DoricUndefined) {
|
|
|
|
[self.view.subviews forEach:^(__kindof UIView *obj) {
|
|
|
|
[obj.doricLayout setFrame];
|
|
|
|
}];
|
|
|
|
}
|
2022-08-24 16:09:31 +08:00
|
|
|
CGRect originFrame = CGRectMake(self.measuredX, self.measuredY,
|
|
|
|
self.measuredWidth, self.measuredHeight);
|
2022-08-17 17:54:10 +08:00
|
|
|
if (!CGAffineTransformEqualToTransform(self.view.transform, CGAffineTransformIdentity)) {
|
|
|
|
CGPoint anchor = self.view.layer.anchorPoint;
|
2022-08-24 16:09:31 +08:00
|
|
|
originFrame = CGRectOffset(originFrame,
|
|
|
|
-anchor.x * self.measuredWidth - self.measuredX,
|
|
|
|
-anchor.y * self.measuredHeight - self.measuredY);
|
2022-08-17 17:54:10 +08:00
|
|
|
originFrame = CGRectApplyAffineTransform(originFrame, self.view.transform);
|
2022-08-24 16:09:31 +08:00
|
|
|
originFrame = CGRectOffset(originFrame,
|
|
|
|
anchor.x * self.measuredWidth + self.measuredX,
|
|
|
|
anchor.y * self.measuredHeight + self.measuredY);
|
2022-08-17 17:54:10 +08:00
|
|
|
}
|
|
|
|
BOOL isFrameChange = ![self rect:originFrame equalTo:self.view.frame];
|
|
|
|
if (isFrameChange) {
|
|
|
|
if (isnan(originFrame.origin.x) || isinf(originFrame.origin.x)
|
|
|
|
|| isnan(originFrame.origin.y) || isinf(originFrame.origin.y)
|
|
|
|
|| isnan(originFrame.size.width) || isinf(originFrame.size.width)
|
|
|
|
|| isnan(originFrame.size.height) || isinf(originFrame.size.height)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.view.frame = originFrame;
|
|
|
|
}
|
|
|
|
if (!UIEdgeInsetsEqualToEdgeInsets(self.corners, UIEdgeInsetsZero)) {
|
|
|
|
if (self.view.layer.mask) {
|
|
|
|
if ([self.view.layer.mask isKindOfClass:[DoricShapeLayer class]]) {
|
|
|
|
DoricShapeLayer *shapeLayer = (DoricShapeLayer *) self.view.layer.mask;
|
|
|
|
if (!UIEdgeInsetsEqualToEdgeInsets(self.corners, shapeLayer.corners)
|
|
|
|
|| !CGRectEqualToRect(self.view.bounds, shapeLayer.viewBounds)) {
|
|
|
|
shapeLayer.corners = self.corners;
|
|
|
|
shapeLayer.viewBounds = self.view.bounds;
|
|
|
|
[self configMaskWithLayer:shapeLayer];
|
|
|
|
}
|
|
|
|
} else if (isFrameChange) {
|
|
|
|
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
|
|
|
|
[self configMaskWithLayer:shapeLayer];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DoricShapeLayer *shapeLayer = [DoricShapeLayer layer];
|
|
|
|
shapeLayer.corners = self.corners;
|
|
|
|
shapeLayer.viewBounds = self.view.bounds;
|
|
|
|
[self configMaskWithLayer:shapeLayer];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)configMaskWithLayer:(CAShapeLayer *)shapeLayer {
|
|
|
|
CGPathRef path = DoricCreateRoundedRectPath(self.view.bounds,
|
|
|
|
self.corners.top, self.corners.left, self.corners.bottom, self.corners.right);
|
|
|
|
shapeLayer.path = path;
|
|
|
|
if ((self.corners.left != self.corners.right
|
|
|
|
|| self.corners.left != self.corners.top
|
|
|
|
|| self.corners.left != self.corners.bottom)
|
|
|
|
&& self.view.layer.borderWidth > CGFLOAT_MIN) {
|
|
|
|
CAShapeLayer *lineLayer = [CAShapeLayer layer];
|
|
|
|
lineLayer.lineWidth = self.view.layer.borderWidth * 2;
|
|
|
|
lineLayer.strokeColor = self.view.layer.borderColor;
|
|
|
|
lineLayer.path = path;
|
|
|
|
lineLayer.fillColor = nil;
|
|
|
|
[[self.view.layer sublayers] forEach:^(__kindof CALayer *obj) {
|
2022-08-24 16:09:31 +08:00
|
|
|
if ([obj isKindOfClass:CAShapeLayer.class]
|
|
|
|
&& ((CAShapeLayer *) obj).lineWidth > CGFLOAT_MIN) {
|
2022-08-17 17:54:10 +08:00
|
|
|
[obj removeFromSuperlayer];
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
[self.view.layer addSublayer:lineLayer];
|
|
|
|
}
|
|
|
|
CGPathRelease(path);
|
|
|
|
self.view.layer.mask = shapeLayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
- (void)layoutStack {
|
|
|
|
for (__kindof UIView *subview in self.view.subviews) {
|
|
|
|
DoricLayout *layout = subview.doricLayout;
|
|
|
|
if (layout.disabled) {
|
2019-12-04 13:29:26 +08:00
|
|
|
continue;
|
|
|
|
}
|
2020-04-03 16:36:43 +08:00
|
|
|
[layout layout];
|
|
|
|
DoricGravity gravity = layout.alignment;
|
2020-03-27 10:07:53 +08:00
|
|
|
if ((gravity & DoricGravityLeft) == DoricGravityLeft) {
|
2020-04-03 16:36:43 +08:00
|
|
|
layout.measuredX = self.paddingLeft;
|
2020-03-27 10:07:53 +08:00
|
|
|
} else if ((gravity & DoricGravityRight) == DoricGravityRight) {
|
2020-04-03 16:36:43 +08:00
|
|
|
layout.measuredX = self.measuredWidth - self.paddingRight - layout.measuredWidth;
|
2020-04-03 10:19:13 +08:00
|
|
|
} else if ((gravity & DoricGravityCenterX) == DoricGravityCenterX) {
|
2020-04-03 16:36:43 +08:00
|
|
|
layout.measuredX = self.measuredWidth / 2 - layout.measuredWidth / 2;
|
2019-12-04 13:29:26 +08:00
|
|
|
} else {
|
2021-05-07 18:47:33 +08:00
|
|
|
layout.measuredX = self.paddingLeft;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
2020-04-03 16:36:43 +08:00
|
|
|
if ((gravity & DoricGravityTop) == DoricGravityTop) {
|
|
|
|
layout.measuredY = self.paddingTop;
|
2020-04-03 10:19:13 +08:00
|
|
|
} else if ((gravity & DoricGravityBottom) == DoricGravityBottom) {
|
2020-04-03 16:36:43 +08:00
|
|
|
layout.measuredY = self.measuredHeight - self.paddingBottom - layout.measuredHeight;
|
2020-04-03 10:19:13 +08:00
|
|
|
} else if ((gravity & DoricGravityCenterY) == DoricGravityCenterY) {
|
2020-04-03 16:36:43 +08:00
|
|
|
layout.measuredY = self.measuredHeight / 2 - layout.measuredHeight / 2;
|
2019-12-04 13:29:26 +08:00
|
|
|
} else {
|
2021-05-07 18:47:33 +08:00
|
|
|
layout.measuredY = self.paddingTop;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
2020-03-21 16:03:03 +08:00
|
|
|
|
|
|
|
if (!gravity) {
|
2020-04-03 16:36:43 +08:00
|
|
|
gravity = DoricGravityLeft | DoricGravityTop;
|
2020-03-21 16:03:03 +08:00
|
|
|
}
|
2020-04-03 16:36:43 +08:00
|
|
|
if (layout.marginLeft && !((gravity & DoricGravityRight) == DoricGravityRight)) {
|
|
|
|
layout.measuredX += layout.marginLeft;
|
2020-03-21 16:03:03 +08:00
|
|
|
}
|
2020-04-03 16:36:43 +08:00
|
|
|
if (layout.marginRight && !((gravity & DoricGravityLeft) == DoricGravityLeft)) {
|
|
|
|
layout.measuredX -= layout.marginRight;
|
2020-03-21 16:03:03 +08:00
|
|
|
}
|
2020-04-03 16:36:43 +08:00
|
|
|
if (layout.marginTop && !((gravity & DoricGravityBottom) == DoricGravityBottom)) {
|
|
|
|
layout.measuredY += layout.marginTop;
|
2020-03-21 17:03:52 +08:00
|
|
|
}
|
2020-04-03 16:36:43 +08:00
|
|
|
if (layout.marginBottom && !((gravity & DoricGravityTop) == DoricGravityTop)) {
|
|
|
|
layout.measuredY -= layout.marginBottom;
|
2020-03-21 16:03:03 +08:00
|
|
|
}
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
- (void)layoutVLayout {
|
|
|
|
CGFloat yStart = self.paddingTop;
|
|
|
|
if ((self.gravity & DoricGravityTop) == DoricGravityTop) {
|
|
|
|
yStart = self.paddingTop;
|
2020-04-03 10:19:13 +08:00
|
|
|
} else if ((self.gravity & DoricGravityBottom) == DoricGravityBottom) {
|
2022-08-24 14:19:10 +08:00
|
|
|
yStart = self.measuredHeight - self.totalLength - self.paddingBottom;
|
2020-04-03 10:19:13 +08:00
|
|
|
} else if ((self.gravity & DoricGravityCenterY) == DoricGravityCenterY) {
|
2022-08-24 16:09:31 +08:00
|
|
|
yStart = (self.measuredHeight - self.totalLength
|
|
|
|
- self.paddingTop - self.paddingBottom) / 2 + self.paddingTop;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
2020-04-03 16:36:43 +08:00
|
|
|
for (UIView *child in self.view.subviews) {
|
|
|
|
DoricLayout *layout = child.doricLayout;
|
|
|
|
if (layout.disabled) {
|
2019-12-04 13:29:26 +08:00
|
|
|
continue;
|
|
|
|
}
|
2020-04-03 16:36:43 +08:00
|
|
|
[layout layout];
|
|
|
|
DoricGravity gravity = layout.alignment | self.gravity;
|
2020-03-27 10:07:53 +08:00
|
|
|
if ((gravity & DoricGravityLeft) == DoricGravityLeft) {
|
2020-04-03 16:36:43 +08:00
|
|
|
layout.measuredX = self.paddingLeft;
|
2020-03-27 10:07:53 +08:00
|
|
|
} else if ((gravity & DoricGravityRight) == DoricGravityRight) {
|
2020-04-03 16:36:43 +08:00
|
|
|
layout.measuredX = self.measuredWidth - self.paddingRight - layout.measuredWidth;
|
2020-04-03 10:19:13 +08:00
|
|
|
} else if ((gravity & DoricGravityCenterX) == DoricGravityCenterX) {
|
2020-04-03 16:36:43 +08:00
|
|
|
layout.measuredX = self.measuredWidth / 2 - layout.measuredWidth / 2;
|
2019-12-04 13:29:26 +08:00
|
|
|
} else {
|
2020-04-03 16:36:43 +08:00
|
|
|
layout.measuredX = self.paddingLeft;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
2020-03-21 16:03:03 +08:00
|
|
|
if (!gravity) {
|
2020-03-27 10:07:53 +08:00
|
|
|
gravity = DoricGravityLeft;
|
2020-03-21 16:03:03 +08:00
|
|
|
}
|
2020-04-03 16:36:43 +08:00
|
|
|
if (layout.marginLeft && !((gravity & DoricGravityRight) == DoricGravityRight)) {
|
|
|
|
layout.measuredX += layout.marginLeft;
|
2020-03-21 17:03:52 +08:00
|
|
|
}
|
2020-04-03 16:36:43 +08:00
|
|
|
if (layout.marginRight && !((gravity & DoricGravityLeft) == DoricGravityLeft)) {
|
|
|
|
layout.measuredX -= layout.marginRight;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
2020-04-03 16:36:43 +08:00
|
|
|
layout.measuredY = yStart + layout.marginTop;
|
|
|
|
yStart += self.spacing + layout.takenHeight;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
- (void)layoutHLayout {
|
|
|
|
CGFloat xStart = self.paddingLeft;
|
2020-03-27 10:07:53 +08:00
|
|
|
if ((self.gravity & DoricGravityLeft) == DoricGravityLeft) {
|
2020-04-03 16:36:43 +08:00
|
|
|
xStart = self.paddingLeft;
|
2020-03-27 10:07:53 +08:00
|
|
|
} else if ((self.gravity & DoricGravityRight) == DoricGravityRight) {
|
2022-08-24 14:19:10 +08:00
|
|
|
xStart = self.measuredWidth - self.totalLength - self.paddingRight;
|
2020-04-03 10:19:13 +08:00
|
|
|
} else if ((self.gravity & DoricGravityCenterX) == DoricGravityCenterX) {
|
2022-08-24 16:09:31 +08:00
|
|
|
xStart = (self.measuredWidth - self.totalLength
|
|
|
|
- self.paddingLeft - self.paddingRight) / 2 + self.paddingLeft;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
2020-04-03 16:36:43 +08:00
|
|
|
for (UIView *child in self.view.subviews) {
|
|
|
|
DoricLayout *layout = child.doricLayout;
|
|
|
|
if (layout.disabled) {
|
2019-12-04 13:29:26 +08:00
|
|
|
continue;
|
|
|
|
}
|
2021-04-07 17:24:33 +08:00
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
[layout layout];
|
2019-12-14 15:04:34 +08:00
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
DoricGravity gravity = layout.alignment | self.gravity;
|
|
|
|
if ((gravity & DoricGravityTop) == DoricGravityTop) {
|
|
|
|
layout.measuredY = self.paddingTop;
|
2020-04-03 10:19:13 +08:00
|
|
|
} else if ((gravity & DoricGravityBottom) == DoricGravityBottom) {
|
2020-04-03 18:15:43 +08:00
|
|
|
layout.measuredY = self.measuredHeight - self.paddingBottom - layout.measuredHeight;
|
2020-04-03 10:19:13 +08:00
|
|
|
} else if ((gravity & DoricGravityCenterY) == DoricGravityCenterY) {
|
2020-04-03 16:36:43 +08:00
|
|
|
layout.measuredY = self.measuredHeight / 2 - layout.measuredHeight / 2;
|
2019-12-04 13:29:26 +08:00
|
|
|
} else {
|
2020-04-03 16:36:43 +08:00
|
|
|
layout.measuredY = self.paddingTop;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
2020-03-21 16:03:03 +08:00
|
|
|
if (!gravity) {
|
2020-04-03 16:36:43 +08:00
|
|
|
gravity = DoricGravityTop;
|
2020-03-21 16:03:03 +08:00
|
|
|
}
|
2020-04-03 16:36:43 +08:00
|
|
|
if (layout.marginTop && !((gravity & DoricGravityBottom) == DoricGravityBottom)) {
|
|
|
|
layout.measuredY += layout.marginTop;
|
2020-03-21 16:03:03 +08:00
|
|
|
}
|
2020-04-03 16:36:43 +08:00
|
|
|
if (layout.marginBottom && !((gravity & DoricGravityTop) == DoricGravityTop)) {
|
|
|
|
layout.measuredY -= layout.marginBottom;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
2020-04-03 16:36:43 +08:00
|
|
|
layout.measuredX = xStart + layout.marginLeft;
|
|
|
|
xStart += self.spacing + layout.takenWidth;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
}
|
2020-04-03 16:36:43 +08:00
|
|
|
|
2019-12-04 13:29:26 +08:00
|
|
|
@end
|
2021-11-17 10:23:21 +08:00
|
|
|
|