简体   繁体   English

如何根据字符串长度更改字体大小?

[英]How to make font-size change depending on string length?

I have a table: 我有一张桌子:

<table width="200px">
   <tr>
     <td>
       <font style="font-size:14px;"><?php echo $text; ?></font>
     </td>
    </tr>
 </table>

The $text variable is dynamic, and might be 3 characters long, or 50 characters long. $ text变量是动态的,长度可以为3个字符或50个字符。

The problem is the width of the table column is limited to 200px. 问题是表格列的宽度限制为200px。

How can I make the text always fit on one line, and preserve the limited width of the table? 如何使文本始终适合一行,并保留表格的有限宽度?

I am looking for something like a dynamic font-size function? 我在寻找类似动态字体大小的功能吗? Or any other (good/easy) way? 还是任何其他(好/容易)方式?

Thanks 谢谢

如果您使用的是jQuery,则自动调整大小的动态文本以填充固定大小的容器中的解决方案似乎将为您工作。

Using Mootools you can basically measure an Element size. 使用Mootools,您基本上可以测量元素大小。

I think that if you create a dummy element in the DOM (positioned outside the screen view) containing the text you need to insert. 我认为,如果您在DOM中(位于屏幕视图外部)创建一个虚拟元素,其中包含需要插入的文本。 Then you can cycle the font size until you find the dimension that fit you're table width. 然后,您可以循环更改字体大小,直到找到适合表格宽度的尺寸。

A little bit dirty ... 有点脏...

EDITED: same approach, but in jquery, is used by miket2e answer link 编辑:相同的方法,但在miquery2e答案链接中使用的jquery

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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