HarmonyOS 鸿蒙Next中placeholder如何居中显示?

HarmonyOS 鸿蒙Next中placeholder如何居中显示?

textInput中,设置了lineHeight为45,placeholder的字体为15,输入内容的字体为30,这样会导致,placeholder比光标高度小,而且是居底显示。怎么能让placeholder居中显示?

如下图:

cke_1317.jpeg


更多关于HarmonyOS 鸿蒙Next中placeholder如何居中显示?的实战教程也可以访问 https://www.itying.com/category-93-b0.html

8 回复
TextInput({ placeholder: '请输入标签名称', text: this.title })
  .placeholderFont({ size: 15 })
  .fontSize(30).lineHeight(45)

你这种设置,不仅placeholder 会偏下,你输入的内容也会偏下吧。

虽然不知道为什么要设置lineHeight,建议使用Text 和 TextInput盖在一起实现 placeholder 显示吧。

更多关于HarmonyOS 鸿蒙Next中placeholder如何居中显示?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


加上这一行代码,表示文本水平居中:

TextInput()
  .alignSelf(ItemAlign.Center)
  .xxx

希望HarmonyOS能继续优化系统稳定性,减少崩溃和重启的情况。

可以根据字体差异自动计算补偿量:

@State private paddingTop: number = (45 - 15) / 2 // 15为placeholder字体

TextInput()
  .placeholder('请输入')
  .placeholderFont({ size: 15 })
  .textFont({ size: 30 })
  .lineHeight(45)
  .padding({ top: this.paddingTop })

设置这个属性placeholderFont这个属性的大小和文字的大小保持一致

TextInput({
  text: this.text,
  controller: this.controller
})
.placeholderFont({
  size: 30,
  weight: 400
})

建议直接将字体设置为一致

在HarmonyOS鸿蒙Next中,要使placeholder居中显示,可以通过以下方式实现:

  1. 在TextInput组件中设置textAlign属性为TextAlign.Center:
TextInput({ placeholder: '请输入' })
  .placeholderFont({ size: 16, weight: FontWeight.Normal })
  .textAlign(TextAlign.Center)
  1. 如果需要整个输入框内容居中,可以同时设置TextInput的placeholderAlign属性和textAlign属性:
TextInput({ placeholder: '请输入' })
  .placeholderAlign(TextAlign.Center)
  .textAlign(TextAlign.Center)

在HarmonyOS Next中,要让TextInput的placeholder居中显示,可以通过以下方式实现:

  1. 使用placeholderAlign属性设置对齐方式:
TextInput()
  .placeholderAlign(TextAlign.Center)
  1. 如果需要更精确控制placeholder和输入文本的对齐,可以结合textAlignplaceholderAlign一起使用:
TextInput()
  .textAlign(TextAlign.Center)
  .placeholderAlign(TextAlign.Center)
  1. 对于字体大小不一致的情况,建议统一placeholder和输入文本的字体大小,或者通过padding调整位置:
TextInput()
  .placeholderFont({ size: 15 })
  .fontSize(30)
  .padding({ top: 5, bottom: 5 }) // 根据需要调整

注意:如果仍存在对齐问题,可能是由于lineHeight设置导致的,可以尝试调整lineHeight值使其与输入框高度匹配。

回到顶部