{"name":"getTextWidth","title":"getTextWidth","type":"command","url":"https://community.bistudio.com/wiki/getTextWidth","revid":376640,"updated":"2025-11-01T20:58:51Z","groups":["GUI Control","Strings"],"status":"ok","games":{"arma3":"1.98"},"since":"arma3 1.98","arma3_since":"1.98","description":"Returns estimated width of the text based on font type and size. Similar to ctrlTextWidth but does not require a control and can be used to estimate text width for any control including buttons. Result does not include left and right margins (hardcoded at 0.008 each).\n\n> [!NOTE]\n> This command always uses unicode (see also forceUnicode).","syntaxes":[{"syntax":"text getTextWidth [font, size]","params":[{"name":"text","type":"String","description":"text string to measure","type_expr":"String"},{"name":"font","type":"String","optional":true,"default":"\"\" (system font)","description":"font type","type_expr":"String","optional_since":"arma3 2.22","text":"font: String - (Optional since Arma 3 v2.22, default \"\" (system font)) font type"},{"name":"size","type":"Number","optional":true,"default":"-1 (system font size)","description":"font size","type_expr":"Number","optional_since":"arma3 2.22","text":"size: Number - (Optional since Arma 3 v2.22, default -1 (system font size)) font size"}],"returns":{"type":"Number","type_expr":"Number"},"form":"binary","left":{"text":"text"},"right":{"text":"[font, size]"}}],"examples":[{"code":["_textWidth = \"Hello World\" getTextWidth [\"PuristaMedium\", 0.03];"]},{"code":["_textWidthInclMargins = (\"Hello World\" getTextWidth [\"PuristaMedium\", 0.03]) + 0.016;"]},{"code":["#define MARGINS 0.016\nprivate _ctrl = findDisplay 46 ctrlCreate [\"RscText\", -1];\n_ctrl ctrlSetText \"TestString\";\n_ctrl ctrlSetFont \"PuristaMedium\";\n_ctrl ctrlSetFontHeight 0.04;\nctrlTextWidth _ctrl == (\"TestString\" getTextWidth [\"PuristaMedium\", 0.04]) + MARGINS; // true"]}],"see_also":["ctrlTextWidth"],"notes":[{"user":"R3vo","date":"2020-10-28T22:10:00Z","text":"When working with multiline controls ctrlTextWidth or getTextWidth will not return a proper value since the text will be treated as one long line.\n**Solution (Execute in Eden Editor):**\n\n```sqf\ndisableSerialization;\n\nprivate _display = findDisplay 313 createDisplay \"RscDisplayEmpty\";\n\nprivate _edit = _display ctrlCreate [\"RscEditMulti\", -1];\n_edit ctrlSetPosition [0,0,1,1];\n_edit ctrlSetBackgroundColor [0,0,0,1];\n_edit ctrlSetFont \"EtelkaMonospacePro\";\n_edit ctrlSetFontHeight 0.03;\n_edit ctrlCommit 0;\n\nprivate _longestLineWidth = 0;\nprivate _exampleString = \"abcdefghijklmnopqrstuvxyz0134567890\";\nprivate _text = \"\";\n\nfor \"_lines\" from 1 to 400 do // loop through all lines and find the longest one\n{\n    _lineText = _exampleString select [round random 35];\n    _longestLineWidth = _longestLineWidth max (_lineText getTextWidth [\"EtelkaMonospacePro\", 0.03]);\n    _text = _text + _lineText + endl;\n};\n\n_edit ctrlSetPositionW (_longestLineWidth + 2 * 0.008); // width of longest line + left and right margin set in engine\n_edit ctrlCommit 0;\n_edit ctrlSetText _text;\n```"}]}