-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yuyuehui.yyh
committed
Jun 8, 2023
1 parent
63853e5
commit f6f6332
Showing
2 changed files
with
34 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import G6 from '@antv/g6'; | ||
|
||
// G6换行符处理超长文本 | ||
export const superLongTextHandle = ( | ||
str: string, | ||
maxWidth: number, | ||
fontSize: number, | ||
) => { | ||
let currentWidth = 0; | ||
let res = str; | ||
// 区分汉字和字母 | ||
const pattern = new RegExp('[\u4E00-\u9FA5]+'); | ||
str.split('').forEach((letter, i) => { | ||
if (currentWidth > maxWidth) return; | ||
if (pattern.test(letter)) { | ||
// 中文字符 | ||
currentWidth += fontSize; | ||
} else { | ||
// 根据字体大小获取单个字母的宽度 | ||
currentWidth += G6.Util.getLetterWidth(letter, fontSize); | ||
} | ||
if (currentWidth > maxWidth) { | ||
res = `${str.slice(0, i)}\n${superLongTextHandle( | ||
str.slice(i), | ||
maxWidth, | ||
fontSize, | ||
)}`; | ||
} | ||
}); | ||
return res; | ||
}; |