|
1 | 1 | import measure, { MeasureOptions } from '@ohos.measure';
|
| 2 | +import display from '@ohos.display'; |
2 | 3 | import { text } from "@kit.ArkGraphics2D";
|
3 | 4 |
|
4 | 5 | export function config(measureText: MeasureOptions) {
|
5 | 6 | let textSize = measure.measureTextSize(measureText);
|
6 | 7 | return textSize;
|
7 | 8 | };
|
8 | 9 |
|
9 |
| -export function measureTextParagraph(measureOptions: MeasureOptions,width: number){ |
| 10 | +function getWidth(width: number | undefined | null) { |
| 11 | + let densityWidth: number = 0; |
| 12 | + if (width) { |
| 13 | + densityWidth = width; |
| 14 | + } else { |
| 15 | + densityWidth = Math.floor(display.getDefaultDisplaySync().width / display.getDefaultDisplaySync().densityPixels); |
| 16 | + } |
| 17 | + return densityWidth; |
| 18 | +} |
| 19 | + |
| 20 | +export function measureTextParagraph(measureOptions: MeasureOptions, width: number) { |
10 | 21 | let myTextStyle: text.TextStyle = {
|
11 | 22 | fontSize: Number(measureOptions.fontSize),
|
12 |
| - fontStyle:text.FontStyle.NORMAL, |
13 |
| - letterSpacing: Number(measureOptions.letterSpacing), |
| 23 | + fontStyle: text.FontStyle.NORMAL, |
| 24 | + letterSpacing: Number(measureOptions.letterSpacing) || 0, |
14 | 25 | };
|
15 | 26 | let myParagraphStyle: text.ParagraphStyle = {
|
16 | 27 | textStyle: myTextStyle,
|
17 |
| - align: text.TextAlign.END, |
18 | 28 | };
|
19 | 29 | let ParagraphGraphBuilder = new text.ParagraphBuilder(myParagraphStyle, new text.FontCollection());
|
20 | 30 | ParagraphGraphBuilder.addText(`${measureOptions.textContent}`);
|
21 | 31 | // 文字绘制
|
22 | 32 | let paragraph = ParagraphGraphBuilder.build();
|
23 |
| - paragraph.layoutSync(width); |
| 33 | + paragraph.layoutSync(getWidth(width)); |
24 | 34 | // 行数
|
25 | 35 | let lines = paragraph.getLineCount();
|
26 | 36 | // 最后一行宽
|
|
0 commit comments