You are not logged in.
Pages: 1
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.
/// @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
When argument[1] would be smaller than (string first letter + "...") width, then returned string would be always too wide.
Offline
Agreed, in which case it returns "..."
Offline
Pages: 1