如何跟踪单词换行时的行数?

发布于 2024-09-28 09:17:02 字数 146 浏览 0 评论 0原文

有一个要在 div 中输出的文本,使用非固定间距字体打印。此 div 高度仅允许打印 4 行文本。是否可以跟踪自动浏览器自动换行,以便我可以在文本达到 4 行时找到文本位置并剪切下一行? 有什么建议使用 Javascript 操作 DOM 或使用 PHP 帮助计算文本行长度吗?

Have a text to output in div printed with not fixed-pitch font. This div height permit to print only 4 lines of text. Is it possible to track automatic browser word wrap, so I can find position in text, when it has reached 4 lines and to cut away next lines?
Any suggestions using Javascript manipulating with DOM or maybe text line length calculation with PHP help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云之铃。 2024-10-05 09:17:02

使用 overflow:hidden 怎么样?

假设您的 font-size 为 12px,line-height 为 16px。

4 lines * 16px = 64px

.hide-extra-lines {
    height: 64px;
    overflow: hidden;
}

这就是你所追求的吗?

How about using overflow: hidden?

Say you had a font-size of 12px, and a line-height of 16px.

4 lines * 16px = 64px

.hide-extra-lines {
    height: 64px;
    overflow: hidden;
}

Is this what you're after?

只为守护你 2024-10-05 09:17:02

1)您可以在服务器上测量文本的大小并且相当准确。我已经在 Windows 机器上使用 GDI+ 完成了此操作;不确定您可以访问哪些操作系统或库。

2) 您可以在客户端上使用等宽字体,并根据容器的宽度和字长检测文本何时换行。然后,您可以截断文本。

3)您不能在客户端使用等宽字体,仍然使用脚本进行猜测,并使用overflow:hidden捕获任何溢出的文本。

1) You can measure the size of the text on the server and be reasonably accurate. I've done this with GDI+ on a Windows machine; not sure what OS or libraries you have access to.

2) You can use a monospace font on the client and detect when text will wrap based on width of the container and word length. Then, you can truncate the text.

3) You can not use a monospace font on the client, still make a guess using script, and use overflow: hidden to capture any text that overflows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文