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>
|
|
|
|
#import "UIView+Doric.h"
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2019-10-23 20:06:21 +08:00
|
|
|
@implementation DoricMarginConfig
|
2019-10-23 19:37:06 +08:00
|
|
|
- (instancetype)init {
|
|
|
|
if (self = [super init]) {
|
2019-10-23 20:06:21 +08:00
|
|
|
_margin = DoricMarginMake(0, 0, 0, 0);
|
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 margin:(DoricMargin)margin {
|
2019-10-23 19:37:06 +08:00
|
|
|
if (self = [super initWithWidth:width height:height]) {
|
|
|
|
_margin = margin;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2019-10-23 20:06:21 +08:00
|
|
|
@implementation DoricStackConfig
|
2019-10-23 19:37:06 +08:00
|
|
|
@end
|
|
|
|
|
2019-10-23 20:06:21 +08:00
|
|
|
@implementation DoricLinearConfig
|
2019-10-23 19:37:06 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
2019-10-23 20:06:21 +08:00
|
|
|
@interface DoricLayoutContainer ()
|
2019-10-23 19:37:06 +08:00
|
|
|
@property(nonatomic, assign) BOOL waitingLayout;
|
|
|
|
@end
|
|
|
|
|
2019-10-23 20:06:21 +08:00
|
|
|
@implementation DoricLayoutContainer
|
2019-10-23 19:37:06 +08:00
|
|
|
- (instancetype)init {
|
|
|
|
if (self = [super init]) {
|
|
|
|
_waitingLayout = NO;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame {
|
|
|
|
if (self = [super initWithFrame:frame]) {
|
|
|
|
_waitingLayout = NO;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithCoder:(NSCoder *)coder {
|
|
|
|
if (self = [super initWithCoder:coder]) {
|
|
|
|
_waitingLayout = NO;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-23 20:06:21 +08:00
|
|
|
- (DoricLayoutConfig *)configForChild:(UIView *)child {
|
|
|
|
DoricLayoutConfig *config = child.layoutConfig;
|
2019-10-23 19:37:06 +08:00
|
|
|
if (!config) {
|
2019-10-23 20:06:21 +08:00
|
|
|
config = [[DoricLayoutConfig alloc] init];
|
2019-10-23 19:37:06 +08:00
|
|
|
}
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)requestLayout {
|
2019-10-23 20:11:24 +08:00
|
|
|
if ([self.superview isKindOfClass:[DoricLinearView class]]) {
|
|
|
|
[(DoricLinearView *) self.superview requestLayout];
|
2019-10-23 19:37:06 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (self.waitingLayout) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.waitingLayout = YES;
|
2019-11-01 17:50:56 +08:00
|
|
|
__weak typeof(self) _self = self;
|
2019-10-23 19:37:06 +08:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2019-11-01 17:50:56 +08:00
|
|
|
__strong typeof(_self) self = _self;
|
2019-10-23 19:37:06 +08:00
|
|
|
[self sizeToFit];
|
|
|
|
[self layout];
|
2019-11-01 17:50:56 +08:00
|
|
|
self.waitingLayout = NO;
|
2019-10-23 19:37:06 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-01 17:50:56 +08:00
|
|
|
- (void)setNeedsLayout {
|
2019-11-01 19:22:57 +08:00
|
|
|
[super setNeedsLayout];
|
2019-11-01 17:50:56 +08:00
|
|
|
if (self.waitingLayout) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-01 19:22:57 +08:00
|
|
|
[self requestLayout];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)waitingLayout {
|
|
|
|
if ([self.superview isKindOfClass:[DoricLayoutContainer class]]) {
|
|
|
|
return [(DoricLayoutContainer *) self.superview waitingLayout];
|
|
|
|
}
|
|
|
|
return _waitingLayout;
|
2019-11-01 17:50:56 +08:00
|
|
|
}
|
|
|
|
|
2019-10-23 19:37:06 +08:00
|
|
|
- (void)layout {
|
|
|
|
[self.subviews enumerateObjectsUsingBlock:^(__kindof UIView *child, NSUInteger idx, BOOL *stop) {
|
2019-10-23 20:06:21 +08:00
|
|
|
if ([child isKindOfClass:[DoricLayoutContainer class]]) {
|
|
|
|
[(DoricLayoutContainer *) child layout];
|
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
|
|
|
|
- (DoricStackConfig *)configForChild:(UIView *)child {
|
|
|
|
DoricStackConfig *config = (DoricStackConfig *) child.layoutConfig;
|
2019-10-23 19:37:06 +08:00
|
|
|
if (!config) {
|
2019-10-23 20:06:21 +08:00
|
|
|
config = [[DoricStackConfig alloc] init];
|
2019-10-23 19:37:06 +08:00
|
|
|
}
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)sizeToFit {
|
2019-10-23 20:06:21 +08:00
|
|
|
DoricLayoutConfig *config = self.layoutConfig;
|
2019-10-23 19:37:06 +08:00
|
|
|
self.contentWidth = 0;
|
|
|
|
self.contentHeight = 0;
|
|
|
|
for (UIView *child in self.subviews) {
|
|
|
|
if (child.isHidden) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-10-23 20:06:21 +08:00
|
|
|
DoricStackConfig *childConfig = [self configForChild:child];
|
|
|
|
if ([child isKindOfClass:[DoricLayoutContainer class]]
|
|
|
|
|| childConfig.widthSpec == DoricLayoutWrapContent
|
|
|
|
|| childConfig.heightSpec == DoricLayoutWrapContent) {
|
2019-10-23 19:37:06 +08:00
|
|
|
[child sizeToFit];
|
|
|
|
}
|
|
|
|
self.contentWidth = MAX(self.contentWidth, child.width);
|
|
|
|
self.contentHeight = MAX(self.contentHeight, child.height);
|
|
|
|
}
|
2019-10-23 20:06:21 +08:00
|
|
|
if (config.widthSpec == DoricLayoutWrapContent) {
|
2019-10-23 19:37:06 +08:00
|
|
|
self.width = self.contentWidth;
|
2019-10-23 20:06:21 +08:00
|
|
|
} else if (config.widthSpec == DoricLayoutAtMost) {
|
2019-10-23 19:37:06 +08:00
|
|
|
self.width = self.superview.width;
|
|
|
|
}
|
2019-10-23 20:06:21 +08:00
|
|
|
if (config.heightSpec == DoricLayoutWrapContent) {
|
2019-10-23 19:37:06 +08:00
|
|
|
self.height = self.contentHeight;
|
2019-10-23 20:06:21 +08:00
|
|
|
} else if (config.heightSpec == DoricLayoutAtMost) {
|
2019-10-23 19:37:06 +08:00
|
|
|
self.height = self.superview.height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)layout {
|
|
|
|
for (UIView *child in self.subviews) {
|
|
|
|
if (child.isHidden) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-10-23 20:06:21 +08:00
|
|
|
DoricStackConfig *childConfig = [self configForChild:child];
|
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) {
|
|
|
|
child.centerX = self.width / 2;
|
|
|
|
}
|
|
|
|
if ((gravity & TOP) == TOP) {
|
|
|
|
child.top = 0;
|
|
|
|
} else if ((gravity & BOTTOM) == BOTTOM) {
|
|
|
|
child.bottom = self.height;
|
|
|
|
} else if ((gravity & CENTER_Y) == CENTER_Y) {
|
|
|
|
child.centerY = self.height / 2;
|
|
|
|
}
|
2019-10-23 20:06:21 +08:00
|
|
|
if (childConfig.widthSpec == DoricLayoutAtMost) {
|
2019-10-23 19:37:06 +08:00
|
|
|
child.width = self.width;
|
|
|
|
}
|
2019-10-23 20:06:21 +08:00
|
|
|
if (childConfig.heightSpec == DoricLayoutAtMost) {
|
2019-10-23 19:37:06 +08:00
|
|
|
child.height = self.height;
|
|
|
|
}
|
|
|
|
|
2019-10-23 20:06:21 +08:00
|
|
|
if ([child isKindOfClass:[DoricLayoutContainer class]]) {
|
|
|
|
[(DoricLayoutContainer *) child layout];
|
2019-10-23 19:37:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2019-10-23 20:11:24 +08:00
|
|
|
@interface DoricLinearView ()
|
2019-10-23 19:37:06 +08:00
|
|
|
@property(nonatomic, assign) CGFloat contentWidth;
|
|
|
|
@property(nonatomic, assign) CGFloat contentHeight;
|
|
|
|
@property(nonatomic, assign) NSUInteger contentWeight;
|
|
|
|
@end
|
|
|
|
|
2019-10-23 20:11:24 +08:00
|
|
|
@implementation DoricLinearView
|
2019-10-23 20:06:21 +08:00
|
|
|
- (DoricLinearConfig *)configForChild:(UIView *)child {
|
|
|
|
DoricLinearConfig *config = (DoricLinearConfig *) child.layoutConfig;
|
2019-10-23 19:37:06 +08:00
|
|
|
if (!config) {
|
2019-10-23 20:06:21 +08:00
|
|
|
config = [[DoricLinearConfig alloc] init];
|
2019-10-23 19:37:06 +08:00
|
|
|
}
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2019-10-23 20:11:24 +08:00
|
|
|
@implementation DoricVLayoutView
|
2019-10-23 19:37:06 +08:00
|
|
|
|
|
|
|
- (void)sizeToFit {
|
2019-10-23 20:06:21 +08:00
|
|
|
DoricLayoutConfig *config = self.layoutConfig;
|
2019-10-23 19:37:06 +08:00
|
|
|
self.contentWidth = 0;
|
|
|
|
self.contentHeight = 0;
|
|
|
|
self.contentWeight = 0;
|
|
|
|
for (UIView *child in self.subviews) {
|
|
|
|
if (child.isHidden) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-10-23 20:06:21 +08:00
|
|
|
DoricLinearConfig *childConfig = [self configForChild:child];
|
|
|
|
if ([child isKindOfClass:[DoricLayoutContainer class]]
|
|
|
|
|| childConfig.widthSpec == DoricLayoutWrapContent
|
|
|
|
|| childConfig.heightSpec == DoricLayoutWrapContent) {
|
2019-10-23 19:37:06 +08:00
|
|
|
[child sizeToFit];
|
|
|
|
}
|
|
|
|
self.contentWidth = MAX(self.contentWidth, child.width + childConfig.margin.left + childConfig.margin.right);
|
|
|
|
self.contentHeight += child.height + self.space + childConfig.margin.top + childConfig.margin.bottom;
|
|
|
|
self.contentWeight += childConfig.weight;
|
|
|
|
}
|
|
|
|
self.contentHeight -= self.space;
|
2019-10-23 20:06:21 +08:00
|
|
|
if (config.widthSpec == DoricLayoutWrapContent) {
|
2019-10-23 19:37:06 +08:00
|
|
|
self.width = self.contentWidth;
|
2019-10-23 20:06:21 +08:00
|
|
|
} else if (config.widthSpec == DoricLayoutAtMost) {
|
2019-10-23 19:37:06 +08:00
|
|
|
self.width = self.superview.width;
|
|
|
|
}
|
2019-10-23 20:06:21 +08:00
|
|
|
if (config.heightSpec == DoricLayoutWrapContent) {
|
2019-10-23 19:37:06 +08:00
|
|
|
self.height = self.contentHeight;
|
2019-10-23 20:06:21 +08:00
|
|
|
} else if (config.heightSpec == DoricLayoutAtMost) {
|
2019-10-23 19:37:06 +08:00
|
|
|
self.height = self.superview.height;
|
|
|
|
}
|
|
|
|
if (self.contentWeight) {
|
|
|
|
CGFloat remain = self.height - self.contentHeight;
|
|
|
|
for (UIView *child in self.subviews) {
|
|
|
|
if (child.isHidden) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-10-23 20:06:21 +08:00
|
|
|
DoricLinearConfig *childConfig = [self configForChild:child];
|
2019-10-23 19:37:06 +08:00
|
|
|
if (childConfig.weight) {
|
|
|
|
child.height += remain / self.contentWeight * childConfig.weight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.contentHeight = self.height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)layout {
|
|
|
|
CGFloat yStart = 0;
|
|
|
|
if ((self.gravity & TOP) == TOP) {
|
|
|
|
yStart = 0;
|
|
|
|
} else if ((self.gravity & BOTTOM) == BOTTOM) {
|
|
|
|
yStart = self.height - self.contentHeight;
|
|
|
|
} else if ((self.gravity & CENTER_Y) == CENTER_Y) {
|
|
|
|
yStart = (self.height - self.contentHeight) / 2;
|
|
|
|
}
|
|
|
|
for (UIView *child in self.subviews) {
|
|
|
|
if (child.isHidden) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-10-23 20:06:21 +08:00
|
|
|
DoricLinearConfig *childConfig = [self configForChild:child];
|
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) {
|
|
|
|
child.centerX = self.width / 2;
|
|
|
|
} else {
|
|
|
|
if (childConfig.margin.left) {
|
|
|
|
child.left = childConfig.margin.left;
|
|
|
|
} else if (childConfig.margin.right) {
|
|
|
|
child.right = self.width - childConfig.margin.right;
|
|
|
|
}
|
|
|
|
}
|
2019-10-23 20:06:21 +08:00
|
|
|
if (childConfig.widthSpec == DoricLayoutAtMost) {
|
2019-10-23 19:37:06 +08:00
|
|
|
child.width = self.width;
|
|
|
|
}
|
2019-10-23 20:06:21 +08:00
|
|
|
if (childConfig.heightSpec == DoricLayoutAtMost) {
|
2019-10-23 19:37:06 +08:00
|
|
|
child.height = self.height - yStart - childConfig.margin.top - childConfig.margin.bottom - self.space;
|
|
|
|
}
|
|
|
|
if (childConfig.margin.top) {
|
|
|
|
yStart += childConfig.margin.top;
|
|
|
|
}
|
|
|
|
child.top = yStart;
|
|
|
|
yStart = child.bottom + self.space;
|
|
|
|
if (childConfig.margin.bottom) {
|
|
|
|
yStart += childConfig.margin.bottom;
|
|
|
|
}
|
2019-10-23 20:06:21 +08:00
|
|
|
if ([child isKindOfClass:[DoricLayoutContainer class]]) {
|
|
|
|
[(DoricLayoutContainer *) child layout];
|
2019-10-23 19:37:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2019-10-23 20:11:24 +08:00
|
|
|
@implementation DoricHLayoutView
|
2019-10-23 19:37:06 +08:00
|
|
|
- (void)sizeToFit {
|
2019-10-23 20:06:21 +08:00
|
|
|
DoricLinearConfig *config;
|
2019-10-23 20:11:24 +08:00
|
|
|
if ([self.superview isKindOfClass:[DoricLinearView class]]) {
|
|
|
|
config = [(DoricLinearView *) self.superview configForChild:self];
|
2019-10-23 19:37:06 +08:00
|
|
|
} else {
|
2019-10-23 20:06:21 +08:00
|
|
|
config = (DoricLinearConfig *) self.layoutConfig;
|
2019-10-23 19:37:06 +08:00
|
|
|
if (!config) {
|
2019-10-23 20:06:21 +08:00
|
|
|
config = [[DoricLinearConfig alloc] init];
|
2019-10-23 19:37:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self.contentWidth = 0;
|
|
|
|
self.contentHeight = 0;
|
|
|
|
self.contentWeight = 0;
|
|
|
|
for (UIView *child in self.subviews) {
|
|
|
|
if (child.isHidden) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-10-23 20:06:21 +08:00
|
|
|
DoricLinearConfig *childConfig = [self configForChild:child];
|
|
|
|
if ([child isKindOfClass:[DoricLayoutContainer class]]
|
|
|
|
|| childConfig.widthSpec == DoricLayoutWrapContent
|
|
|
|
|| childConfig.heightSpec == DoricLayoutWrapContent) {
|
2019-10-23 19:37:06 +08:00
|
|
|
[child sizeToFit];
|
|
|
|
}
|
|
|
|
self.contentHeight = MAX(self.contentHeight, child.height + childConfig.margin.top + childConfig.margin.bottom);
|
|
|
|
self.contentWidth += child.width + self.space + childConfig.margin.left + childConfig.margin.right;
|
|
|
|
self.contentWeight += childConfig.weight;
|
|
|
|
}
|
|
|
|
self.contentWidth -= self.space;
|
2019-10-23 20:06:21 +08:00
|
|
|
if (config.widthSpec == DoricLayoutWrapContent) {
|
2019-10-23 19:37:06 +08:00
|
|
|
self.width = self.contentWidth;
|
2019-10-23 20:06:21 +08:00
|
|
|
} else if (config.widthSpec == DoricLayoutAtMost) {
|
2019-10-23 19:37:06 +08:00
|
|
|
self.width = self.superview.width;
|
|
|
|
}
|
2019-10-23 20:06:21 +08:00
|
|
|
if (config.heightSpec == DoricLayoutWrapContent) {
|
2019-10-23 19:37:06 +08:00
|
|
|
self.height = self.contentHeight;
|
2019-10-23 20:06:21 +08:00
|
|
|
} else if (config.heightSpec == DoricLayoutAtMost) {
|
2019-10-23 19:37:06 +08:00
|
|
|
self.height = self.superview.height;
|
|
|
|
}
|
|
|
|
if (self.contentWeight) {
|
|
|
|
CGFloat remain = self.width - self.contentWidth;
|
|
|
|
for (UIView *child in self.subviews) {
|
|
|
|
if (child.isHidden) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-10-23 20:06:21 +08:00
|
|
|
DoricLinearConfig *childConfig = [self configForChild:child];
|
2019-10-23 19:37:06 +08:00
|
|
|
if (childConfig.weight) {
|
|
|
|
child.width += remain / self.contentWeight * childConfig.weight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.contentWidth = self.width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)layout {
|
|
|
|
CGFloat xStart = 0;
|
|
|
|
if ((self.gravity & LEFT) == LEFT) {
|
|
|
|
xStart = 0;
|
|
|
|
} else if ((self.gravity & RIGHT) == RIGHT) {
|
|
|
|
xStart = self.width - self.contentWidth;
|
|
|
|
} else if ((self.gravity & CENTER_X) == CENTER_X) {
|
|
|
|
xStart = (self.width - self.contentWidth) / 2;
|
|
|
|
}
|
|
|
|
for (UIView *child in self.subviews) {
|
|
|
|
if (child.isHidden) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-10-23 20:06:21 +08:00
|
|
|
DoricLinearConfig *childConfig = [self configForChild:child];
|
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) {
|
|
|
|
child.bottom = self.height;
|
|
|
|
} else if ((gravity & CENTER_Y) == CENTER_Y) {
|
|
|
|
child.centerY = self.height / 2;
|
|
|
|
} else {
|
|
|
|
if (childConfig.margin.top) {
|
|
|
|
child.top = childConfig.margin.top;
|
|
|
|
} else if (childConfig.margin.bottom) {
|
|
|
|
child.bottom = self.height - childConfig.margin.bottom;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 20:06:21 +08:00
|
|
|
if (childConfig.heightSpec == DoricLayoutAtMost) {
|
2019-10-23 19:37:06 +08:00
|
|
|
child.height = self.height;
|
|
|
|
}
|
2019-10-23 20:06:21 +08:00
|
|
|
if (childConfig.widthSpec == DoricLayoutAtMost) {
|
2019-10-23 19:37:06 +08:00
|
|
|
child.width = self.width - xStart - childConfig.margin.right - childConfig.margin.left - self.space;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (childConfig.margin.left) {
|
|
|
|
xStart += childConfig.margin.left;
|
|
|
|
}
|
|
|
|
child.left = xStart;
|
|
|
|
xStart = child.right + self.space;
|
|
|
|
if (childConfig.margin.right) {
|
|
|
|
xStart += childConfig.margin.right;
|
|
|
|
}
|
2019-10-23 20:06:21 +08:00
|
|
|
if ([child isKindOfClass:[DoricLayoutContainer class]]) {
|
|
|
|
[(DoricLayoutContainer *) child layout];
|
2019-10-23 19:37:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
static const void *kLayoutConfig = &kLayoutConfig;
|
|
|
|
static const void *kTagString = &kTagString;
|
|
|
|
|
2019-10-23 20:11:24 +08:00
|
|
|
@implementation UIView (DoricLayoutConfig)
|
2019-10-23 19:37:06 +08:00
|
|
|
@dynamic layoutConfig;
|
|
|
|
|
2019-10-23 20:06:21 +08:00
|
|
|
- (void)setLayoutConfig:(DoricLayoutConfig *)layoutConfig {
|
2019-10-23 19:37:06 +08:00
|
|
|
objc_setAssociatedObject(self, kLayoutConfig, layoutConfig, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
|
|
|
}
|
|
|
|
|
2019-10-23 20:06:21 +08:00
|
|
|
- (DoricLayoutConfig *)layoutConfig {
|
2019-10-23 19:37:06 +08:00
|
|
|
return objc_getAssociatedObject(self, kLayoutConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (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
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-10-23 20:11:24 +08:00
|
|
|
DoricVLayoutView *vLayoutWithBlock(NSArray <UIView *(^)()> *blocks) {
|
|
|
|
DoricVLayoutView *layout = [[DoricVLayoutView alloc] initWithFrame:CGRectZero];
|
2019-10-23 19:37:06 +08:00
|
|
|
UIView *(^block)();
|
|
|
|
for (block in blocks) {
|
|
|
|
[layout addSubview:block()];
|
|
|
|
}
|
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 *hLayoutWithBlock(NSArray <UIView *(^)()> *blocks) {
|
|
|
|
DoricHLayoutView *layout = [[DoricHLayoutView alloc] initWithFrame:CGRectZero];
|
2019-10-23 19:37:06 +08:00
|
|
|
UIView *(^block)();
|
|
|
|
for (block in blocks) {
|
|
|
|
[layout addSubview:block()];
|
|
|
|
}
|
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:06:21 +08:00
|
|
|
}
|