wrapping text words in new line

后端 未结 8 740
眼角桃花
眼角桃花 2021-02-10 04:53

I\'m using the below code for wrapping long text, entered by users in a text area for commenting:

function addNewlines(comments) {
  var result = \'\';
  while (         


        
8条回答
  •  旧巷少年郎
    2021-02-10 05:25

    I had the same issue, I solved it using the below function.

    function wordWrap(message, lineSize, breakPoint){
        let wordsArr = message.split(" "),
            wordsLen = wordsArr.length,
            finalMsg = "",
            linesArr = [""],
            currLine = 0;
    
        for(let i=0; i< wordsLen; i++){
            if(linesArr[currLine].length + wordsArr[i].length > lineSize){
                currLine +=1;
                linesArr[currLine] = wordsArr[i] + " ";
            } else {
                linesArr[currLine] += wordsArr[i] + " ";
            }
        }
        let linesLen = linesArr.length;
        for(let i=0; i

    Hope this helps.

提交回复
热议问题