Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getLinesText(text) {
- var
- countLines = 1,
- maxLineLength = 10,
- words = text.split(' ');
- var lineBuffer = '', lineLength = 0, lines = [];
- for (var w in words) {
- var word = words[w];
- if ( (lineLength+word.length+1) > maxLineLength ) { // 1 - пробел
- if( lineBuffer.length > 0 ) { lines.push(lineBuffer); }
- lineBuffer = word;
- lineLength = word.length;
- } else {
- if (lineLength > 0) {
- lineLength += 1; lineBuffer += ' '; // добавляем пробел, если слово не первое
- }
- lineLength += word.length;
- lineBuffer += word;
- }
- // если это последнее слово, отправляем в буффер
- if ( w == (words.length -1) ) { lines.push(lineBuffer) };
- }
- // находим самую длинную строку
- var maxLineLength = 0, outText = '';
- for(var l in lines) {
- var
- line = lines[l],
- len = line.length;
- if ( len > maxLineLength) {
- maxLineLength = len;
- }
- if ( l > 0 ) { outText += '<br>' };
- outText += line;
- }
- return {
- countLines: lines.length,
- maxLineLenght: maxLineLength,
- text: outText
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement