| 103 | * @returns |
| 104 | */ |
| 105 | export const fittingString = (str: string, maxWidth: number, fontSize: number) => { |
| 106 | const ellipsis = '...' |
| 107 | const ellipsisLength = G6.Util.getTextSize(ellipsis, fontSize)[0] |
| 108 | let currentWidth = 0 |
| 109 | let res = str |
| 110 | const pattern = new RegExp('[\u4E00-\u9FA5]+') |
| 111 | str.split('').forEach((letter, i) => { |
| 112 | if (currentWidth > maxWidth - ellipsisLength) return |
| 113 | if (pattern.test(letter)) { |
| 114 | currentWidth += fontSize |
| 115 | } else { |
| 116 | currentWidth += G6.Util.getLetterWidth(letter, fontSize) |
| 117 | } |
| 118 | if (currentWidth > maxWidth - ellipsisLength) { |
| 119 | res = `${str.substr(0, i)}${ellipsis}` |
| 120 | } |
| 121 | }) |
| 122 | return res |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * 动态获取节点距离 |