{"name":"getTextWidth","operator":null,"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","replaced_by":[],"games":[{"game":"arma3","name":"Arma 3","version":"1.98"}],"since":{"game":"arma3","name":"Arma 3","version":"1.98"},"arma3_since":"1.98","branch":null,"locality":{"argument":null,"effect":null,"server_exec":false},"exec":null,"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]","since":null,"effect":null,"exec":null,"params":[{"name":"text","type":"String","optional":false,"default":null,"description":"text string to measure","types":["String"],"since":null,"text":"text: String - text string to measure"},{"name":"font","type":"String","optional":true,"default":"\"\" (system font)","description":"font type","types":["String"],"since":null,"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","types":["Number"],"since":null,"text":"size: Number - (Optional since Arma 3 v2.22, default -1 (system font size)) font size"}],"returns":{"type":"Number","types":["Number"],"description":null,"text":"Number"},"form":"binary","operator":null,"left":{"text":"text","types":["String"]},"right":{"text":"[font, size]","types":["Array"]}}],"examples":[{"text":"```sqf\n_textWidth = \"Hello World\" getTextWidth [\"PuristaMedium\", 0.03];\n```","code":["_textWidth = \"Hello World\" getTextWidth [\"PuristaMedium\", 0.03];"]},{"text":"```sqf\n_textWidthInclMargins = (\"Hello World\" getTextWidth [\"PuristaMedium\", 0.03]) + 0.016;\n```","code":["_textWidthInclMargins = (\"Hello World\" getTextWidth [\"PuristaMedium\", 0.03]) + 0.016;"]},{"text":"```sqf\n#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\n```","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"]}],"multiplayer":null,"problems":null,"aliases":[],"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\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```"}],"categories":["Arma 3: New Scripting Commands","Arma 3: Scripting Commands","Command Group: GUI Control","Command Group: Strings","Introduced with Arma 3 version 1.98","Scripting Commands"]}