Jump to content
Sign in to follow this  
Alwarren

measure the on-screen size of a string

Recommended Posts

Hey.

I am facing a problem where I need, for layouting purposes, the length of a text string in pixels. I have a CT_STATIC control and want to resize is to such a point that the whole text fits without clipping it, but I cannot seem to find a way to do this.

Anyone got an idea? I've seen there is a "ctrlTextHeight" which returns the height of a structured text, but a version that returns the width doesn't seem to exist.

Help is very much appreciated!

~~ Alwarren

Share this post


Link to post
Share on other sites

Off the too of my head, this would be difficult of not impossible to do in a simple manner, purely and simply because of the different screen ratios and resolutions people will be using.

One potential method would be to determine how many characters the string holds, determine the current font size, find the x and y screen resolutions, determine the screen ratio, then calculate how many pixels it's using. Consider also, whether the user has chosen different sized UI elements.

Tricky problem.

Share this post


Link to post
Share on other sites

Well, I sort of found a solution for the problem:

/* * Get the length of a text in screen estate
*
* Parameters:
* _this select 0: Text
* _this select 1: (Optional) Font size
* _this select 2: (Optional) Font
*/
FHQ_Ticker_PixelLength = {
private ["_display", "_text", "_ctrl", "_size", "_font", "_targetHeight", "_realHeight", "_w", "_scale"];
_text = [_this, 0, "tick", [""]] call BIS_fnc_param;
_size = [_this, 1, (((((safezoneW / safezoneH) min 1.2) / 1.2) / 25) * 1), [0]] call BIS_fnc_param;
_font = [_this, 2, "PuristaMedium",  [""]] call BIS_fnc_param;


_scale = _size / 0.02;

disableSerialization;
_display = uinamespace getvariable "FHQ_title";
_ctrl =  (_display displayCtrl 999);
_ctrl ctrlSetPosition [0,0, safeZoneW, safeZoneH];
_ctrl ctrlShow false;
_ctrl ctrlSetStructuredText (parseText format ["<t size=""%1"">%2</t>", _scale, _text]);
_ctrl ctrlSetFont _font;
_ctrl ctrlSetFontHeight _size;
_ctrl ctrlCommit 0;

_targetHeight = ctrlTextHeight _ctrl;
_realHeight = 2;
_ctrl ctrlSetPosition [0,0, _size, safeZoneH];
_ctrl ctrlCommit 0;

_w = _size;

while {_realHeight != _targetHeight} do {
	_ctrl ctrlSetPosition [0,0,_w, 1];
	_ctrl ctrlCommit 0;
	_realHeight = ctrlTextHeight _ctrl;
	_w = _w + (_size / 2);
};

_w
};

This has a hidden control (idc 999) that is a structuredText. I have it on fullscreen and check how high the text is, then resize it to zero and keep resizing it until the height matches the "optimal" height. It's not pretty, but it works.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×