fix: web text maxLines

This commit is contained in:
钱泽虹
2023-09-12 17:10:24 +08:00
committed by osborn
parent 49b9232f92
commit 87a485e5a2
3 changed files with 12 additions and 38 deletions

View File

@@ -8,6 +8,7 @@ export class DoricTextNode extends DoricViewNode {
const div = document.createElement('div')
div.style.display = "flex"
div.style.overflow = "hidden"
div.style.lineHeight = `${this.lineHeight()}em`
this.textElement = document.createElement('span')
div.appendChild(this.textElement)
div.style.justifyContent = "center"
@@ -69,7 +70,6 @@ export class DoricTextNode extends DoricViewNode {
case "maxLines":
this.maxLines = prop as number
this.view.style.whiteSpace = 'normal'
this.view.style.overflow = 'hidden'
break
case "maxWidth":
if (prop) {
@@ -102,7 +102,7 @@ export class DoricTextNode extends DoricViewNode {
if (this.maxLines > 0) {
const computedStyle = window.getComputedStyle(this.view)
const currentContentHeight = this.view.clientHeight - pixelString2Number(computedStyle.paddingTop) - pixelString2Number(computedStyle.paddingBottom)
let maxHeight = this.maxLinesHeight(computedStyle)
let maxHeight = parseFloat(computedStyle.getPropertyValue('font-size')) * this.lineHeight() * this.maxLines
if (currentContentHeight > 0) {
maxHeight = Math.min(maxHeight, Math.ceil(currentContentHeight))
}
@@ -112,6 +112,10 @@ export class DoricTextNode extends DoricViewNode {
}
}
lineHeight() {
return 1.3
}
computedHeight(style:CSSStyleDeclaration) {
const tempEle = document.createElement('div')
tempEle.style.font = style.font
@@ -125,23 +129,6 @@ export class DoricTextNode extends DoricViewNode {
return height
}
maxLinesHeight(style:CSSStyleDeclaration) {
let text = ''
for (let i = 0; i < this.maxLines; i++) {
text += 'Test测试😊\n'
}
const tempEle = document.createElement('div')
tempEle.style.font = style.font
tempEle.textContent = text
tempEle.style.width = this.view.style.width
tempEle.style.whiteSpace = 'pre';
tempEle.style.overflow = 'hidden';
document.body.appendChild(tempEle)
const height = tempEle.offsetHeight
document.body.removeChild(tempEle)
return height
}
truncationText(style:CSSStyleDeclaration, maxHeight:number) {
const originalText = this.textElement.innerText
let start = 0, end = originalText.length