Make lines of text have equal length

前端 未结 4 1042
刺人心
刺人心 2021-02-07 15:44

In centered h1 elements, if the text falls on multiple lines, line breaks make the text look like this:

                This is a header that takes          


        
相关标签:
4条回答
  • 2021-02-07 16:24

    Late to this party, but here's my approach. I get the initial element height (any elements with the class balance_lines, in the code below), then incrementally shrink the width of the element. Once the height of the element changes, I've gone too far. The step before that should have lovely roughly-equal line lengths.

    $('.balance_lines').each(function(){
        var currentHeight = $(this).height();
        var thisHeight = currentHeight;
        var currentWidth = $(this).width();
        var newWidth = currentWidth;
        // Try shrinking width until height changes
        while (thisHeight == currentHeight) {
            var testWidth = newWidth - 10;
            $(this).width(testWidth);
            thisHeight = $(this).height();
            if (thisHeight == currentHeight) {
                newWidth = testWidth;
            } else {
                break;
            }
        }
        $(this).width(newWidth);
    });
    

    You can see this code in action on the homepage at apollopad.com.

    0 讨论(0)
  • 2021-02-07 16:31

    Late to this party, but here's my approach. I get the initial element height (any elements with the class balance_lines, in the code below), then incrementally shrink the width of the element. Once the height of the element changes, I've gone too far. The step before that should have lovely roughly-equal line lengths

    0 讨论(0)
  • 2021-02-07 16:42

    I would solve it using only strict JavaScript, going this way:

    1. put a class named 'truncate' to h1 tags you want to break
    2. configure the javascript code on your needs knowing that

  • MAXCOUNT: (integer) max chars counted per line
  • COUNT_SPACES: (boolean) white spaces must be counted?
  • COUNT_PUNCTUATION: (boolean) punctuation must be counted?
  • EXACT: (boolean) the last word must be cut?
  • BLOCKS_CLASS: (string) the className of the h1 to consider

    I wrote the code very quickly so it must be better tested for bugs, but it can be a starting point I think.

    I'm not using jQuery in this code to keep the code light and to avoid dependencies.
    I think I'm using all cross-browser commands (cannot test it I've got only linux now). However any correction for cross-browser compatibility task (included the use of jQUery if requested) might be easy.

    Here is the code:

    <html>
    
    <head>
        <style>
            h1 {background-color: yellow;}
            #hiddenDiv {background-color: yellow; display: table-cell; visibility:hidden;}
        </style>
        <script>
    
            var MAXCOUNT            = 20;
            var COUNT_SPACES        = false;
            var EXACT               = false;
            var COUNT_PUNCTUATION   = true;
            var BLOCKS_CLASS        = 'truncate';
    
            window.onload = function () 
            {
                var hidden = document.getElementById('hiddenDiv');
    
                if (hidden == null)
                {
                    hidden = document.createElement('div');
                    hidden.id = 'hiddenDiv';
                    document.body.appendChild(hidden);
                }
    
                var blocks = document.getElementsByClassName(BLOCKS_CLASS);     
    
                for (var i=0; i<blocks.length; i++)
                {
                    var block           = blocks[i];
                    var text            = block.innerHTML;
                    var truncate        = '';
                    var html_tag        = false;
                    var special_char    = false;
                    maxcount            = MAXCOUNT;
                    for (var x=0; x<maxcount; x++)
                    {
                        var previous_char = (x>0) ? text.charAt(x-1) : '';
                        var current_char = text.charAt(x);
    
                        // Closing HTML tags
                        if (current_char == '>' && html_tag)
                        {
                            html_tag = false;
                            maxcount++;
                            continue;
                        }
                        // Closing special chars
                        if (current_char == ';' && special_char)
                        {
                            special_char = false;
                            maxcount++;
                            continue;
                        }
                        // Jumping HTML tags
                        if (html_tag)
                        {
                            maxcount++;
                            continue;
                        }
                        // Jumping special chars
                        if (special_char)
                        {
                            maxcount++;
                            continue;
                        }
                        // Checking for HTML tags
                        if (current_char == '<')
                        {
                            var next = text.substring(x,text.indexOf('>')+1);
                            var regex = /(^<\w+[^>]*>$)/gi;
                            var matches = regex.exec(next); 
                            if (matches[0])
                            {
                                html_tag = true;
                                maxcount++;
                                continue;
                            }                       
                        }
                        // Checking for special chars
                        if (current_char == '&')
                        {
                            var next = text.substring(x,text.indexOf(';')+1);
                            var regex = /(^&#{0,1}[0-9a-z]+;$)/gi;
                            var matches = regex.exec(next);
                            if (matches[0])
                            {
                                special_char = true;
                                maxcount++;
                                continue;
                            }
                        }                   
                        // Shrink multiple white spaces into a single white space
                        if (current_char == ' ' && previous_char == ' ')
                        {
                            maxcount++;
                            continue;
                        }
                        // Jump new lines
                        if (current_char.match(/\n/))
                        {
                            maxcount++;
                            continue;
                        }                   
                        if (current_char == ' ')
                        {
                            // End of the last word
                            if (x == maxcount-1 && !EXACT) { break; }
                            // Must I count white spaces?
                            if ( !COUNT_SPACES ) { maxcount++; }
                        }
                        // Must I count punctuation?
                        if (current_char.match(/\W/) && current_char != ' ' && !COUNT_PUNCTUATION)
                        {
                            maxcount++;
                        }
                        // Adding this char
                        truncate += current_char;
                        // Must I cut exactly?
                        if (!EXACT && x == maxcount-1) { maxcount++; }
                    }
    
                    hidden.innerHTML = '<h1><nobr>'+truncate+'</nobr></h1>';
    
                    block.style.width = hidden.offsetWidth+"px";
                }
            }
    
        </script>
    </head>
    
    
    <body>
    
    <center>
        <h1 class="truncate">
            This is a header that
            takes up two lines
        </h1>
    
        <br>
    
        <h1 class="truncate">
            This is a header that takes 
            up three lines because it
            is really, really long
        </h1>
    
        <br>
    
        <h1>
            This is a header pretty short
            or pretty long ... still undecided
            which in any case is not truncated!
        </h1>
    </center>
    
    </body>
    
    </html>
    

    And here is a demo of that: http://jsfiddle.net/6rtdF/

0 讨论(0)
  • 2021-02-07 16:47

    The CSS Text 4 draft proposes text-wrap: balance, but I don't think any browser implements it yet.

    In the meantime, you can use Adobe's jQuery plugin (demo): https://github.com/adobe-webplatform/balance-text

    0 讨论(0)
  • 提交回复
    热议问题