* @function LevelRenderer.Tool.Areal.prototype.getTextHeight * @description 测算多行文本高度
(text, textFont)
| 1043 | * @description 测算多行文本高度 |
| 1044 | */ |
| 1045 | getTextHeight(text, textFont) { |
| 1046 | var key = text + ':' + textFont; |
| 1047 | if (this._textHeightCache[key]) { |
| 1048 | return this._textHeightCache[key]; |
| 1049 | } |
| 1050 | |
| 1051 | this._ctx = this._ctx || this.util.getContext(); |
| 1052 | |
| 1053 | this._ctx.save(); |
| 1054 | if (textFont) { |
| 1055 | this._ctx.font = textFont; |
| 1056 | } |
| 1057 | |
| 1058 | text = (text + '').split('\n'); |
| 1059 | // 比较粗暴 |
| 1060 | //var height = (this._ctx.measureText('国').width + 2) * text.length; //打包不支持中文,替换掉 |
| 1061 | var height = (this._ctx.measureText('ZH').width + 2) * text.length; |
| 1062 | |
| 1063 | this._ctx.restore(); |
| 1064 | |
| 1065 | this._textHeightCache[key] = height; |
| 1066 | if (++this._textHeightCacheCounter > this.TEXT_CACHE_MAX) { |
| 1067 | // 内存释放 |
| 1068 | this._textHeightCacheCounter = 0; |
| 1069 | this._textHeightCache = {}; |
| 1070 | } |
| 1071 | return height; |
| 1072 | } |
| 1073 | } |
no test coverage detected