Text组件设置maxLines后如何确定文本是否被隐藏

Text组件设置maxLines后如何确定文本是否被隐藏

HarmonyOS
2024-01-21 12:57:36
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
荣光因缘来

可以通过MeasureText.measureTextSize来判断Text文本的高度是否超出maxLines设置的高度进行判断。具体使用可参考以下代码示例及参考链接。

代码示例

import { MeasureText } from '@kit.ArkUI' 
 
@Entry 
@Component 
struct TextInputExample { 
  @State text: string = ''; 
  @State truncatedHint: 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(400) 
        .height(40) 
        .margin(20) 
        .fontSize(14) 
        .fontColor(Color.Black) 
        .onChange((value: string) => { 
          this.text = value; 
          let textSizeShow1: SizeOptions = MeasureText.measureTextSize({ 
            textContent: this.text, 
            constraintWidth: 100, 
            fontSize: 14, 
            overflow: TextOverflow.Ellipsis, 
            maxLines: 2 
          }) 
          let textSizeShow2: SizeOptions = MeasureText.measureTextSize({ 
            textContent: this.text + " ", 
            constraintWidth: 100, 
            fontSize: 14, 
            overflow: TextOverflow.Ellipsis, 
            maxLines: 2000000 
          }) 
          console.log("textSizeShow1.height=" + textSizeShow1.height); 
          console.log("textSizeShow2.height=" + textSizeShow2.height); 
 
          if (textSizeShow2 && textSizeShow1 && textSizeShow2?.height && textSizeShow1?.height && (textSizeShow2?.height > textSizeShow1?.height)) { 
            console.log("文本截断"); 
            this.truncatedHint = "文本截断"; 
          } else { 
            console.log("文本未截断"); 
            this.truncatedHint = "文本未截断"; 
          } 
        }) 
      Text(this.text) 
        .maxLines(2) 
        .width(100) 
        .textOverflow({ overflow: TextOverflow.Ellipsis }) 
        .border({ width: 1 }) 
        .minFontSize(14) 
        .maxFontSize(24) 
      Text(this.truncatedHint) 
 
    }.width('100%') 
  } 
}

已于2024-1-30 18:35:51修改
分享
微博
QQ
微信
回复
2024-01-22 15:21:27
相关问题
HarmonyOS 单行Text是否截断隐藏?
95浏览 • 1回复 待解决
Text设置maxLines时使用Infinity报错
1669浏览 • 1回复 待解决
如何计算文本是否溢出省略
1555浏览 • 1回复 待解决
Text怎么设置文本渐变?
96浏览 • 0回复 待解决
获取文本Text组件的宽度
293浏览 • 1回复 待解决
Input组件是否支持设置文本居中对齐
1791浏览 • 1回复 待解决
文本组件是否支持分段设置字体样式
1992浏览 • 1回复 待解决
确定文件名是否确定,再试一次
18937浏览 • 1回复 待解决
Text组件是否支持多行显示
1814浏览 • 1回复 待解决
提问
该提问已有0人参与 ,帮助了0人