智能客服
你问我答,随时在线为你解决问题

























HarmonyOS开发者版主1HarmonyOS 全文省略展开收起demo (API12+)

import { inspector } from '@kit.ArkUI';
@Entry
@Component
struct RenderTest {
@State text: string = 'Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World!'
@State isEllipsis: boolean = false
controller: TextController = new TextController()
maxLine: number = 2
ellipsisText: string = '...展开'
textWidth: number = 200
aboutToAppear(): void {
const context = this.getUIContext()
let listener: inspector.ComponentObserver = context.getUIInspector().createComponentObserver('TEXT_DEMO')
listener.on('layout', () => {
const manager = this.controller.getLayoutManager()
const line = manager.getLineCount()
console.log(`ccc ${line} `)
if (line <= this.maxLine) {
return;
}
const lastLine = manager.getLineMetrics(this.maxLine - 1)
const eWidth = context.getMeasureUtils().measureText({ textContent: this.ellipsisText })
const parentWidth = context.vp2px(this.textWidth)
let lastIndex = lastLine.endIndex
for (let i = lastLine.endIndex; i > lastLine.startIndex; i--) {
const textBox = manager.getRectsForRange({ start: lastLine.startIndex, end: i }, 0, 0)
let info = textBox[0]
let width = info.rect.right - info.rect.left
if (eWidth + width <= parentWidth) {
lastIndex = i
break;
}
}
this.text = this.text.slice(0, lastIndex)
this.isEllipsis = true
})
}
build() {
Column() {
Column() {
Text(undefined, { controller: this.controller }) {
Span(this.text)
if (this.isEllipsis) {
Span(this.ellipsisText)
}
}
.id('TEXT_DEMO')
}.width(this.textWidth)
.height(200)
.backgroundColor(Color.Pink)
}.width('100%')
.height('100%')
}
}
我要提问题
HarmonyOS grid网格拖拽交换顺序demo(API12+)
HarmonyOS NEXT 应用开发中想要一个通过@ohos.file.photoAccessHelper (相册管理模块)和富文本(RichEditor)来实现图文动态的编辑和发布,要怎么实现,有类似的demo吗?
HarmonyOS Next应用开发如何实现ping功能以及探测数据统计展示?
HarmonyOS Progress组件首尾重叠成一个点怎么取消,并且边缘有空白,怎么解决?(API12+)
HarmonyOS 全文省略demo (API12+)
HarmonyOS 文件下载demo(API12+)
HarmonyOS 查看图片示例(API12+)
HarmonyOS 文件解压demo (API12+)
智能客服
你问我答,随时在线为你解决问题
合作咨询
我们的专家服务团队将竭诚为您提供专业的合作咨询服务
解决方案
精准高效的一站式服务支持,助力开发者商业成功