Skip to content

Commit cba30b5

Browse files
fix(ios): center TextInput placeholder when lineHeight > fontSize
The placeholder in RCTUITextField and RCTUITextView inherits the paragraph style from defaultTextAttributes, so it suffers the same bottom-anchor misalignment as typed text when lineHeight > fontSize. Apply the equivalent NSBaselineOffsetAttributeName in _placeholderTextAttributes so the placeholder renders at the same vertical position as typed text. These backing views are shared between Paper and Fabric, so this also fixes the placeholder on the old arch (where facebook#38359 only addressed typed text).
1 parent 609af21 commit cba30b5

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

packages/react-native/Libraries/Text/TextInput/Multiline/RCTUITextView.mm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,12 @@ - (void)_updatePlaceholder
364364
[textAttributes setValue:defaultPlaceholderFont() forKey:NSFontAttributeName];
365365
}
366366

367+
NSParagraphStyle *paragraphStyle = textAttributes[NSParagraphStyleAttributeName];
368+
UIFont *font = textAttributes[NSFontAttributeName];
369+
if (paragraphStyle && font && paragraphStyle.maximumLineHeight > font.lineHeight) {
370+
textAttributes[NSBaselineOffsetAttributeName] = @((paragraphStyle.maximumLineHeight - font.lineHeight) / 2.0);
371+
}
372+
367373
return textAttributes;
368374
}
369375

packages/react-native/Libraries/Text/TextInput/Singleline/RCTUITextField.mm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ - (void)setDisableKeyboardShortcuts:(BOOL)disableKeyboardShortcuts
169169
[textAttributes removeObjectForKey:NSForegroundColorAttributeName];
170170
}
171171

172+
NSParagraphStyle *paragraphStyle = textAttributes[NSParagraphStyleAttributeName];
173+
UIFont *font = textAttributes[NSFontAttributeName];
174+
if (paragraphStyle && font && paragraphStyle.maximumLineHeight > font.lineHeight) {
175+
textAttributes[NSBaselineOffsetAttributeName] = @((paragraphStyle.maximumLineHeight - font.lineHeight) / 2.0);
176+
}
177+
172178
return textAttributes;
173179
}
174180

0 commit comments

Comments
 (0)