简体   繁体   中英

How to cut the string in javascript so that it fits exactly two lines & add … at the end

I have a requirement where in i have to display the text for two lines & add ... if its overflowing My div Width is fixed.(Even height can be considered as fixed).

Actual text looks like this:

1

Lorem Ipsum is simply
dummy text of the printing
and typesetting industry.

2

Lorem Ipsum is simply text
of the printing and types
industry.

Expected:

1

Lorem Ipsum is simply
dummy text of the print...

2

Lorem Ipsum is simply text
of the printing and typ...

I do not want to overload with plugins(three dots jquery plugin does that).

I'm planning to do just by cutting(split) the string as div width is fixed.

Thanks in Advance.

UPDATE: Looks like text-overflow is only useful for horizontal truncation.

Here's a little jQuery that should work.

Try it out: http://jsfiddle.net/UfxYQ/

var $container = $('#container');
var $text = $('#text');
var originalText = $text.text();
var temp = originalText;

if( $container.outerHeight() < $text.outerHeight() ) {

    while($container.outerHeight() < $text.outerHeight()) {
        $text.text( temp = temp.substr(0, temp.length-1) );
    }
    $text.text( temp = temp.substr(0, temp.length-3) );
    $text.append('...');
}

HTML

<div id='container'>
    <span id='text'>Lorem Ipsum is simply
        dummy text of the printing
        and typesetting industry.
    </span>
</div>​

CSS

#container {
    width: 150px;
    height: 50px;
    line-height: 25px;
    position: relative;
}

You won't have full cross browser support, but close. There's a CSS property called text-overflow that is supported in IE6+, Safari, Konqueror, and Opera (with an Opera specific property).

 #myOverflowDiv { text-overflow: ellipsis; -o-text-overflow: ellipsis; } 

http://www.quirksmode.org/css/textoverflow.html

http://www.quirksmode.org/css/contents.html#t413

The CSS text-overflow property is the nicest way, but it's not supported by all browsers yet. If you want to use Javascript, you could create an offscreen element. Use CSS (generated by the script) to give it the same width and font as your target, with a height of '2em' (ie 2 lines). Run a loop, adding the text into it one letter at a time, until either you run out of letters or the height changes. When the height increases you know you've exceeded two lines.

Of course you need to account for the width of the "..." as well, so you might append it to the letter you're about to add.

Be sure the element is offscreen rather than using "display: none", as non-displayed elements have no height. "visibility: hidden" might work; I haven't tried.

Just an addition for post of user113716. Cleaner approach of doing this:

var $container = $('#container');
var $text = $('#text');
var temp = $text.text();

if ($container.height() < $text.outerHeight()) {
  while($container.height() < $text.outerHeight()) {
    temp = temp.substr(0, temp.length-1)
    $text.text(temp + '...');
  }
}

http://jsfiddle.net/YR7F2/

A neat way to do it would be with a little jQuery plugin...

$.fn.trimToParentHeight = function(options) {
    options = $.extend($.fn.trimToParentHeight.defaults, options);

    return this.each(function(index, text) {
        text = $(text);
        var height = parseInt(text.parent().height());
        var temp = text.text();
        while(parseInt(text.outerHeight()) > height && temp.length>0) {
            temp = temp.substr(0, temp.length-1)
            text.text(temp +  options.ellipsis);
        }
    });
}

$.fn.trimToParentHeight.defaults = {
    ellipsis: '...'
};

Then you can just do...

$(".container .text").trimToParentHeight()

... as long as the container is the direct parent of the text.

string="test/firstpage/secondpage/thirdpage";

string.split("/").slice(0, 1);

Out put will be : test

As the overflow ellipsis css doesn't work for multiline text you have to do it with JavaScript. You could copy the text box, position it absolute with top -1000000px and cut the last letter of the text until the box has the right height. Or you simple cut after a specific letter count this will be faster but not accurate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM