getTextWidth: Difference between revisions
Killzone Kid (talk | contribs) No edit summary |
Killzone Kid (talk | contribs) No edit summary |
||
| Line 15: | Line 15: | ||
|p1= text: [[String]] - text string to measure | |p1= text: [[String]] - text string to measure | ||
|p2= font: [[String]] - (Optional since | |p2= font: [[String]] - (Optional since {{GVI|arma3|2.22|size= 0.75}}, default "" (system font)) [[FXY_File_Format#Available_Fonts|font type]] | ||
|p3= size: [[Number]] - (Optional since | |p3= size: [[Number]] - (Optional since {{GVI|arma3|2.22|size= 0.75}}, default -1 (system font size)) font size | ||
|r1= [[Number]] | |r1= [[Number]] | ||
Latest revision as of 21:58, 1 November 2025
Description
- 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).
- Groups:
- GUI ControlStrings
Syntax
- Syntax:
- text getTextWidth [font, size]
- Parameters:
- text: String - text string to measure
- font: String - (Optional since
2.22, default "" (system font)) font type - size: Number - (Optional since
2.22, default -1 (system font size)) font size - Return Value:
- Number
Examples
- Example 1:
- Example 2:
- Example 3:
- #define MARGINS 0.016 private _ctrl = findDisplay 46 ctrlCreate ["RscText", -1]; _ctrl ctrlSetText "TestString"; _ctrl ctrlSetFont "PuristaMedium"; _ctrl ctrlSetFontHeight 0.04; ctrlTextWidth _ctrl == ("TestString" getTextWidth ["PuristaMedium", 0.04]) + MARGINS; // true
Additional Information
- See also:
- ctrlTextWidth
Notes
-
Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note
- Posted on Oct 28, 2020 - 22:10 (UTC)
-
When working with multiline controls ctrlTextWidth or getTextWidth will not return a proper value since the text will be treated as one long line.
Solution (Execute in Eden Editor):disableSerialization; private _display = findDisplay 313 createDisplay "RscDisplayEmpty"; private _edit = _display ctrlCreate ["RscEditMulti", -1]; _edit ctrlSetPosition [0,0,1,1]; _edit ctrlSetBackgroundColor [0,0,0,1]; _edit ctrlSetFont "EtelkaMonospacePro"; _edit ctrlSetFontHeight 0.03; _edit ctrlCommit 0; private _longestLineWidth = 0; private _exampleString = "abcdefghijklmnopqrstuvxyz0134567890"; private _text = ""; for "_lines" from 1 to 400 do // loop through all lines and find the longest one { _lineText = _exampleString select [round random 35]; _longestLineWidth = _longestLineWidth max (_lineText getTextWidth ["EtelkaMonospacePro", 0.03]); _text = _text + _lineText + endl; }; _edit ctrlSetPositionW (_longestLineWidth + 2 * 0.008); // width of longest line + left and right margin set in engine _edit ctrlCommit 0; _edit ctrlSetText _text;