2019-10-23 19:37:06 +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.
|
|
|
|
//
|
|
|
|
|
2019-10-23 20:06:21 +08:00
|
|
|
#import "DoricLayouts.h"
|
2019-10-23 19:37:06 +08:00
|
|
|
#import <objc/runtime.h>
|
2019-11-27 15:28:39 +08:00
|
|
|
#import <Doric/DoricLayouts.h>
|
2019-10-23 19:37:06 +08:00
|
|
|
#import "UIView+Doric.h"
|
|
|
|
|
2019-11-27 14:31:52 +08:00
|
|
|
static const void *kLayoutConfig = &kLayoutConfig;
|
|
|
|
|
|
|
|
@implementation UIView (DoricLayoutConfig)
|
|
|
|
@dynamic layoutConfig;
|
|
|
|
|
|
|
|
- (void)setLayoutConfig:(DoricLayoutConfig *)layoutConfig {
|
|
|
|
objc_setAssociatedObject(self, kLayoutConfig, layoutConfig, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (DoricLayoutConfig *)layoutConfig {
|
|
|
|
return objc_getAssociatedObject(self, kLayoutConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
static const void *kTagString = &kTagString;
|
|
|
|
|
|
|
|
@implementation UIView (DoricTag)
|
|
|
|
|
|
|
|
- (void)setTagString:(NSString *)tagString {
|
|
|
|
objc_setAssociatedObject(self, kTagString, tagString, OBJC_ASSOCIATION_COPY_NONATOMIC);
|
|
|
|
self.tag = [tagString hash];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)tagString {
|
|
|
|
return objc_getAssociatedObject(self, kTagString);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (UIView *)viewWithTagString:(NSString *)tagString {
|
|
|
|
// notice the potential hash collision
|
|
|
|
return [self viewWithTag:[tagString hash]];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation UIView (DoricLayouts)
|
2019-11-27 15:28:39 +08:00
|
|
|
/**
|
|
|
|
* Measure self's size
|
|
|
|
* */
|
2019-11-27 14:31:52 +08:00
|
|
|
- (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);
|
|
|
|
}
|
|
|
|
|
2019-11-27 15:28:39 +08:00
|
|
|
/**
|
|
|
|
* layout self and subviews
|
|
|
|
* */
|
|
|
|
- (void)layoutSelf:(CGSize)targetSize {
|
|
|
|
CGSize contentSize = [self measureSize:targetSize];
|
2019-11-27 14:31:52 +08:00
|
|
|
if (self.layoutConfig.widthSpec == DoricLayoutAtMost) {
|
2019-11-27 15:28:39 +08:00
|
|
|
self.width = targetSize.width;
|
2019-11-27 14:31:52 +08:00
|
|
|
} else if (self.layoutConfig.widthSpec == DoricLayoutWrapContent) {
|
|
|
|
self.width = contentSize.width;
|
|
|
|
}
|
|
|
|
if (self.layoutConfig.heightSpec == DoricLayoutAtMost) {
|
2019-11-27 15:28:39 +08:00
|
|
|
self.height = targetSize.height;
|
2019-11-27 14:31:52 +08:00
|
|
|
} else if (self.layoutConfig.heightSpec == DoricLayoutWrapContent) {
|
|
|
|
self.height = contentSize.height;
|
|
|
|
}
|
|
|
|
}
|
2019-11-27 15:28:39 +08:00
|
|
|
|
|
|
|
- (BOOL)requestSuperview {
|
|
|
|
return self.superview
|
|
|
|
&& self.layoutConfig
|
|
|
|
&& self.layoutConfig.widthSpec != DoricLayoutExact
|
|
|
|
&& self.layoutConfig.heightSpec != DoricLayoutExact;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)doricLayoutSubviews {
|
|
|
|
if ([self requestSuperview]) {
|
|
|
|
[self.superview doricLayoutSubviews];
|
|
|
|
} else {
|
|
|
|
[self layoutSelf:CGSizeMake(self.width, self.height)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 14:31:52 +08:00
|
|
|
@end
|
|
|
|
|
2019-10-23 20:06:21 +08:00
|
|
|
DoricMargin DoricMarginMake(CGFloat left, CGFloat top, CGFloat right, CGFloat bottom) {
|
|
|
|
DoricMargin margin;
|
2019-10-23 19:37:06 +08:00
|
|
|
margin.left = left;
|
|
|
|
margin.top = top;
|
|
|
|
margin.right = right;
|
|
|
|
margin.bottom = bottom;
|
|
|
|
return margin;
|
|
|
|
}
|
|
|
|
|
2019-10-23 20:06:21 +08:00
|
|
|
@implementation DoricLayoutConfig
|
2019-10-23 19:37:06 +08:00
|
|
|
- (instancetype)init {
|
|
|
|
if (self = [super init]) {
|
2019-10-23 20:06:21 +08:00
|
|
|
_widthSpec = DoricLayoutExact;
|
|
|
|
_heightSpec = DoricLayoutExact;
|
2019-10-23 19:37:06 +08:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2019-10-23 20:06:21 +08:00
|
|
|
- (instancetype)initWithWidth:(DoricLayoutSpec)width height:(DoricLayoutSpec)height {
|
2019-10-23 19:37:06 +08:00
|
|
|
if (self = [super init]) {
|
|
|
|
_widthSpec = width;
|
|
|
|
_heightSpec = height;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2019-10-23 20:06:21 +08:00
|
|
|
- (instancetype)initWithWidth:(DoricLayoutSpec)width height:(DoricLayoutSpec)height margin:(DoricMargin)margin {
|
2019-11-15 15:38:24 +08:00
|
|
|
if (self = [super init]) {
|
|
|
|
_widthSpec = width;
|
|
|
|
_heightSpec = height;
|
2019-10-23 19:37:06 +08:00
|
|
|
_margin = margin;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
2019-11-15 15:38:24 +08:00
|
|
|
@interface DoricLayoutContainer ()
|
|
|
|
@property(nonatomic, assign) CGFloat contentWidth;
|
|
|
|
@property(nonatomic, assign) CGFloat contentHeight;
|
|
|
|
@property(nonatomic, assign) NSUInteger contentWeight;
|
2019-10-23 19:37:06 +08:00
|
|
|
@end
|
|
|
|
|
2019-10-23 20:06:21 +08:00
|
|
|
@implementation DoricLayoutContainer
|
2019-11-15 15:38:24 +08:00
|
|
|
- (void)layoutSubviews {
|
2019-11-27 15:28:39 +08:00
|
|
|
[super layoutSubviews];
|
|
|
|
[self doricLayoutSubviews];
|
2019-11-15 15:38:24 +08:00
|
|
|
}
|
2019-10-23 19:37:06 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
2019-10-23 20:06:21 +08:00
|
|
|
@interface DoricStackView ()
|
2019-10-23 19:37:06 +08:00
|
|
|
@property(nonatomic, assign) CGFloat contentWidth;
|
|
|
|
@property(nonatomic, assign) CGFloat contentHeight;
|
|
|
|
@end
|
|
|
|
|
2019-10-23 20:06:21 +08:00
|
|
|
@implementation DoricStackView
|
2019-10-23 19:37:06 +08:00
|
|
|
|
2019-11-27 15:28:39 +08:00
|
|
|
- (CGSize)sizeThatFits:(CGSize)size {
|
2019-11-15 15:38:24 +08:00
|
|
|
CGFloat contentWidth = 0;
|
|
|
|
CGFloat contentHeight = 0;
|
2019-10-23 19:37:06 +08:00
|
|
|
for (UIView *child in self.subviews) {
|
|
|
|
if (child.isHidden) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-15 15:38:24 +08:00
|
|
|
DoricLayoutConfig *childConfig = child.layoutConfig;
|
|
|
|
if (!childConfig) {
|
|
|
|
childConfig = [DoricLayoutConfig new];
|
|
|
|
}
|
2019-11-27 15:28:39 +08:00
|
|
|
CGSize childSize;
|
2019-11-27 11:55:49 +08:00
|
|
|
if (CGAffineTransformEqualToTransform(child.transform, CGAffineTransformIdentity)) {
|
2019-11-27 15:28:39 +08:00
|
|
|
childSize = [child measureSize:CGSizeMake(size.width, size.height)];
|
2019-11-27 11:55:49 +08:00
|
|
|
} else {
|
|
|
|
childSize = child.bounds.size;
|
2019-11-15 15:38:24 +08:00
|
|
|
}
|
|
|
|
contentWidth = MAX(contentWidth, childSize.width + childConfig.margin.left + childConfig.margin.right);
|
|
|
|
contentHeight = MAX(contentHeight, childSize.height + childConfig.margin.top + childConfig.margin.bottom);
|
2019-10-23 19:37:06 +08:00
|
|
|
}
|
2019-11-15 15:38:24 +08:00
|
|
|
self.contentWidth = contentWidth;
|
|
|
|
self.contentHeight = contentHeight;
|
|
|
|
return CGSizeMake(contentWidth, contentHeight);
|
2019-10-23 19:37:06 +08:00
|
|
|
}
|
|
|
|
|
2019-11-27 15:28:39 +08:00
|
|
|
- (void)layoutSelf:(CGSize)targetSize {
|
|
|
|
[super layoutSelf:targetSize];
|
2019-10-23 19:37:06 +08:00
|
|
|
for (UIView *child in self.subviews) {
|
|
|
|
if (child.isHidden) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-27 11:55:49 +08:00
|
|
|
if (!CGAffineTransformEqualToTransform(child.transform, CGAffineTransformIdentity)) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-15 15:38:24 +08:00
|
|
|
DoricLayoutConfig *childConfig = child.layoutConfig;
|
|
|
|
if (!childConfig) {
|
|
|
|
childConfig = [DoricLayoutConfig new];
|
|
|
|
}
|
2019-11-27 15:28:39 +08:00
|
|
|
CGSize size = [child measureSize:CGSizeMake(targetSize.width, targetSize.height)];
|
|
|
|
[child layoutSelf:size];
|
|
|
|
DoricGravity gravity = childConfig.alignment;
|
2019-10-23 19:37:06 +08:00
|
|
|
if ((gravity & LEFT) == LEFT) {
|
|
|
|
child.left = 0;
|
|
|
|
} else if ((gravity & RIGHT) == RIGHT) {
|
2019-11-15 15:38:24 +08:00
|
|
|
child.right = targetSize.width;
|
2019-10-23 19:37:06 +08:00
|
|
|
} else if ((gravity & CENTER_X) == CENTER_X) {
|
2019-11-15 15:38:24 +08:00
|
|
|
child.centerX = targetSize.width / 2;
|
|
|
|
} else {
|
|
|
|
if (childConfig.margin.left) {
|
|
|
|
child.left = childConfig.margin.left;
|
|
|
|
} else if (childConfig.margin.right) {
|
|
|
|
child.right = targetSize.width - childConfig.margin.right;
|
|
|
|
}
|
2019-10-23 19:37:06 +08:00
|
|
|
}
|
|
|
|
if ((gravity & TOP) == TOP) {
|
|
|
|
child.top = 0;
|
|
|
|
} else if ((gravity & BOTTOM) == BOTTOM) {
|
2019-11-15 15:38:24 +08:00
|
|
|
child.bottom = targetSize.height;
|
2019-10-23 19:37:06 +08:00
|
|
|
} else if ((gravity & CENTER_Y) == CENTER_Y) {
|
2019-11-15 15:38:24 +08:00
|
|
|
child.centerY = targetSize.height / 2;
|
|
|
|
} else {
|
|
|
|
if (childConfig.margin.top) {
|
|
|
|
child.top = childConfig.margin.top;
|
|
|
|
} else if (childConfig.margin.bottom) {
|
|
|
|
child.bottom = targetSize.height - childConfig.margin.bottom;
|
|
|
|
}
|
2019-10-23 19:37:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2019-10-23 20:11:24 +08:00
|
|
|
@implementation DoricLinearView
|
2019-10-23 19:37:06 +08:00
|
|
|
@end
|
|
|
|
|
2019-10-23 20:11:24 +08:00
|
|
|
@implementation DoricVLayoutView
|
2019-11-27 15:28:39 +08:00
|
|
|
|
|
|
|
- (CGSize)sizeThatFits:(CGSize)size {
|
2019-11-15 15:38:24 +08:00
|
|
|
CGFloat contentWidth = 0;
|
|
|
|
CGFloat contentHeight = 0;
|
|
|
|
NSUInteger contentWeight = 0;
|
2019-10-23 19:37:06 +08:00
|
|
|
for (UIView *child in self.subviews) {
|
|
|
|
if (child.isHidden) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-15 15:38:24 +08:00
|
|
|
DoricLayoutConfig *childConfig = child.layoutConfig;
|
|
|
|
if (!childConfig) {
|
|
|
|
childConfig = [DoricLayoutConfig new];
|
|
|
|
}
|
2019-11-27 15:28:39 +08:00
|
|
|
CGSize childSize;
|
2019-11-27 11:55:49 +08:00
|
|
|
if (CGAffineTransformEqualToTransform(child.transform, CGAffineTransformIdentity)) {
|
2019-11-27 15:28:39 +08:00
|
|
|
childSize = [child measureSize:CGSizeMake(size.width, size.height - contentHeight)];
|
2019-11-27 11:55:49 +08:00
|
|
|
if (childConfig.weight) {
|
|
|
|
childSize.height = child.height;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
childSize = child.bounds.size;
|
2019-11-15 15:38:24 +08:00
|
|
|
}
|
|
|
|
contentWidth = MAX(contentWidth, childSize.width + childConfig.margin.left + childConfig.margin.right);
|
|
|
|
contentHeight += childSize.height + self.space + childConfig.margin.top + childConfig.margin.bottom;
|
|
|
|
contentWeight += childConfig.weight;
|
|
|
|
}
|
|
|
|
contentHeight -= self.space;
|
|
|
|
self.contentWidth = contentWidth;
|
|
|
|
self.contentHeight = contentHeight;
|
|
|
|
self.contentWeight = contentWeight;
|
|
|
|
if (contentWeight) {
|
|
|
|
contentHeight = size.height;
|
2019-10-23 19:37:06 +08:00
|
|
|
}
|
2019-11-15 15:38:24 +08:00
|
|
|
return CGSizeMake(contentWidth, contentHeight);
|
2019-10-23 19:37:06 +08:00
|
|
|
}
|
|
|
|
|
2019-11-27 15:28:39 +08:00
|
|
|
- (void)layoutSelf:(CGSize)targetSize {
|
|
|
|
[super layoutSelf:targetSize];
|
2019-11-26 20:15:16 +08:00
|
|
|
self.width = targetSize.width;
|
|
|
|
self.height = targetSize.height;
|
2019-10-23 19:37:06 +08:00
|
|
|
CGFloat yStart = 0;
|
|
|
|
if ((self.gravity & TOP) == TOP) {
|
|
|
|
yStart = 0;
|
|
|
|
} else if ((self.gravity & BOTTOM) == BOTTOM) {
|
2019-11-15 15:38:24 +08:00
|
|
|
yStart = targetSize.height - self.contentHeight;
|
2019-10-23 19:37:06 +08:00
|
|
|
} else if ((self.gravity & CENTER_Y) == CENTER_Y) {
|
2019-11-15 15:38:24 +08:00
|
|
|
yStart = (targetSize.height - self.contentHeight) / 2;
|
2019-10-23 19:37:06 +08:00
|
|
|
}
|
2019-11-15 15:38:24 +08:00
|
|
|
CGFloat remain = targetSize.height - self.contentHeight;
|
2019-10-23 19:37:06 +08:00
|
|
|
for (UIView *child in self.subviews) {
|
|
|
|
if (child.isHidden) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-27 11:55:49 +08:00
|
|
|
if (!CGAffineTransformEqualToTransform(child.transform, CGAffineTransformIdentity)) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-15 15:38:24 +08:00
|
|
|
DoricLayoutConfig *childConfig = child.layoutConfig;
|
|
|
|
if (!childConfig) {
|
|
|
|
childConfig = [DoricLayoutConfig new];
|
|
|
|
}
|
|
|
|
|
2019-11-27 15:28:39 +08:00
|
|
|
CGSize size = [child measureSize:CGSizeMake(targetSize.width, targetSize.height - yStart)];
|
2019-11-15 15:38:24 +08:00
|
|
|
if (childConfig.weight) {
|
2019-11-27 15:28:39 +08:00
|
|
|
size.height = child.height + remain / self.contentWeight * childConfig.weight;
|
2019-11-15 15:38:24 +08:00
|
|
|
}
|
2019-11-27 15:28:39 +08:00
|
|
|
[child layoutSelf:size];
|
2019-10-23 19:37:06 +08:00
|
|
|
DoricGravity gravity = childConfig.alignment | self.gravity;
|
|
|
|
if ((gravity & LEFT) == LEFT) {
|
|
|
|
child.left = 0;
|
|
|
|
} else if ((gravity & RIGHT) == RIGHT) {
|
|
|
|
child.right = self.width;
|
|
|
|
} else if ((gravity & CENTER_X) == CENTER_X) {
|
2019-11-15 15:38:24 +08:00
|
|
|
child.centerX = targetSize.width / 2;
|
2019-10-23 19:37:06 +08:00
|
|
|
} else {
|
|
|
|
if (childConfig.margin.left) {
|
|
|
|
child.left = childConfig.margin.left;
|
|
|
|
} else if (childConfig.margin.right) {
|
2019-11-15 15:38:24 +08:00
|
|
|
child.right = targetSize.width - childConfig.margin.right;
|
2019-10-23 19:37:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (childConfig.margin.top) {
|
|
|
|
yStart += childConfig.margin.top;
|
|
|
|
}
|
|
|
|
child.top = yStart;
|
|
|
|
yStart = child.bottom + self.space;
|
|
|
|
if (childConfig.margin.bottom) {
|
|
|
|
yStart += childConfig.margin.bottom;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2019-10-23 20:11:24 +08:00
|
|
|
@implementation DoricHLayoutView
|
2019-11-27 15:28:39 +08:00
|
|
|
- (CGSize)sizeThatFits:(CGSize)size {
|
2019-11-15 15:38:24 +08:00
|
|
|
CGFloat contentWidth = 0;
|
|
|
|
CGFloat contentHeight = 0;
|
|
|
|
NSUInteger contentWeight = 0;
|
2019-10-23 19:37:06 +08:00
|
|
|
for (UIView *child in self.subviews) {
|
|
|
|
if (child.isHidden) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-15 15:38:24 +08:00
|
|
|
DoricLayoutConfig *childConfig = child.layoutConfig;
|
|
|
|
if (!childConfig) {
|
|
|
|
childConfig = [DoricLayoutConfig new];
|
|
|
|
}
|
2019-11-27 15:28:39 +08:00
|
|
|
CGSize childSize;
|
2019-11-27 11:55:49 +08:00
|
|
|
if (CGAffineTransformEqualToTransform(child.transform, CGAffineTransformIdentity)) {
|
2019-11-27 15:28:39 +08:00
|
|
|
childSize = [child measureSize:CGSizeMake(size.width - contentWidth, size.height)];
|
2019-11-27 11:55:49 +08:00
|
|
|
if (childConfig.weight) {
|
|
|
|
childSize.width = child.width;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
childSize = child.bounds.size;
|
2019-11-15 15:38:24 +08:00
|
|
|
}
|
|
|
|
contentWidth += childSize.width + self.space + childConfig.margin.left + childConfig.margin.right;
|
|
|
|
contentHeight = MAX(contentHeight, childSize.height + childConfig.margin.top + childConfig.margin.bottom);
|
|
|
|
contentWeight += childConfig.weight;
|
|
|
|
}
|
|
|
|
contentWidth -= self.space;
|
|
|
|
self.contentWidth = contentWidth;
|
|
|
|
self.contentHeight = contentHeight;
|
|
|
|
self.contentWeight = contentWeight;
|
|
|
|
if (contentWeight) {
|
|
|
|
contentWidth = size.width;
|
2019-10-23 19:37:06 +08:00
|
|
|
}
|
2019-11-15 15:38:24 +08:00
|
|
|
return CGSizeMake(contentWidth, contentHeight);
|
2019-10-23 19:37:06 +08:00
|
|
|
}
|
|
|
|
|
2019-11-27 15:28:39 +08:00
|
|
|
- (void)layoutSelf:(CGSize)targetSize {
|
|
|
|
[super layoutSelf:targetSize];
|
2019-10-23 19:37:06 +08:00
|
|
|
CGFloat xStart = 0;
|
2019-11-15 15:38:24 +08:00
|
|
|
if (self.contentWeight) {
|
|
|
|
xStart = 0;
|
|
|
|
} else if ((self.gravity & LEFT) == LEFT) {
|
2019-10-23 19:37:06 +08:00
|
|
|
xStart = 0;
|
|
|
|
} else if ((self.gravity & RIGHT) == RIGHT) {
|
2019-11-15 15:38:24 +08:00
|
|
|
xStart = targetSize.width - self.contentWidth;
|
2019-10-23 19:37:06 +08:00
|
|
|
} else if ((self.gravity & CENTER_X) == CENTER_X) {
|
2019-11-15 15:38:24 +08:00
|
|
|
xStart = (targetSize.width - self.contentWidth) / 2;
|
2019-10-23 19:37:06 +08:00
|
|
|
}
|
2019-11-15 15:38:24 +08:00
|
|
|
CGFloat remain = targetSize.width - self.contentWidth;
|
2019-10-23 19:37:06 +08:00
|
|
|
for (UIView *child in self.subviews) {
|
|
|
|
if (child.isHidden) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-27 11:55:49 +08:00
|
|
|
if (!CGAffineTransformEqualToTransform(child.transform, CGAffineTransformIdentity)) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-15 15:38:24 +08:00
|
|
|
DoricLayoutConfig *childConfig = child.layoutConfig;
|
|
|
|
if (!childConfig) {
|
|
|
|
childConfig = [DoricLayoutConfig new];
|
|
|
|
}
|
|
|
|
|
2019-11-27 15:28:39 +08:00
|
|
|
CGSize size = [child measureSize:CGSizeMake(targetSize.width - xStart, targetSize.height)];
|
2019-11-15 15:38:24 +08:00
|
|
|
if (childConfig.weight) {
|
2019-11-27 15:28:39 +08:00
|
|
|
size.width = child.width + remain / self.contentWeight * childConfig.weight;
|
2019-11-15 15:38:24 +08:00
|
|
|
}
|
|
|
|
|
2019-11-27 15:28:39 +08:00
|
|
|
[child layoutSelf:size];
|
2019-11-15 15:38:24 +08:00
|
|
|
|
2019-10-23 19:37:06 +08:00
|
|
|
DoricGravity gravity = childConfig.alignment | self.gravity;
|
|
|
|
if ((gravity & TOP) == TOP) {
|
|
|
|
child.top = 0;
|
|
|
|
} else if ((gravity & BOTTOM) == BOTTOM) {
|
2019-11-15 15:38:24 +08:00
|
|
|
child.bottom = targetSize.height;
|
2019-10-23 19:37:06 +08:00
|
|
|
} else if ((gravity & CENTER_Y) == CENTER_Y) {
|
2019-11-15 15:38:24 +08:00
|
|
|
child.centerY = targetSize.height / 2;
|
2019-10-23 19:37:06 +08:00
|
|
|
} else {
|
|
|
|
if (childConfig.margin.top) {
|
|
|
|
child.top = childConfig.margin.top;
|
|
|
|
} else if (childConfig.margin.bottom) {
|
2019-11-15 15:38:24 +08:00
|
|
|
child.bottom = targetSize.height - childConfig.margin.bottom;
|
2019-10-23 19:37:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (childConfig.margin.left) {
|
|
|
|
xStart += childConfig.margin.left;
|
|
|
|
}
|
|
|
|
child.left = xStart;
|
|
|
|
xStart = child.right + self.space;
|
|
|
|
if (childConfig.margin.right) {
|
|
|
|
xStart += childConfig.margin.right;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
2019-10-23 20:11:24 +08:00
|
|
|
DoricVLayoutView *vLayout(NSArray <__kindof UIView *> *views) {
|
|
|
|
DoricVLayoutView *layout = [[DoricVLayoutView alloc] initWithFrame:CGRectZero];
|
2019-10-23 19:37:06 +08:00
|
|
|
for (__kindof UIView *uiView in views) {
|
|
|
|
[layout addSubview:uiView];
|
|
|
|
}
|
2019-10-23 20:06:21 +08:00
|
|
|
layout.layoutConfig = [[DoricLayoutConfig alloc] initWithWidth:DoricLayoutWrapContent height:DoricLayoutWrapContent];
|
2019-10-23 19:37:06 +08:00
|
|
|
return layout;
|
|
|
|
}
|
|
|
|
|
2019-10-23 20:11:24 +08:00
|
|
|
DoricHLayoutView *hLayout(NSArray <__kindof UIView *> *views) {
|
|
|
|
DoricHLayoutView *layout = [[DoricHLayoutView alloc] initWithFrame:CGRectZero];
|
2019-10-23 19:37:06 +08:00
|
|
|
for (__kindof UIView *uiView in views) {
|
|
|
|
[layout addSubview:uiView];
|
|
|
|
}
|
2019-10-23 20:06:21 +08:00
|
|
|
layout.layoutConfig = [[DoricLayoutConfig alloc] initWithWidth:DoricLayoutWrapContent height:DoricLayoutWrapContent];
|
2019-10-23 19:37:06 +08:00
|
|
|
return layout;
|
|
|
|
}
|