我有一个字符串,必须适合一个盒子,并且长度最多为 3 行。为了缩短它,我计划截断它并以“...”结尾。我可以将其缩短为一定数量的字符,但如果我用“wwwwwwwww [...] wwww”使其看起来不错,那么用“iiiiiiiiiiii [...] iiii”看起来就不正确。
有没有什么方法可以缩短字符串占用的空间,而不是不使用固定宽度字体的字符串中的字符数?理想情况下,我想在服务器端(php)执行此操作,但认识到实际的字符宽度内容更有可能在客户端(JS / jQuery)
Mala
ps 中可行。请不要“简单地创建一个‘...’的图像并将其覆盖在行尾”黑客或类似的 - 我实际上想将字符串缩短到适当的长度
I have a string that must fit into a box, and must be at most 3 lines long. To shorten it, I plan to truncate it and end it with '...'. I could shorten it to a certain # of characters but if i make it look good with "wwwwwwwww [...] wwww" it won't look right with "iiiiiiiiiii [...] iiii".
Is there some way I can shorten it by how much space the string would take up, as opposed to how many characters there are in a string without using a fixed-width font? Ideally I'd like to do this server-side (php) but recognize that actual character width stuff is far more likely to be feasible client-side (JS / jQuery)
Mala
ps. Please no "simply create an image of '...' and overlay it over the end of the line" hacks or similar - I actually want to shorten the string to the appropriate length
发布评论
评论(5)
有 imageftbbox 和 imagettfbbox 为此。
There's imageftbbox and imagettfbbox for that.
看看这个: http://www. switchonthecode.com/tutorials/javascript-tutorial-how-to-auto-ellipse-text
have a look at this: http://www.switchonthecode.com/tutorials/javascript-tutorial-how-to-auto-ellipse-text
此网站提供和任何方法一样好,并且是跨浏览器的。我喜欢简单:
This site offers as good a way as any, and is cross-browser. I like the simplicity:
我想不出有什么方法可以轻松地将字体大小设置为您所说的程度,但可能有一种方法可以“以您想要的方式显示”。我假设既然您提到了三行,我们正在讨论
虽然文本在那里,但它不可见,并且滚动条不会显示。
There's no way that I can think of to easily get font sizes to the degree that you speak of, but there may be a hack that would "appear" the way you want. I assume since you mention three lines, we're talking about a
<textarea>
element:Although the text is there, it's not visible and the scroll bars would not display.
有许多 jQuery 插件可以实现此目的: http://plugins.jquery.com/taxonomy/期限/2229
There is a number of jQuery plugins for this: http://plugins.jquery.com/taxonomy/term/2229