Skip to content

Commit 35955d1

Browse files
authored
Fix Android TextInput placeholder ignoring lineHeight
## Summary: On Android, a `TextInput` with a `lineHeight` draws its placeholder a few pixels off its text. `lineHeight` reaches the text as a `CustomLineHeightSpan`, but the hint is set as a plain string, so it keeps the font's own line height. The hint now gets the same span, rebuilt when the placeholder, `lineHeight` or font scale changes. A removed `lineHeight` comes back as `0`: the hint stays plain then, since a 0-high span would hide it. Related: #39145 ## Changelog: [ANDROID] [FIXED] - TextInput placeholder takes the input's `lineHeight` ## Test Plan: - New tests in `ReactTextInputPropertyTest`. `./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest` passes. - RNTester Playground on a Pixel 8 Pro: placeholder (left), same text typed (right), `fontSize: 16`, `lineHeight: 40`, single-line then multiline. Also checked: removing `lineHeight` at runtime, and font scale 1.5. | Before | After | | --- | --- | | <img src="https://raw.githubusercontent.com/Pduhard/react-native/pr-assets/placeholder-line-height-before.png" width="400" /> | <img src="https://raw.githubusercontent.com/Pduhard/react-native/pr-assets/placeholder-line-height-after.png" width="400" /> | Written with help from Claude Code, tested on device by me. Differential Revision: D130917481 Pull Request resolved: #59825
1 parent 5441168 commit 35955d1

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import android.os.Bundle
2525
import android.text.Editable
2626
import android.text.InputType
2727
import android.text.Spannable
28+
import android.text.SpannableString
2829
import android.text.SpannableStringBuilder
2930
import android.text.Spanned
3031
import android.text.TextPaint
@@ -392,6 +393,7 @@ public open class ReactEditText public constructor(context: Context) : AppCompat
392393
override fun setLineHeight(lineHeight: Int) {
393394
textAttributes.lineHeight = lineHeight.toFloat()
394395
// We don't call super.setLineHeight() because LineHeight is fully managed by ReactNative
396+
updateHint()
395397
}
396398

397399
override fun onScrollChanged(horiz: Int, vert: Int, oldHoriz: Int, oldVert: Int) {
@@ -669,11 +671,32 @@ public open class ReactEditText public constructor(context: Context) : AppCompat
669671
public fun setPlaceholder(placeholder: String?) {
670672
if (placeholder != this.placeholder) {
671673
this.placeholder = placeholder
672-
hint = placeholder
674+
updateHint()
673675
}
674676
updatePlaceholderEllipsize()
675677
}
676678

679+
// The text takes its line height from a span (see addSpansFromStyleAttributes); the hint needs
680+
// the same span, or it is drawn with the font's own line height, off the text's baseline.
681+
private fun updateHint() {
682+
val placeholder = placeholder
683+
val lineHeight = textAttributes.lineHeight
684+
hint =
685+
// A removed lineHeight prop comes back as 0, and a 0-high span would hide the hint.
686+
if (placeholder.isNullOrEmpty() || lineHeight.isNaN() || lineHeight <= 0f) {
687+
placeholder
688+
} else {
689+
SpannableString(placeholder).apply {
690+
setSpan(
691+
CustomLineHeightSpan(textAttributes.effectiveLineHeight),
692+
0,
693+
length,
694+
Spanned.SPAN_INCLUSIVE_INCLUSIVE,
695+
)
696+
}
697+
}
698+
}
699+
677700
private fun updatePlaceholderEllipsize() {
678701
ellipsize =
679702
if (!isMultiline) {
@@ -1176,6 +1199,7 @@ public open class ReactEditText public constructor(context: Context) : AppCompat
11761199
if (!effectiveLetterSpacing.isNaN()) {
11771200
letterSpacing = effectiveLetterSpacing
11781201
}
1202+
updateHint()
11791203
}
11801204

11811205
/**

‎packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/textinput/ReactTextInputPropertyTest.kt‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import com.facebook.react.uimanager.ThemedReactContext
3636
import com.facebook.react.uimanager.style.BorderRadiusProp
3737
import com.facebook.react.views.text.DefaultStyleValuesUtil.getDefaultTextColorHint
3838
import com.facebook.react.views.text.ReactTextUpdate
39+
import com.facebook.react.views.text.internal.span.CustomLineHeightSpan
3940
import com.facebook.react.views.text.internal.span.CustomStyleSpan
4041
import com.facebook.testutils.shadows.ShadowSoLoader
4142
import org.assertj.core.api.Assertions.assertThat
@@ -206,6 +207,33 @@ class ReactTextInputPropertyTest {
206207
assertThat(view.ellipsize).isEqualTo(TextUtils.TruncateAt.END)
207208
}
208209

210+
@Test
211+
fun testPlaceholderTakesTheLineHeight() {
212+
manager.updateProperties(view, buildStyles("lineHeight", 30.0))
213+
manager.updateProperties(view, buildStyles("placeholder", "sometext"))
214+
215+
val hint = view.hint as Spanned
216+
assertThat(hint.toString()).isEqualTo("sometext")
217+
assertThat(hint.getSpans(0, hint.length, CustomLineHeightSpan::class.java)).hasSize(1)
218+
}
219+
220+
@Test
221+
fun testPlaceholderTakesALineHeightSetAfterIt() {
222+
manager.updateProperties(view, buildStyles("placeholder", "sometext"))
223+
manager.updateProperties(view, buildStyles("lineHeight", 30.0))
224+
225+
val hint = view.hint as Spanned
226+
assertThat(hint.getSpans(0, hint.length, CustomLineHeightSpan::class.java)).hasSize(1)
227+
}
228+
229+
@Test
230+
fun testPlaceholderDropsARemovedLineHeight() {
231+
manager.updateProperties(view, buildStyles("placeholder", "sometext", "lineHeight", 30.0))
232+
manager.updateProperties(view, buildStyles("lineHeight", null))
233+
234+
assertThat(view.hint).isEqualTo("sometext")
235+
}
236+
209237
@Test
210238
fun testPlaceholderEllipsizeRespectsMultiline() {
211239
manager.updateProperties(view, buildStyles("placeholder", "a very long placeholder string"))

0 commit comments

Comments
 (0)