TextInput组件的placeholder文本较长时,默认会使用省略号来截断过长的文本。可通过以下四种方案让placeholder文本显示完整。
@Entry @Component struct TextInputExample { @State text: string = ''; controller: TextInputController = new TextInputController(); @State placeholder: string = 'input your word'; build() { Column() { TextInput({ text: this.text, placeholder: this.placeholder, controller: this.controller }) .placeholderColor(Color.Grey) .placeholderFont({ size: 14, weight: 400 }) .caretColor(Color.Blue) .width('30%') .margin(20) .fontSize(14) .fontColor(Color.Black) .inputFilter('[0-9]', (e) => { console.error(e); }) .onClick(() => { this.getUIContext().getPromptAction().showToast({ message: this.placeholder }); }) .onChange((value: string) => { this.text = value; }) } .width('100%') .backgroundColor(Color.White) } }
效果预览:

@Entry @Component struct TextInputExample2 { @State text: string = ''; controller: TextAreaController = new TextAreaController(); build() { Column() { TextArea({ text: this.text, placeholder: 'input your word', controller: this.controller }) .placeholderColor(Color.Grey) .placeholderFont({ size: 14, weight: 400 }) .caretColor(Color.Blue) .width('30%') .margin(20) .fontSize(14) .fontColor(Color.Black) .inputFilter('[0-9]', (e) => { console.error(e); }) .onChange((value: string) => { this.text = value; }) } .width('100%') .backgroundColor(Color.White) } }
效果预览:

@Entry @Component struct TextInputExample3 { @State text: string = ''; controller: TextInputController = new TextInputController(); build() { Column() { TextInput({ text: this.text, placeholder: 'input your word', controller: this.controller }) .placeholderColor(Color.Grey) .placeholderFont({ size: 10, weight: 400 }) .caretColor(Color.Blue) .width('30%') .margin(20) .fontSize(14) .fontColor(Color.Black) .inputFilter('[0-9]', (e) => { console.error(e); }) .onChange((value: string) => { this.text = value; }) } .width('100%') .backgroundColor(Color.White) } }
效果预览:

@Entry @Component struct TextInputExample4 { @State text: string = ''; controller: TextInputController = new TextInputController(); build() { Column() { TextInput({ text: this.text, placeholder: 'input your word', controller: this.controller }) .placeholderColor(Color.Grey) .placeholderFont({ size: 14, weight: 400 }) .caretColor(Color.Blue) .width('30%') .margin(20) .fontSize(14) .fontColor(Color.Black) .style(TextInputStyle.Inline) .wordBreak(WordBreak.BREAK_WORD) .inputFilter('[0-9]', (e) => { console.error(e); }) .onChange((value: string) => { this.text = value; }) } .width('100%') .backgroundColor(Color.White) } }
效果预览:
