Created
September 18, 2013 09:28
-
-
Save nazywamsiepawel/6606745 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function wordWrap(text){ | |
var words = text.split(' '); | |
var charWidth = 6; // and then suddenly a charWidth appeared. | |
var horizotalLimit = 200; | |
var lines = []; | |
var currentLine = 0; | |
lines.push({line : '', text_length : 0}); | |
var i = 0; | |
while(i < words.length){ | |
currentLine = lines.length-1; | |
if(words[i].length*charWidth >= horizotalLimit){ | |
var longWord = {line : words[i], length:words[i].length*charWidth}; | |
/*handling very long ugly german words*/ | |
if(lines[currentLine].length){ | |
lines.push(longWord); | |
} else { | |
lines[currentLine] = longWord; | |
} | |
i++; | |
} else if((words[i].length * charWidth)+lines[currentLine].text_length < horizotalLimit){ | |
lines[currentLine].line += ' ' + words[i]; | |
lines[currentLine].text_length = lines[currentLine].line.length * charWidth; | |
i++; | |
} else { | |
lines.push({line : '', text_length : 0}); | |
} | |
} | |
/* prepare for multiline */ | |
console.log(lines); | |
var finalLines = []; | |
for(var i = 0 ; i<lines.length; i++){ | |
finalLines.push(lines[i].line.trim()); | |
} | |
return finalLines; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment