[YogaKit] add unit tests.

- pixel-align for view’s frame;
- add unit tests for YogaKit.
This commit is contained in:
vvveiii
2020-08-13 17:17:15 +08:00
parent b78cfa6698
commit ae3aa9582c
6 changed files with 147 additions and 40 deletions

View File

@@ -43,7 +43,7 @@ static void YogaSwizzleInstanceMethod(Class cls, SEL originalSelector, SEL swizz
@implementation UIView (YogaKitAutoApplyLayout)
+ (void)load {
static dispatch_once_t onceToken = 0;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
YogaSwizzleInstanceMethod(self, @selector(initWithFrame:), @selector(_yoga_initWithFrame:));
YogaSwizzleInstanceMethod(self, @selector(setFrame:), @selector(_yoga_setFrame:));

View File

@@ -468,7 +468,7 @@ static CGFloat YGRoundPixelValue(CGFloat value) {
scale = [UIScreen mainScreen].scale;
});
return roundf(value * scale) / scale;
return ceil(value * scale) / scale; // Pixel-align
}
static void YGApplyLayoutToViewHierarchy(UIView* view, BOOL preserveOrigin) {
@@ -513,15 +513,13 @@ static void YGApplyLayoutToViewHierarchy(UIView* view, BOOL preserveOrigin) {
CGRect frame = (CGRect){
.origin =
{
.x = YGRoundPixelValue(topLeft.x + origin.x),
.y = YGRoundPixelValue(topLeft.y + origin.y),
.x = topLeft.x + origin.x,
.y = topLeft.y + origin.y,
},
.size =
{
.width = MAX(YGRoundPixelValue(bottomRight.x) -
YGRoundPixelValue(topLeft.x), 0),
.height = MAX(YGRoundPixelValue(bottomRight.y) -
YGRoundPixelValue(topLeft.y), 0),
.width = MAX(YGRoundPixelValue(bottomRight.x - topLeft.x), 0),
.height = MAX(YGRoundPixelValue(bottomRight.y - topLeft.y), 0),
},
};