Not without a lot of trial and error.
Here's something simple I've had for years.
//******************************************************************************** // JavaScript String Extensions //******************************************************************************** // String extensions for wordwrap + trimming //String.wordWrap(maxLength: Integer, [breakWith: String = "\n"], [cutWords: Boolean = false]): String //Returns an string with the extra characters/words "broken". //maxLengthmaximum amount of characters per line //breakWithstring that will be added whenever it's needed to break the line //cutWordsif true, the words will be cut, so the line will have exactly "maxLength" characters, otherwise the words won't be cut // String.prototype.wordWrap = function(m, b, c) { var i, j, s, r = this.split("\n"); if(m > 0) { for(i in r) { for(s = r[i], r[i] = ""; s.length > m; j = c ? m : (j = s.substr(0, m).match(/\S*$/)).input.length - j[0].length || m,r[i] += s.substr(0, j) + ((s = s.substr(j)).length ? b : "")); r[i] += s; } } return r.join("\n"); };
String.prototype.wordWrapIntoArray = function(m, b, c) { var i, j, s, r = this.split("\n"); if(m > 0) { for(i in r) { for(s = r[i], r[i] = ""; s.length > m; j = c ? m : (j = s.substr(0, m).match(/\S*$/)).input.length - j[0].length || m,r[i] += s.substr(0, j) + ((s = s.substr(j)).length ? b : "")); r[i] += s; } } return r; };
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,""); };
String.prototype.ltrim = function() { return this.replace(/^\s+/,""); };
String.prototype.rtrim = function() { return this.replace(/\s+$/,""); }; //******************************************************************************** // JavaScript String Extensions //********************************************************************************
Hope this will get you started. To really do this right, you need to examine the textWidth/textHeight required and adjust text accordingly which is much more complex than what I posted above.
|