fix: the error in text.bounds when there is no root canvas (#2543)

This commit is contained in:
AZhan
2025-02-08 17:01:47 +08:00
committed by GitHub
parent 5ab9ce490d
commit 57a9f48084
2 changed files with 22 additions and 1 deletions

View File

@@ -598,7 +598,8 @@ export class Text extends UIRenderer implements ITextRenderer {
this._text === "" ||
this._fontSize === 0 ||
(this.enableWrapping && size.x <= 0) ||
(this.overflowMode === OverflowMode.Truncate && size.y <= 0)
(this.overflowMode === OverflowMode.Truncate && size.y <= 0) ||
!this._getRootCanvas()
);
}

View File

@@ -42,6 +42,26 @@ describe("Text", async () => {
expect(label.enableWrapping).to.eq(true);
});
it("get bounds", () => {
const textWithoutCanvas = root.addComponent(Text);
textWithoutCanvas.text = "hello world";
const bounds = textWithoutCanvas.bounds;
expect(bounds.min).to.deep.include({ x: -50, y: -50, z: 0 });
expect(bounds.max).to.deep.include({ x: 50, y: 50, z: 0 });
const labelBounds = label.bounds;
expect(labelBounds.min).to.deep.include({ x: -50, y: -50, z: 0 });
expect(labelBounds.max).to.deep.include({ x: 50, y: 50, z: 0 });
label.text = "hello world";
const labelBounds2 = label.bounds;
expect(labelBounds2.min).to.deep.include({ x: -50, y: -50, z: 0 });
expect(labelBounds2.max).to.deep.include({ x: 50, y: 50, z: 0 });
(<UITransform>label.entity.transform).size.x = 200;
const labelBounds3 = label.bounds;
expect(labelBounds3.min).to.deep.include({ x: -100, y: -50, z: 0 });
expect(labelBounds3.max).to.deep.include({ x: 100, y: 50, z: 0 });
});
it("emoji", () => {
const textEntity = canvasEntity.createChild("text");