GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2016-12-22 13:02:59

Tsa05
Member
Registered: 2016-11-16
Posts: 13

string_truncate

For cases when you want to show a string within a specific area (like a button), but keep the size constrained.
You supply the string and size; this supplies a portion of your string plus "..."

If your desired width is smaller than "..." then I can't help you.

Expand/// @desc string_truncate(string, width);
/*
*   Shortens a string and appends an ellipsis if the string is
*   wider than width. Resulting string fits within width.
*/
///   @arg {string} string The string to truncate
///   @arg {real}   width  The width to fit within
/*  
*	Returns:
*   Truncated string
*/

var txt = argument[0];
var extra = 0;
while(string_width(txt)>argument[1]-extra && string_length(txt)>1){
    extra = string_width("...");
    txt = string_copy(txt,1,string_length(txt)-1);
}
if(extra) txt+="...";
return txt;

Last edited by Tsa05 (2016-12-22 13:03:34)

Offline

#2 2017-01-13 07:24:50

gnysek
Member
Registered: 2011-12-29
Posts: 36

Re: string_truncate

When argument[1] would be smaller than (string first letter + "...") width, then returned string would be always too wide.

Offline

#3 2017-01-21 11:12:02

Tsa05
Member
Registered: 2016-11-16
Posts: 13

Re: string_truncate

Agreed, in which case it returns "..."

Offline

Board footer

Powered by FluxBB