Skip to content

Commit 51b3529

Browse files
sherginfacebook-github-bot
authored andcommittedFeb 16, 2018
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
1 parent 7630a61 commit 51b3529

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
 

‎Libraries/Text/Text/RCTTextShadowView.m

+36
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge
3131
_cachedTextStorages = [NSMapTable strongToStrongObjectsMapTable];
3232
_needsUpdateView = YES;
3333
YGNodeSetMeasureFunc(self.yogaNode, RCTTextShadowViewMeasure);
34+
YGNodeSetBaselineFunc(self.yogaNode, RCTTextShadowViewBaseline);
3435
}
3536

3637
return self;
@@ -307,6 +308,27 @@ - (void)layoutSubviewsWithContext:(RCTLayoutContext)layoutContext
307308
];
308309
}
309310

311+
- (CGFloat)lastBaselineForSize:(CGSize)size
312+
{
313+
NSAttributedString *attributedText =
314+
[self textStorageAndLayoutManagerThatFitsSize:size exclusiveOwnership:NO];
315+
316+
__block CGFloat maximumDescender = 0.0;
317+
318+
[attributedText enumerateAttribute:NSFontAttributeName
319+
inRange:NSMakeRange(0, attributedText.length)
320+
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
321+
usingBlock:
322+
^(UIFont *font, NSRange range, __unused BOOL *stop) {
323+
if (maximumDescender > font.descender) {
324+
maximumDescender = font.descender;
325+
}
326+
}
327+
];
328+
329+
return size.height + maximumDescender;
330+
}
331+
310332
static YGSize RCTTextShadowViewMeasure(YGNodeRef node, float width, YGMeasureMode widthMode, float height, YGMeasureMode heightMode)
311333
{
312334
CGSize maximumSize = (CGSize){
@@ -341,4 +363,18 @@ static YGSize RCTTextShadowViewMeasure(YGNodeRef node, float width, YGMeasureMod
341363
};
342364
}
343365

366+
static float RCTTextShadowViewBaseline(YGNodeRef node, const float width, const float height)
367+
{
368+
RCTTextShadowView *shadowTextView = (__bridge RCTTextShadowView *)YGNodeGetContext(node);
369+
370+
CGSize size = (CGSize){
371+
RCTCoreGraphicsFloatFromYogaFloat(width),
372+
RCTCoreGraphicsFloatFromYogaFloat(height)
373+
};
374+
375+
CGFloat lastBaseline = [shadowTextView lastBaselineForSize:size];
376+
377+
return RCTYogaFloatFromCoreGraphicsFloat(lastBaseline);
378+
}
379+
344380
@end

0 commit comments

Comments
 (0)
Please sign in to comment.