Skip to content

Size the iOS multiline input in whole lines of its own text view - #218

Draft
simonhamp wants to merge 1 commit into
mainfrom
fix/ios-multiline-whole-lines
Draft

simonhamp wants to merge 1 commit into
mainfrom
fix/ios-multiline-whole-lines

Conversation

@simonhamp

Copy link
Copy Markdown
Member

On iOS, a multiline text input that has gone past its max-lines shows its top line cut part way through. It's easiest to see with a custom font, and it gets worse at bigger text sizes. We hit it in the devnull app (Geist font) while building its composer and reply bar (NativePHP/dev-null-app#177, #180).

Cause

The multiline branch of NativeUITextInputCore sized its TextEditor from an invisible Text copy of the typed text, with lineLimit(min...max). Text and UITextView lay out a custom font's lines at different heights. With Geist, five lines of Text came to about 5.4 lines of the editor. Once the text is longer than that, the editor scrolls the caret line to the bottom of the frame and the top line shows half cut.

The editor also used negative padding (-5 / -8) to pull back UITextView's text container insets, so it drew past its own frame. That's why even the system font field on main shows a sliver of a sixth line under the fifth.

Fix

  • The editor is sized from the UITextView behind it. A small UIViewRepresentable in the editor's background finds the text view, watches its contentSize, and reports its content height and line height. The field is then as tall as its text, in whole lines of the text view's own line height, between min-lines and max-lines. Past that it scrolls.
  • The line height comes from the typing font, so an empty field is already the right height and doesn't move when the first character goes in.
  • The text view's insets are set to zero on the text view itself instead of with negative padding, so nothing it scrolls is drawn outside the field. SwiftUI can put the insets back when it updates the editor, so they come off again before every report.
  • Because the height now follows the text a moment later, a focused field that gets shorter (for example after a text size change) scrolls the caret's line back into view.

Android was already fine and isn't touched.

Screenshots

iOS 27.0, iPhone 18 Pro simulator. Each pair is main on the left and this branch on the right. The top field uses Geist with max-lines="5" and the caret at the end; the second uses the system font.

Medium text size:

Large (default):

Extra extra large:

Testing

  • Pest: 504 passed. New tests/IosMultilineTextInputSizingTest.php checks the field is sized from the text view and its insets are zeroed; both tests fail on main. BaseTextInputSelectionTest matched the old indentation of the TextEditor line exactly, so that one assertion is now a regex.
  • swiftc -typecheck -wmo over mobile-air main's NativeRender plus this repo's resources/ios: the same 78 errors as main (types that live outside those folders) and none in this file. Planted errors in the new code were reported, so the file was checked.
  • On the simulator, in mobile-starter with this branch and mobile-air main:
    • medium, large and extra extra large text, each from a fresh launch (above);
    • typing into an empty Geist field with min-lines="2": its height went 41.67, 62.67, 83.33 pt for 2, 3 and 4 lines (Geist's line is 20.83 pt), and the first line stayed in view as it grew;
    • switching text size while the field was focused and scrolled kept the caret's line in view.
  • An earlier version of this patch has been in the devnull app's iPhone builds since build 31.

Not tested: iPad, a hardware keyboard, and iOS 18 or 26 runtimes.

What to watch for

  • The text view is found by introspection: the probe walks up to six ancestors and picks the UITextView under its own centre. If a future SwiftUI stops backing TextEditor with a UITextView, nothing is found and the field stays at min-lines of an estimated line (textSize * 1.3) and scrolls. It wouldn't crash, but it wouldn't grow either.
  • The height now updates one runloop after the text changes (KVO, then an async hop), where the old mirror changed in the same layout pass. I couldn't see a frame where the top line was hidden while growing, but it's the part I'd look at on a device.
  • leading-* still has no effect on the typed text, as before. The sizer reads the paragraph style's line spacing in case it ever does.

🤖 Generated with Claude Code

The multiline field sized its TextEditor from an invisible Text copy of
the typed text. Text and UITextView lay out a custom font's lines at
different heights, so the max-lines cap was never a whole number of the
editor's lines. Past the cap the editor scrolls the caret line to the
bottom of the frame, and the top line showed cut part way through.

The editor is now sized from the UITextView behind it: its content
height, in whole lines of its typing font's line height, between
min-lines and max-lines. Its text container insets are set to zero on
the text view itself instead of being pulled back with negative padding,
so no part of a scrolled-off line is drawn outside the field.

The field's height now follows its text a moment later than before, so
when a focused field gets shorter (a smaller text size, say) it scrolls
the caret's line back into view.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant