Skip to content

Commit 0dbe183

Browse files
sherginfacebook-github-bot
authored andcommitted
base-line metric exposure for <TextInput>
Summary: Now <TextInput> (both bersions) exposes base-line metric to Yoga. Before: https://cl.ly/0G1Q29450O0y After: https://cl.ly/2X103V3O0322 Reviewed By: yungsters Differential Revision: D6957054 fbshipit-source-id: d76d96f56720d710a4230c53beafdb0b2521e8a9
1 parent 51b3529 commit 0dbe183

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

Libraries/Text/TextInput/RCTBaseTextInputShadowView.m

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge
3737
_needsUpdateView = YES;
3838

3939
YGNodeSetMeasureFunc(self.yogaNode, RCTBaseTextInputShadowViewMeasure);
40+
YGNodeSetBaselineFunc(self.yogaNode, RCTTextInputShadowViewBaseline);
4041
}
4142

4243
return self;
@@ -167,7 +168,7 @@ - (void)uiManagerWillPerformMounting
167168

168169
#pragma mark -
169170

170-
- (CGSize)sizeThatFitsMinimumSize:(CGSize)minimumSize maximumSize:(CGSize)maximumSize
171+
- (NSAttributedString *)measurableAttributedText
171172
{
172173
// Only for the very first render when we don't have `_localAttributedText`,
173174
// we use value directly from the property and/or nested content.
@@ -187,6 +188,13 @@ - (CGSize)sizeThatFitsMinimumSize:(CGSize)minimumSize maximumSize:(CGSize)maximu
187188
attributedText = [[NSAttributedString alloc] initWithString:text attributes:self.textAttributes.effectiveTextAttributes];
188189
}
189190

191+
return attributedText;
192+
}
193+
194+
- (CGSize)sizeThatFitsMinimumSize:(CGSize)minimumSize maximumSize:(CGSize)maximumSize
195+
{
196+
NSAttributedString *attributedText = [self measurableAttributedText];
197+
190198
if (!_textStorage) {
191199
_textContainer = [NSTextContainer new];
192200
_textContainer.lineFragmentPadding = 0.0; // Note, the default value is 5.
@@ -209,6 +217,26 @@ - (CGSize)sizeThatFitsMinimumSize:(CGSize)minimumSize maximumSize:(CGSize)maximu
209217
};
210218
}
211219

220+
- (CGFloat)lastBaselineForSize:(CGSize)size
221+
{
222+
NSAttributedString *attributedText = [self measurableAttributedText];
223+
224+
__block CGFloat maximumDescender = 0.0;
225+
226+
[attributedText enumerateAttribute:NSFontAttributeName
227+
inRange:NSMakeRange(0, attributedText.length)
228+
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
229+
usingBlock:
230+
^(UIFont *font, NSRange range, __unused BOOL *stop) {
231+
if (maximumDescender > font.descender) {
232+
maximumDescender = font.descender;
233+
}
234+
}
235+
];
236+
237+
return size.height + maximumDescender;
238+
}
239+
212240
static YGSize RCTBaseTextInputShadowViewMeasure(YGNodeRef node, float width, YGMeasureMode widthMode, float height, YGMeasureMode heightMode)
213241
{
214242
RCTShadowView *shadowView = (__bridge RCTShadowView *)YGNodeGetContext(node);
@@ -253,4 +281,18 @@ static YGSize RCTBaseTextInputShadowViewMeasure(YGNodeRef node, float width, YGM
253281
};
254282
}
255283

284+
static float RCTTextInputShadowViewBaseline(YGNodeRef node, const float width, const float height)
285+
{
286+
RCTBaseTextInputShadowView *shadowTextView = (__bridge RCTBaseTextInputShadowView *)YGNodeGetContext(node);
287+
288+
CGSize size = (CGSize){
289+
RCTCoreGraphicsFloatFromYogaFloat(width),
290+
RCTCoreGraphicsFloatFromYogaFloat(height)
291+
};
292+
293+
CGFloat lastBaseline = [shadowTextView lastBaselineForSize:size];
294+
295+
return RCTYogaFloatFromCoreGraphicsFloat(lastBaseline);
296+
}
297+
256298
@end

0 commit comments

Comments
 (0)