2019-09-30 15:04:19 -07:00
|
|
|
/*
|
2018-09-11 15:27:47 -07:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-10-12 09:32:40 -07:00
|
|
|
*
|
2019-10-15 10:30:08 -07:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-10-12 09:32:40 -07:00
|
|
|
*/
|
2019-10-15 10:30:08 -07:00
|
|
|
|
2020-02-18 08:09:53 -08:00
|
|
|
#import <objc/runtime.h>
|
2016-12-02 11:18:15 -08:00
|
|
|
#import "UIView+Yoga.h"
|
2017-01-08 11:42:43 -08:00
|
|
|
#import "YGLayout+Private.h"
|
2016-10-12 09:32:40 -07:00
|
|
|
|
2020-02-18 08:09:53 -08:00
|
|
|
static const void* kYGYogaAssociatedKey = &kYGYogaAssociatedKey;
|
2016-11-21 10:12:26 -08:00
|
|
|
|
2017-01-08 11:42:43 -08:00
|
|
|
@implementation UIView (YogaKit)
|
2016-11-11 16:38:13 -08:00
|
|
|
|
2020-02-18 08:09:53 -08:00
|
|
|
- (YGLayout*)yoga {
|
|
|
|
YGLayout* yoga = objc_getAssociatedObject(self, kYGYogaAssociatedKey);
|
2017-01-08 11:42:43 -08:00
|
|
|
if (!yoga) {
|
|
|
|
yoga = [[YGLayout alloc] initWithView:self];
|
2020-02-18 08:09:53 -08:00
|
|
|
objc_setAssociatedObject(
|
|
|
|
self, kYGYogaAssociatedKey, yoga, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
2016-11-11 16:38:13 -08:00
|
|
|
}
|
2016-11-17 07:29:33 -08:00
|
|
|
|
2017-01-08 11:42:43 -08:00
|
|
|
return yoga;
|
2016-11-17 07:29:33 -08:00
|
|
|
}
|
|
|
|
|
2020-02-18 08:09:53 -08:00
|
|
|
- (BOOL)isYogaEnabled {
|
2017-09-21 09:58:08 -07:00
|
|
|
return objc_getAssociatedObject(self, kYGYogaAssociatedKey) != nil;
|
|
|
|
}
|
|
|
|
|
2020-02-18 08:09:53 -08:00
|
|
|
- (void)configureLayoutWithBlock:(YGLayoutConfigurationBlock)block {
|
2017-02-13 09:16:22 -08:00
|
|
|
if (block != nil) {
|
|
|
|
block(self.yoga);
|
|
|
|
}
|
|
|
|
}
|
2016-10-27 12:45:07 -07:00
|
|
|
|
|
|
|
@end
|
2020-08-13 13:24:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
static const void* kYGBoundsSizeAssociatedKey = &kYGBoundsSizeAssociatedKey;
|
|
|
|
static void YogaSwizzleInstanceMethod(Class cls, SEL originalSelector, SEL swizzledSelector);
|
|
|
|
|
|
|
|
@implementation UIView (YogaKitAutoApplyLayout)
|
|
|
|
|
|
|
|
+ (void)load {
|
2020-08-13 17:17:15 +08:00
|
|
|
static dispatch_once_t onceToken;
|
2020-08-13 13:24:14 +08:00
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
YogaSwizzleInstanceMethod(self, @selector(initWithFrame:), @selector(_yoga_initWithFrame:));
|
|
|
|
YogaSwizzleInstanceMethod(self, @selector(setFrame:), @selector(_yoga_setFrame:));
|
|
|
|
YogaSwizzleInstanceMethod(self, @selector(setBounds:), @selector(_yoga_setBounds:));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CGSize)_yoga_boundsSize {
|
|
|
|
NSValue *value = (NSValue *)objc_getAssociatedObject(self, kYGBoundsSizeAssociatedKey);
|
|
|
|
|
|
|
|
return value ? value.CGSizeValue : CGSizeMake(YGUndefined, YGUndefined);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)set_yoga_boundsSize:(CGSize)size {
|
|
|
|
objc_setAssociatedObject(self,
|
|
|
|
kYGBoundsSizeAssociatedKey,
|
|
|
|
[NSValue valueWithCGSize:size],
|
|
|
|
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)_yoga_initWithFrame:(CGRect)frame {
|
|
|
|
id _self = [self _yoga_initWithFrame:frame];
|
|
|
|
if (_self) {
|
|
|
|
[self _yoga_applyLayout];
|
|
|
|
}
|
|
|
|
|
|
|
|
return _self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)_yoga_setFrame:(CGRect)frame {
|
|
|
|
[self _yoga_setFrame:frame];
|
|
|
|
|
|
|
|
[self _yoga_applyLayout];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)_yoga_setBounds:(CGRect)bounds {
|
|
|
|
[self _yoga_setBounds:bounds];
|
|
|
|
|
|
|
|
[self _yoga_applyLayout];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)_yoga_applyLayout {
|
|
|
|
if (self.isYogaEnabled && self.yoga.isEnabled) {
|
|
|
|
CGSize size = self.bounds.size;
|
|
|
|
CGSize prev = self._yoga_boundsSize;
|
|
|
|
if (!CGSizeEqualToSize(size, prev)) {
|
|
|
|
self._yoga_boundsSize = size;
|
|
|
|
[self.yoga applyLayoutPreservingOrigin:YES];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
static void YogaSwizzleInstanceMethod(Class cls, SEL originalSelector, SEL swizzledSelector) {
|
|
|
|
if (!cls || !originalSelector || !swizzledSelector) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Method originalMethod = class_getInstanceMethod(cls, originalSelector);
|
|
|
|
Method swizzledMethod = class_getInstanceMethod(cls, swizzledSelector);
|
|
|
|
if (!originalMethod || !swizzledMethod) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
IMP swizzledIMP = method_getImplementation(swizzledMethod);
|
|
|
|
if (class_addMethod(cls, originalSelector, swizzledIMP, method_getTypeEncoding(swizzledMethod))) {
|
|
|
|
class_replaceMethod(cls,
|
|
|
|
swizzledSelector,
|
|
|
|
method_getImplementation(originalMethod),
|
|
|
|
method_getTypeEncoding(originalMethod));
|
|
|
|
} else {
|
|
|
|
method_exchangeImplementations(originalMethod, swizzledMethod);
|
|
|
|
}
|
|
|
|
}
|