Permalink
Browse files

`base-line` metric exposure for <Text>

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 51b3529f6c2ca354800c0cf6ecb8eb3115eaa36e
Showing with 36 additions and 0 deletions.
  1. +36 −0 Libraries/Text/Text/RCTTextShadowView.m
@@ -31,6 +31,7 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge
_cachedTextStorages = [NSMapTable strongToStrongObjectsMapTable];
_needsUpdateView = YES;
YGNodeSetMeasureFunc(self.yogaNode, RCTTextShadowViewMeasure);
YGNodeSetBaselineFunc(self.yogaNode, RCTTextShadowViewBaseline);
}
return self;
@@ -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){
@@ -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.