|
@@ -13,6 +13,9 @@ import { SGraphItem } from "../SGraphItem";
|
|
|
* @author 张宇(taohuzy@163.com)
|
|
|
*/
|
|
|
export class STextItem extends SObjectItem {
|
|
|
+ /** 记录painter */
|
|
|
+ private _painter: SPainter | null = null;
|
|
|
+
|
|
|
/** 文本内容 */
|
|
|
private _text: string = "";
|
|
|
get text(): string {
|
|
@@ -20,9 +23,14 @@ export class STextItem extends SObjectItem {
|
|
|
}
|
|
|
set text(v: string) {
|
|
|
this._text = v;
|
|
|
+ this._textArr = this.text.split(/\n/g);
|
|
|
+ this.drawFormatText();
|
|
|
this.update();
|
|
|
}
|
|
|
|
|
|
+ /** 切割后的文本数组 */
|
|
|
+ private _textArr: string[] = [];
|
|
|
+
|
|
|
/** 文本颜色 */
|
|
|
private _color: string = "#333333";
|
|
|
get color(): string {
|
|
@@ -40,6 +48,7 @@ export class STextItem extends SObjectItem {
|
|
|
}
|
|
|
set font(v: SFont) {
|
|
|
this._font = v;
|
|
|
+ this.drawFormatText();
|
|
|
this.update();
|
|
|
}
|
|
|
|
|
@@ -116,7 +125,14 @@ export class STextItem extends SObjectItem {
|
|
|
//绘制文本
|
|
|
painter.brush.color = new SColor(this.color);
|
|
|
painter.font = this.font;
|
|
|
- this.drawFormatText(painter);
|
|
|
+ this._textArr.forEach((text: string, index: number) => {
|
|
|
+ painter.drawText(
|
|
|
+ text,
|
|
|
+ 2,
|
|
|
+ index * (this.font.size + 2) + 2,
|
|
|
+ this.maxWidth
|
|
|
+ );
|
|
|
+ });
|
|
|
} // Function drawShowText()
|
|
|
|
|
|
/**
|
|
@@ -124,25 +140,20 @@ export class STextItem extends SObjectItem {
|
|
|
*
|
|
|
* @param painter 绘制类
|
|
|
*/
|
|
|
- protected drawFormatText(painter: SPainter): void {
|
|
|
- let textArr: string[] = this.text.split(/\n/g);
|
|
|
- let textMaxWidth = 0;
|
|
|
- let textHeight: number = this.font.size;
|
|
|
- textArr.forEach((text: string, index: number) => {
|
|
|
- painter.drawText(
|
|
|
- text,
|
|
|
- 0,
|
|
|
- index * (textHeight + 2) + 2,
|
|
|
- this.maxWidth
|
|
|
- );
|
|
|
- let textWidth: number = painter.textWidth(text);
|
|
|
- if (textWidth > textMaxWidth) {
|
|
|
- textMaxWidth = textWidth;
|
|
|
- }
|
|
|
- });
|
|
|
- // 在绘制文本后计算文本的宽高
|
|
|
- this.width = textMaxWidth;
|
|
|
- this.height = (textHeight + 2) * textArr.length;
|
|
|
+ protected drawFormatText(): void {
|
|
|
+ if (this._painter instanceof SPainter) {
|
|
|
+ let textMaxWidth = 0;
|
|
|
+ let textHeight: number = this.font.size;
|
|
|
+ this._textArr.forEach((text: string, index: number) => {
|
|
|
+ let textWidth: number = this._painter?this._painter.textWidth(text)+4:4;
|
|
|
+ if (textWidth > textMaxWidth) {
|
|
|
+ textMaxWidth = textWidth;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ // 在绘制文本后计算文本的宽高
|
|
|
+ this.width = textMaxWidth;
|
|
|
+ this.height = (textHeight + 2) * this._textArr.length;
|
|
|
+ }
|
|
|
} // Function drawFormatText()
|
|
|
|
|
|
/**
|
|
@@ -151,6 +162,10 @@ export class STextItem extends SObjectItem {
|
|
|
* @param painter 绘画类
|
|
|
*/
|
|
|
onDraw(painter: SPainter): void {
|
|
|
+ if (!(this._painter instanceof SPainter)) {
|
|
|
+ this._painter = painter;
|
|
|
+ this.drawFormatText();
|
|
|
+ }
|
|
|
this.drawShowText(painter);
|
|
|
} // Function onDraw()
|
|
|
} // Class STextItem
|