使文本行具有相等的长度 [英] Make lines of text have equal length

查看:22
本文介绍了使文本行具有相等的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在居中的 h1 元素中,如果文本落在多行上,换行符会使文本看起来像这样:

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 up two
                              lines

                This is a header that takes up three
                lines because it is really, really
                              long

有没有办法操纵这些元素,使文本行的长度大致相等?像这样:

Is there a way to manipulate these elements so that the length of the lines of text is roughly equal? Like this:

                       This is a header that
                        takes up two lines

                    This is a header that takes 
                     up three lines because it
                       is really, really long

jQuery 插件 Widow Fix 可防止单字行,但我正在寻找可以平衡的东西all 多行元素中的行.是否有任何用于此的 jQuery 插件,或者您能推荐一个策略吗?

The jQuery plugin Widow Fix prevents single-word lines, but I'm looking for something that evens out all the lines in a multi-line element. Are there any jQuery plugins for this, or can you recommend a strategy?

推荐答案

我会只使用严格的 JavaScript 来解决它,方法如下:

1. 将一个名为 'truncate' 的类放入您想要破坏的 h1 标签
2.根据您的需要配置javascript代码知道

  • MAXCOUNT:(整数)每行计数的最大字符数
  • COUNT_SPACES:必须计算(布尔)空格?
  • COUNT_PUNCTUATION:必须计算(布尔)标点符号?
  • EXACT:(布尔值)最后一个字必须删掉?
  • BLOCKS_CLASS:(字符串)要考虑的 h1 的类名

    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

    我写的代码很快,所以必须更好地测试错误,但我认为这可以是一个起点.

    我没有在这段代码中使用 jQuery 来保持代码简洁并避免依赖.
    我想我正在使用所有跨浏览器命令(无法测试,我现在只有 linux).然而,对跨浏览器兼容性任务的任何更正(如果需要,包括使用 jQUEry)可能很容易.

    代码如下:

    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>
    

    这是一个演示:http://jsfiddle.net/6rtdF/

    这篇关于使文本行具有相等的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆