Skip to content

Commit

Permalink
feat: baseline support
Browse files Browse the repository at this point in the history
  • Loading branch information
OhKanghoon committed Sep 23, 2023
1 parent 0567063 commit ddf81b9
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions Sources/YogaKit/YGLayout.mm
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,24 @@ - (instancetype)initWithView:(UIView*)view {
_isEnabled = NO;
_isIncludedInLayout = YES;
_isUIView = [view isMemberOfClass:[UIView class]];

if ([view isKindOfClass:[UILabel class]]) {
if (!YGNodeHasBaselineFunc(_node)) {
YGNodeSetBaselineFunc(_node, YGMeasureBaselineLabel);
}
}

if ([view isKindOfClass:[UITextView class]]) {
if (!YGNodeHasBaselineFunc(_node)) {
YGNodeSetBaselineFunc(_node, YGMeasureBaselineTextView);
}
}

if ([view isKindOfClass:[UITextField class]]) {
if (!YGNodeHasBaselineFunc(_node)) {
YGNodeSetBaselineFunc(_node, YGMeasureBaselineTextField);
}
}
}

return self;
Expand Down Expand Up @@ -346,6 +364,42 @@ - (CGSize)calculateLayoutWithSize:(CGSize)size {

#pragma mark - Private

static float YGMeasureBaselineLabel(
YGNodeRef node,
const float width,
const float height) {

UILabel* view = (__bridge UILabel*) YGNodeGetContext(node);
return view.font.ascender; // height + view.font.ascender for lastBaseline
}

static float YGMeasureBaselineTextView(
YGNodeRef node,
const float width,
const float height) {

UITextView* view = (__bridge UITextView*) YGNodeGetContext(node);
return view.font.ascender + view.contentInset.top + view.textContainerInset.top;
}

static float YGMeasureBaselineTextField(
YGNodeRef node,
const float width,
const float height) {

UITextField* view = (__bridge UITextField*) YGNodeGetContext(node);

switch (view.borderStyle) {
case UITextBorderStyleNone:
return view.font.ascender;
case UITextBorderStyleLine:
return view.font.ascender + 4;
case UITextBorderStyleBezel:
case UITextBorderStyleRoundedRect:
return view.font.ascender + 7;
}
}

static YGSize YGMeasureView(
YGNodeRef node,
float width,
Expand Down

0 comments on commit ddf81b9

Please sign in to comment.