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"
|
2019-12-04 13:29:26 +08:00
|
|
|
|
|
|
|
static const void *kLayoutConfig = &kLayoutConfig;
|
|
|
|
|
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
|
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
@interface DoricLayout ()
|
|
|
|
@property(nonatomic, assign) CGFloat contentWidth;
|
|
|
|
@property(nonatomic, assign) CGFloat contentHeight;
|
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;
|
|
|
|
_minWidth = CGFLOAT_MIN;
|
|
|
|
_minHeight = CGFLOAT_MIN;
|
2020-04-03 16:36:43 +08:00
|
|
|
}
|
|
|
|
return self;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
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 {
|
2020-04-13 11:57:31 +08:00
|
|
|
[self measureSelf:targetSize];
|
|
|
|
[self layout];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)measureSelf:(CGSize)targetSize {
|
2020-04-08 15:20:38 +08:00
|
|
|
CGFloat width;
|
|
|
|
CGFloat height;
|
2020-04-03 16:36:43 +08:00
|
|
|
if (self.widthSpec == DoricLayoutMost) {
|
2020-04-10 16:14:27 +08:00
|
|
|
if (self.view.superview.doricLayout.layoutType == DoricHLayout && self.weight > 0) {
|
|
|
|
width = self.measuredWidth = 0;
|
|
|
|
} else {
|
|
|
|
width = self.measuredWidth = targetSize.width;
|
|
|
|
}
|
2020-04-08 15:20:38 +08:00
|
|
|
} else if (self.widthSpec == DoricLayoutJust) {
|
|
|
|
width = self.measuredWidth = self.width;
|
2020-04-03 16:36:43 +08:00
|
|
|
} else {
|
2020-04-08 15:20:38 +08:00
|
|
|
width = targetSize.width;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
2020-04-03 16:36:43 +08:00
|
|
|
if (self.heightSpec == DoricLayoutMost) {
|
2020-04-10 16:14:27 +08:00
|
|
|
if (self.view.superview.doricLayout.layoutType == DoricVLayout && self.weight > 0) {
|
2020-04-21 11:11:35 +08:00
|
|
|
height = self.measuredHeight = 0;
|
2020-04-10 16:14:27 +08:00
|
|
|
} else {
|
|
|
|
height = self.measuredHeight = targetSize.height;
|
|
|
|
}
|
2020-04-08 15:20:38 +08:00
|
|
|
} else if (self.heightSpec == DoricLayoutJust) {
|
|
|
|
height = self.measuredHeight = self.height;
|
2020-04-03 17:17:40 +08:00
|
|
|
} else {
|
2020-04-08 15:20:38 +08:00
|
|
|
height = targetSize.height;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
2020-04-03 16:36:43 +08:00
|
|
|
[self measureContent:CGSizeMake(
|
2020-04-08 15:20:38 +08:00
|
|
|
width - self.paddingLeft - self.paddingRight,
|
|
|
|
height - self.paddingTop - self.paddingBottom)];
|
2020-04-03 17:28:55 +08:00
|
|
|
|
2020-04-11 12:20:43 +08:00
|
|
|
if ([self restrainSize]) {
|
|
|
|
[self measureContent:CGSizeMake(
|
|
|
|
self.measuredWidth - self.paddingLeft - self.paddingRight,
|
|
|
|
self.measuredHeight - self.paddingTop - self.paddingBottom)];
|
|
|
|
}
|
|
|
|
[self restrainSize];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)restrainSize {
|
2020-04-03 17:28:55 +08:00
|
|
|
BOOL needRemeasure = NO;
|
2020-04-11 09:38:27 +08:00
|
|
|
if (self.measuredWidth > self.maxWidth) {
|
|
|
|
self.measuredWidth = self.maxWidth;
|
|
|
|
needRemeasure = YES;
|
2020-04-03 17:28:55 +08:00
|
|
|
}
|
2020-04-11 09:38:27 +08:00
|
|
|
if (self.measuredHeight > self.maxHeight) {
|
|
|
|
self.measuredHeight = self.maxHeight;
|
|
|
|
needRemeasure = YES;
|
|
|
|
}
|
|
|
|
if (self.measuredWidth < self.minWidth) {
|
|
|
|
self.measuredWidth = self.minWidth;
|
|
|
|
needRemeasure = YES;
|
|
|
|
}
|
|
|
|
if (self.measuredHeight < self.minHeight) {
|
|
|
|
self.measuredHeight = self.minHeight;
|
|
|
|
needRemeasure = YES;
|
2020-04-03 17:28:55 +08:00
|
|
|
}
|
2020-04-11 12:20:43 +08:00
|
|
|
return needRemeasure;
|
2020-04-03 16:36:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)measureContent:(CGSize)targetSize {
|
|
|
|
switch (self.layoutType) {
|
|
|
|
case DoricStack: {
|
|
|
|
[self measureStackContent:targetSize];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DoricVLayout: {
|
|
|
|
[self measureVLayoutContent:targetSize];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DoricHLayout: {
|
|
|
|
[self measureHLayoutContent:targetSize];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
[self measureUndefinedContent:targetSize];
|
|
|
|
break;
|
2020-03-20 15:27:09 +08:00
|
|
|
}
|
|
|
|
}
|
2020-04-26 09:27:17 +08:00
|
|
|
if (self.layoutType != DoricUndefined && self.view.superview.doricLayout.widthSpec == DoricLayoutFit && self.widthSpec == DoricLayoutMost) {
|
2020-04-23 19:54:25 +08:00
|
|
|
self.measuredWidth = 0;
|
|
|
|
}
|
2020-04-26 09:27:17 +08:00
|
|
|
if (self.layoutType != DoricUndefined && self.view.superview.doricLayout.heightSpec == DoricLayoutFit && self.heightSpec == DoricLayoutMost) {
|
2020-04-23 19:54:25 +08:00
|
|
|
self.measuredHeight = 0;
|
|
|
|
}
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
- (void)layout {
|
|
|
|
switch (self.layoutType) {
|
|
|
|
case DoricStack: {
|
|
|
|
[self layoutStack];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DoricVLayout: {
|
|
|
|
[self layoutVLayout];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DoricHLayout: {
|
|
|
|
[self layoutHLayout];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
break;
|
|
|
|
}
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 19:30:08 +08:00
|
|
|
- (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;
|
|
|
|
}
|
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
- (void)setFrame {
|
2020-04-08 11:34:36 +08:00
|
|
|
if (self.layoutType != DoricUndefined) {
|
|
|
|
[self.view.subviews forEach:^(__kindof UIView *obj) {
|
|
|
|
[obj.doricLayout setFrame];
|
|
|
|
}];
|
|
|
|
}
|
2020-04-08 19:30:08 +08:00
|
|
|
CGRect originFrame = CGRectMake(self.measuredX, self.measuredY, self.measuredWidth, self.measuredHeight);
|
|
|
|
if (!CGAffineTransformEqualToTransform(self.view.transform, CGAffineTransformIdentity)) {
|
|
|
|
CGPoint anchor = self.view.layer.anchorPoint;
|
|
|
|
originFrame = CGRectOffset(originFrame, -anchor.x * self.measuredWidth - self.measuredX, -anchor.y * self.measuredHeight - self.measuredY);
|
|
|
|
originFrame = CGRectApplyAffineTransform(originFrame, self.view.transform);
|
|
|
|
originFrame = CGRectOffset(originFrame, anchor.x * self.measuredWidth + self.measuredX, anchor.y * self.measuredHeight + self.measuredY);
|
2020-04-08 15:20:38 +08:00
|
|
|
}
|
2020-04-08 19:30:08 +08:00
|
|
|
if (![self rect:originFrame equalTo:self.view.frame]) {
|
|
|
|
self.view.frame = originFrame;
|
2020-04-08 15:20:38 +08:00
|
|
|
}
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
- (void)measureUndefinedContent:(CGSize)targetSize {
|
|
|
|
CGSize measuredSize = [self.view sizeThatFits:targetSize];
|
|
|
|
if (self.widthSpec == DoricLayoutFit) {
|
2020-04-03 17:58:47 +08:00
|
|
|
if ([self.view isKindOfClass:[UIImageView class]]
|
|
|
|
&& self.heightSpec != DoricLayoutFit && measuredSize.height > 0) {
|
|
|
|
self.measuredWidth = measuredSize.width / measuredSize.height * self.measuredHeight
|
|
|
|
+ self.paddingLeft + self.paddingRight;
|
|
|
|
} else {
|
|
|
|
self.measuredWidth = measuredSize.width + self.paddingLeft + self.paddingRight;
|
|
|
|
}
|
2020-04-03 16:36:43 +08:00
|
|
|
}
|
|
|
|
if (self.heightSpec == DoricLayoutFit) {
|
2020-04-03 17:58:47 +08:00
|
|
|
if ([self.view isKindOfClass:[UIImageView class]]
|
|
|
|
&& self.widthSpec != DoricLayoutFit && measuredSize.width > 0) {
|
|
|
|
self.measuredHeight = measuredSize.height / measuredSize.width * self.measuredWidth
|
|
|
|
+ self.paddingTop + self.paddingBottom;
|
|
|
|
} else {
|
|
|
|
self.measuredHeight = measuredSize.height + self.paddingTop + self.paddingBottom;
|
|
|
|
}
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
- (CGFloat)takenWidth {
|
|
|
|
return self.measuredWidth + self.marginLeft + self.marginRight;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
- (CGFloat)takenHeight {
|
|
|
|
return self.measuredHeight + self.marginTop + self.marginBottom;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
2020-04-08 16:07:15 +08:00
|
|
|
- (CGSize)removeMargin:(CGSize)targetSize {
|
|
|
|
return CGSizeMake(
|
|
|
|
targetSize.width - self.marginLeft - self.marginRight,
|
|
|
|
targetSize.height - self.marginTop - self.marginBottom);
|
|
|
|
}
|
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
- (void)measureStackContent:(CGSize)targetSize {
|
|
|
|
CGFloat contentWidth = 0, contentHeight = 0;
|
|
|
|
for (__kindof UIView *subview in self.view.subviews) {
|
|
|
|
DoricLayout *layout = subview.doricLayout;
|
|
|
|
if (layout.disabled) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-08 16:07:15 +08:00
|
|
|
[layout measure:[layout removeMargin:targetSize]];
|
2020-04-03 16:36:43 +08:00
|
|
|
contentWidth = MAX(contentWidth, layout.takenWidth);
|
|
|
|
contentHeight = MAX(contentHeight, layout.takenHeight);
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
2020-04-03 16:36:43 +08:00
|
|
|
if (self.widthSpec == DoricLayoutFit) {
|
|
|
|
self.measuredWidth = contentWidth + self.paddingLeft + self.paddingRight;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
if (self.heightSpec == DoricLayoutFit) {
|
|
|
|
self.measuredHeight = contentHeight + self.paddingTop + self.paddingBottom;
|
|
|
|
}
|
2019-12-04 13:29:26 +08:00
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
self.contentWidth = contentWidth;
|
2019-12-04 13:29:26 +08:00
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
self.contentHeight = contentHeight;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
- (void)measureVLayoutContent:(CGSize)targetSize {
|
2020-04-03 17:17:40 +08:00
|
|
|
CGFloat contentWidth = 0, contentHeight = 0, contentWeight = 0;
|
2020-04-03 16:36:43 +08:00
|
|
|
BOOL had = NO;
|
|
|
|
for (__kindof UIView *subview in self.view.subviews) {
|
|
|
|
DoricLayout *layout = subview.doricLayout;
|
|
|
|
if (layout.disabled) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
had = YES;
|
2020-04-08 16:07:15 +08:00
|
|
|
[layout measure:[layout removeMargin:CGSizeMake(targetSize.width, targetSize.height - contentHeight)]];
|
2020-04-03 16:36:43 +08:00
|
|
|
contentWidth = MAX(contentWidth, layout.takenWidth);
|
|
|
|
contentHeight += layout.takenHeight + self.spacing;
|
2020-04-03 17:17:40 +08:00
|
|
|
contentWeight += layout.weight;
|
2020-04-03 16:36:43 +08:00
|
|
|
}
|
2019-12-04 13:29:26 +08:00
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
if (had) {
|
|
|
|
contentHeight -= self.spacing;
|
|
|
|
}
|
2019-12-04 13:29:26 +08:00
|
|
|
|
2020-04-03 17:17:40 +08:00
|
|
|
if (contentWeight > 0) {
|
|
|
|
CGFloat remaining = targetSize.height - contentHeight;
|
|
|
|
contentWidth = 0;
|
|
|
|
for (__kindof UIView *subview in self.view.subviews) {
|
|
|
|
DoricLayout *layout = subview.doricLayout;
|
|
|
|
if (layout.disabled) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-09 10:54:38 +08:00
|
|
|
CGFloat measuredHeight = layout.measuredHeight + remaining / contentWeight * layout.weight;
|
|
|
|
layout.measuredHeight = measuredHeight;
|
2020-04-03 17:17:40 +08:00
|
|
|
//Need Remeasure
|
|
|
|
[layout measureContent:CGSizeMake(
|
|
|
|
layout.measuredWidth - layout.paddingLeft - layout.paddingRight,
|
2020-04-09 10:54:38 +08:00
|
|
|
measuredHeight - layout.paddingTop - layout.paddingBottom)];
|
|
|
|
layout.measuredHeight = measuredHeight;
|
2020-04-03 17:17:40 +08:00
|
|
|
contentWidth = MAX(contentWidth, layout.takenWidth);
|
|
|
|
}
|
|
|
|
contentHeight = targetSize.height;
|
|
|
|
}
|
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
if (self.widthSpec == DoricLayoutFit) {
|
|
|
|
self.measuredWidth = contentWidth + self.paddingLeft + self.paddingRight;
|
|
|
|
}
|
2019-12-04 13:29:26 +08:00
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
if (self.heightSpec == DoricLayoutFit) {
|
|
|
|
self.measuredHeight = contentHeight + self.paddingTop + self.paddingBottom;
|
|
|
|
}
|
2019-12-04 13:29:26 +08:00
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
self.contentWidth = contentWidth;
|
|
|
|
|
|
|
|
self.contentHeight = contentHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)measureHLayoutContent:(CGSize)targetSize {
|
2020-04-03 17:17:40 +08:00
|
|
|
CGFloat contentWidth = 0, contentHeight = 0, contentWeight = 0;;
|
2020-04-03 16:36:43 +08:00
|
|
|
BOOL had = NO;
|
|
|
|
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
|
|
|
had = YES;
|
2020-04-08 16:07:15 +08:00
|
|
|
[layout measure:[layout removeMargin:CGSizeMake(targetSize.width - contentWidth, targetSize.height)]];
|
2020-04-03 16:36:43 +08:00
|
|
|
contentWidth += layout.takenWidth + self.spacing;
|
|
|
|
contentHeight = MAX(contentHeight, layout.takenHeight);
|
2020-04-03 17:17:40 +08:00
|
|
|
contentWeight += layout.weight;
|
2020-04-03 16:36:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (had) {
|
|
|
|
contentWidth -= self.spacing;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
2020-04-03 17:17:40 +08:00
|
|
|
|
|
|
|
if (contentWeight > 0) {
|
|
|
|
CGFloat remaining = targetSize.width - contentWidth;
|
|
|
|
contentHeight = 0;
|
|
|
|
for (__kindof UIView *subview in self.view.subviews) {
|
|
|
|
DoricLayout *layout = subview.doricLayout;
|
|
|
|
if (layout.disabled) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-09 10:54:38 +08:00
|
|
|
CGFloat measuredWidth = layout.measuredWidth + remaining / contentWeight * layout.weight;
|
|
|
|
layout.measuredWidth = measuredWidth;
|
2020-04-03 17:17:40 +08:00
|
|
|
//Need Remeasure
|
|
|
|
[layout measureContent:CGSizeMake(
|
2020-04-09 10:54:38 +08:00
|
|
|
measuredWidth - layout.paddingLeft - layout.paddingRight,
|
2020-04-03 17:17:40 +08:00
|
|
|
layout.measuredHeight - layout.paddingTop - layout.paddingBottom)];
|
2020-04-09 10:54:38 +08:00
|
|
|
layout.measuredWidth = measuredWidth;
|
2020-04-03 17:17:40 +08:00
|
|
|
contentHeight = MAX(contentHeight, layout.takenHeight);
|
|
|
|
}
|
|
|
|
contentWidth = targetSize.width;
|
|
|
|
}
|
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
if (self.widthSpec == DoricLayoutFit) {
|
|
|
|
self.measuredWidth = contentWidth + self.paddingLeft + self.paddingRight;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self.heightSpec == DoricLayoutFit) {
|
|
|
|
self.measuredHeight = contentHeight + self.paddingTop + self.paddingBottom;
|
|
|
|
}
|
|
|
|
|
2019-12-04 13:29:26 +08:00
|
|
|
self.contentWidth = contentWidth;
|
2020-04-03 16:36:43 +08:00
|
|
|
|
2019-12-04 13:29:26 +08:00
|
|
|
self.contentHeight = contentHeight;
|
|
|
|
}
|
|
|
|
|
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-23 19:54:25 +08:00
|
|
|
if (self.widthSpec == DoricLayoutFit && layout.widthSpec == DoricLayoutMost) {
|
|
|
|
layout.measuredWidth = self.measuredWidth - layout.marginLeft - layout.marginRight;
|
|
|
|
}
|
|
|
|
if (self.heightSpec == DoricLayoutFit && layout.heightSpec == DoricLayoutMost) {
|
|
|
|
layout.measuredHeight = self.measuredHeight - layout.marginTop - layout.marginBottom;
|
|
|
|
}
|
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 {
|
2020-04-03 16:36:43 +08:00
|
|
|
if (layout.marginLeft || layout.marginRight) {
|
|
|
|
layout.measuredX = self.paddingLeft;
|
|
|
|
} else {
|
2020-04-08 15:47:10 +08:00
|
|
|
layout.measuredX = 0;
|
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 {
|
2020-04-03 16:36:43 +08:00
|
|
|
if (layout.marginTop || layout.marginBottom) {
|
|
|
|
layout.measuredY = self.paddingTop;
|
|
|
|
} else {
|
2020-04-08 15:47:10 +08:00
|
|
|
layout.measuredY = 0;
|
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) {
|
2020-04-03 16:36:43 +08:00
|
|
|
yStart = self.measuredHeight - self.contentHeight - self.paddingBottom;
|
2020-04-03 10:19:13 +08:00
|
|
|
} else if ((self.gravity & DoricGravityCenterY) == DoricGravityCenterY) {
|
2020-04-03 16:36:43 +08:00
|
|
|
yStart = (self.measuredHeight - self.contentHeight - 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-23 19:54:25 +08:00
|
|
|
if (self.widthSpec == DoricLayoutFit && layout.widthSpec == DoricLayoutMost) {
|
|
|
|
layout.measuredWidth = self.measuredWidth - layout.marginLeft - layout.marginRight;
|
|
|
|
}
|
|
|
|
if (self.heightSpec == DoricLayoutFit && layout.heightSpec == DoricLayoutMost) {
|
|
|
|
layout.measuredHeight = self.measuredHeight - yStart - layout.marginTop - layout.marginBottom;
|
|
|
|
}
|
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) {
|
2020-04-03 16:36:43 +08:00
|
|
|
xStart = self.measuredWidth - self.contentWidth - self.paddingRight;
|
2020-04-03 10:19:13 +08:00
|
|
|
} else if ((self.gravity & DoricGravityCenterX) == DoricGravityCenterX) {
|
2020-04-03 16:36:43 +08:00
|
|
|
xStart = (self.measuredWidth - self.contentWidth - 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;
|
|
|
|
}
|
2020-04-23 19:54:25 +08:00
|
|
|
|
|
|
|
if (self.widthSpec == DoricLayoutFit && layout.widthSpec == DoricLayoutMost) {
|
|
|
|
layout.measuredWidth = self.measuredWidth - xStart - layout.marginLeft - layout.marginRight;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self.heightSpec == DoricLayoutFit && layout.heightSpec == DoricLayoutMost) {
|
|
|
|
layout.measuredHeight = self.measuredHeight - layout.marginTop - layout.marginBottom;
|
|
|
|
}
|
|
|
|
|
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
|