getTextWidth: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "(\| * )+\}\}" to "}}")
m (Text replacement - "↵↵</sqf>" to " </sqf>")
 
(68 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Command
{{RV|type=command


| arma3
|game1= arma3
 
|version1= 1.98
|1.98


|gr1= GUI Control
|gr1= GUI Control
Line 9: Line 8:
|gr2= Strings
|gr2= Strings


| Returns estimated width of the text based on font type and size. Similar to [[ctrlTextWidth]] but doesn't 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).<br/>
|descr= Returns estimated width of the text based on [[FXY_File_Format#Available_Fonts|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).
Always uses unicode (see [[forceUnicode]]).
{{Feature|informative|This command always uses unicode (see also [[forceUnicode]]).}}


| text [[getTextWidth]] [font, size]
|s1= text [[getTextWidth]] [font, size]


|p1= text: [[String]] - text string to measure
|p1= text: [[String]] - text string to measure
|p2= [font, size]: [[Array]]
|p3= font: [[String]] - font type
|p4= size: [[Number]] - font size


| [[Number]]
|p2= font: [[String]] - [[FXY_File_Format#Available_Fonts|font type]]
 
|p3= size: [[Number]] - font size


|r1= [[Number]]


|x1= <code>_textWidth = "Hello World" [[getTextWidth]] ["PuristaMedium", 0.03];</code>
|x1= <sqf>_textWidth = "Hello World" getTextWidth ["PuristaMedium", 0.03];</sqf>
|x2= <code>_textWidthInclMargins = ("Hello World" [[getTextWidth]] ["PuristaMedium", 0.03]) + 0.016;</code>
|x3= <code>[[private]] _ctrl = [[findDisplay]] 46 [[ctrlCreate]] ["RscText", -1];
_ctrl [[ctrlSetText]] "TestString";
_ctrl [[ctrlSetFont]] "PuristaMedium";
_ctrl [[ctrlSetFontHeight]] 0.04;
[[ctrlTextWidth]] _ctrl == ("TestString" [[getTextWidth]] ["PuristaMedium", 0.04]) + 0.016; {{cc | true}}</code>


|x2= <sqf>_textWidthInclMargins = ("Hello World" getTextWidth ["PuristaMedium", 0.03]) + 0.016;</sqf>


| [[ctrlTextWidth]]
|x3= <sqf>
#define MARGINS 0.0016
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
</sqf>


|seealso= [[ctrlTextWidth]]
}}
}}


{{Note
|user= R3vo
|timestamp= 20201028221000
|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.<br>
'''Solution (Execute in [[:Category:Eden Editor|Eden Editor]]):'''
<sqf>
disableSerialization;


[[Category:Scripting Commands|{{uc:{{PAGENAME}}}}]]
private _display = findDisplay 313 createDisplay "RscDisplayEmpty";
{{GameCategory|arma3|New Scripting Commands}}
{{GameCategory|arma3|Scripting Commands}}


<!-- CONTINUE Notes -->
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;


<!-- DISCONTINUE Notes -->
private _longestLineWidth = 0;
private _exampleString = "abcdefghijklmnopqrstuvxyz0134567890";
private _text = "";


<!-- CONTINUE Notes -->
for "_lines" from 1 to 400 do // loop through all lines and find the longest one
<dl class="command_description">
{
<dd class="notedate">Posted on October 28, 2020 - 22:10 (UTC)</dd>
_lineText = _exampleString select [round random 35];
<dt class="note">[[User:R3vo|R3vo]]</dt>
_longestLineWidth = _longestLineWidth max (_lineText getTextWidth ["EtelkaMonospacePro", 0.03]);
<dd class="note">
_text = _text + _lineText + endl;
When working with multiline controls [[ctrlTextWidth]] or [[getTextWidth]] will not return a proper value since the text will be treated as one long line.</br>
};


'''Solution (Execute in [[Eden Editor]]):'''
_edit ctrlSetPositionW (_longestLineWidth + 2 * 0.008); // width of longest line + left and right margin set in engine
 
_edit ctrlCommit 0;
<code> [[disableSerialization]];
_edit ctrlSetText _text;
 
</sqf>
[[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]] {{cc|Loop through all lines and find the longest one}}
{
    _lineText {{=}} _exampleString [[select]] <nowiki>[</nowiki>[[round]] [[random]] 35];
    _longestLineWidth {{=}} _longestLineWidth [[max]] (_lineText [[getTextWidth]] ["EtelkaMonospacePro", 0.03]);
    _text {{=}} _text [[+]] _lineText [[+]] [[endl]];
};
_edit [[ctrlSetPositionW]] (_longestLineWidth [[+]] 2 * 0.008); {{cc|Width of longest line + left and right margin set in engine}}
_edit [[ctrlCommit]] 0;
_edit [[ctrlSetText]] _text;</code>
</dd>
</dl>
<!-- DISCONTINUE Notes -->

Latest revision as of 19:48, 3 September 2024

Hover & click on the images for description

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).
This command always uses unicode (see also forceUnicode).
Groups:
GUI ControlStrings

Syntax

Syntax:
text getTextWidth [font, size]
Parameters:
text: String - text string to measure
font: String - font type
size: Number - font size
Return Value:
Number

Examples

Example 1:
_textWidth = "Hello World" getTextWidth ["PuristaMedium", 0.03];
Example 2:
_textWidthInclMargins = ("Hello World" getTextWidth ["PuristaMedium", 0.03]) + 0.016;
Example 3:
#define MARGINS 0.0016 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
R3vo - c
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;