Commit bf72714
committed
fix(ios): center TextInput text, placeholder, and caret when lineHeight > fontSize
On iOS, when a TextInput's lineHeight exceeds its font's line height, UIKit
anchors glyphs to the bottom of the attributed-string line box instead of
centering them within it. The same misalignment affects the placeholder.
On single-line UITextField the caret is also sized to the full line box.
This patch fixes all three surfaces. The approach varies by UIKit
rendering path:
UITextView (multi-line) typed text — honors NSBaselineOffsetAttributeName.
Call RCTApplyBaselineOffset in RCTTextInputComponentView._setAttributedString:
to inject the offset. Re-seed NSParagraphStyleAttributeName from
defaultTextAttributes on ranges missing it, because UIKit's typingAttributes
drops the paragraph style between keystrokes and _updateState round-trips
the stripped attributedText through TextInputState — without the re-seed
the helper sees maximumLineHeight == 0 and bails for typed content.
Placeholder on both UITextField.attributedPlaceholder (UILabel draw) and
RCTUITextView._placeholderView — both honor NSBaselineOffsetAttributeName.
Add the offset computation to _placeholderTextAttributes on both backing
views. The fix applies to both Paper and Fabric because the backing views
are shared.
UITextField (single-line) typed text — the UIFieldEditor draw path does
NOT honor NSBaselineOffsetAttributeName, and it sizes the caret to the
paragraph-style line box height. Override setAttributedText: to forward a
copy with paragraphStyle's minimumLineHeight/maximumLineHeight zeroed out
(and NSBaselineOffsetAttributeName removed) to super. UITextField then
renders the line at the font's natural height and its built-in vertical
centering positions it correctly in the bounds; the caret rect likewise
shrinks to the natural font height. _defaultTextAttributes (the local
ivar) keeps the unmodified paragraph style so the placeholder path still
sees the real lineHeight. Yoga's frame-height measurement is unaffected.1 parent 4464ab0 commit bf72714
3 files changed
Lines changed: 75 additions & 0 deletions
File tree
- packages/react-native
- Libraries/Text/TextInput
- Multiline
- Singleline
- React/Fabric/Mounting/ComponentViews/TextInput
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
364 | 364 | | |
365 | 365 | | |
366 | 366 | | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
367 | 373 | | |
368 | 374 | | |
369 | 375 | | |
| |||
Lines changed: 45 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
100 | 139 | | |
101 | 140 | | |
102 | 141 | | |
| |||
169 | 208 | | |
170 | 209 | | |
171 | 210 | | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
172 | 217 | | |
173 | 218 | | |
174 | 219 | | |
| |||
Lines changed: 24 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
768 | 768 | | |
769 | 769 | | |
770 | 770 | | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
771 | 795 | | |
772 | 796 | | |
773 | 797 | | |
| |||
0 commit comments