Skip to content

Commit

Permalink
base-line metric exposure for <Text>
Browse files Browse the repository at this point in the history
Summary:
Now <Text> exposes base-line metric to Yoga.

Before:
https://cl.ly/1F0B0D430U3e

After:
https://cl.ly/0G1Q29450O0y

Reviewed By: yungsters

Differential Revision: D6957055

fbshipit-source-id: 04c1df693915e294b54e3c33e0aea21611dcc232
  • Loading branch information
shergin authored and facebook-github-bot committed Feb 16, 2018
1 parent 7630a61 commit 51b3529
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Libraries/Text/Text/RCTTextShadowView.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge
_cachedTextStorages = [NSMapTable strongToStrongObjectsMapTable];
_needsUpdateView = YES;
YGNodeSetMeasureFunc(self.yogaNode, RCTTextShadowViewMeasure);
YGNodeSetBaselineFunc(self.yogaNode, RCTTextShadowViewBaseline);
}

return self;
Expand Down Expand Up @@ -307,6 +308,27 @@ - (void)layoutSubviewsWithContext:(RCTLayoutContext)layoutContext
];
}

- (CGFloat)lastBaselineForSize:(CGSize)size
{
NSAttributedString *attributedText =
[self textStorageAndLayoutManagerThatFitsSize:size exclusiveOwnership:NO];

__block CGFloat maximumDescender = 0.0;

[attributedText enumerateAttribute:NSFontAttributeName
inRange:NSMakeRange(0, attributedText.length)
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
usingBlock:
^(UIFont *font, NSRange range, __unused BOOL *stop) {
if (maximumDescender > font.descender) {
maximumDescender = font.descender;
}
}
];

return size.height + maximumDescender;
}

static YGSize RCTTextShadowViewMeasure(YGNodeRef node, float width, YGMeasureMode widthMode, float height, YGMeasureMode heightMode)
{
CGSize maximumSize = (CGSize){
Expand Down Expand Up @@ -341,4 +363,18 @@ static YGSize RCTTextShadowViewMeasure(YGNodeRef node, float width, YGMeasureMod
};
}

static float RCTTextShadowViewBaseline(YGNodeRef node, const float width, const float height)
{
RCTTextShadowView *shadowTextView = (__bridge RCTTextShadowView *)YGNodeGetContext(node);

CGSize size = (CGSize){
RCTCoreGraphicsFloatFromYogaFloat(width),
RCTCoreGraphicsFloatFromYogaFloat(height)
};

CGFloat lastBaseline = [shadowTextView lastBaselineForSize:size];

return RCTYogaFloatFromCoreGraphicsFloat(lastBaseline);
}

@end

0 comments on commit 51b3529

Please sign in to comment.