将字符限制为特定宽度 [英] Limiting Characters to a specfic width
问题描述
我已经遇到过几次这个问题.将字符串中的字符限制为特定数字我没有问题.但是,某些字符比其他字符长,因此它会换行或使我的DIV元素更宽,并且与我网站的其余部分不一致.
I have run into this problem a few times. I have no problem limiting characters in a string to a specific number. However, some characters are longer than others so it wraps to another line or makes my DIV element wider and not uniform with the rest of my website.
例如:
Literacy - Is it the Sa
Machu Picchu, Cuzco, Pe
包括空格在内的字符数(23)完全相同,但马丘比丘(Machu Pichu)字符在屏幕上的实际宽度更长.
are the exactly the same amount of characters (23) including spaces but the Machu Pichu one is longer in terms of the actual width on screen.
是否有任何方法可以使字符串的大小基于实际字符串的宽度(而不是字符数)?有人必须为此提出解决方案,对吗?
Is there any way to have a uniform size for a string that is based on the width of the actual string as opposed to the number of characters? Someone has had to have come up with a solution to this before right?
推荐答案
- 第一个(显而易见的)解决方案:切换到固定宽度的字体,例如Courier,Lucida Console,Consolas等.
- 第二个解决方案:使用 GD库来
- First (obvious) solution: switch to a fixed-width font such as Courier, Lucida Console, Consolas, etc.
- Second solution: use the GD library to write strings to a graphic object and measure that object.
这篇关于将字符限制为特定宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!