简体   繁体   English

“最适合”字体大小 - 如何测量句子宽度?

[英]“Best fit” font size - how do I measure a sentence width?

I'm coming up against a common problem with customised content management system design. 我遇到了定制内容管理系统设计的常见问题。

I run a small web design company and I often have to make a certain area of a website editable for the customer. 我经营一家小型网页设计公司,我经常要为客户制作一个可编辑的网站区域。 For example, there may be a bar along the top of the page advertising the latest news or deals which the customer wants to be able to edit themselves. 例如,页面顶部可能有一个栏,用于宣传客户希望自己编辑的最新新闻或交易。

The problem I'm having is that they often write too much and the text overflows. 我遇到的问题是他们经常写得太多,文字溢出。 Maybe I'm being a bit too defensive, but in these situations I usually privately blame the customer - surely they check after updating the text, realise it doesn't fit, and modify it so that it does? 也许我有点过于防守,但在这些情况下,我通常私下责怪客户 - 他们肯定会在更新文本后检查,发现它不合适,并修改它以便它做到了吗?

Anyway, I guess it's my responsibility to make the site look nice so I've been trying to find a way of making text fit in a fixed size box. 无论如何,我想我的责任是让网站看起来不错,所以我一直试图找到一种方法让文字适合固定大小的盒子。 One way is to give the box (usually a div ) this css property: 一种方法是给这个css属性框(通常是div ):

overflow: auto;

but sometimes this isn't suitable, such as in the example described above where the bar is only one line high. 但有时这不合适,例如在上面描述的例子中,条只有一行高。

My question (yes I've finally got to it) is how can I make the font change size automatically so that it fits in the box? 我的问题(是的,我终于得到了它)是如何自动更改字体大小以使其适合框?

In some applications (Powerpoint being one example), you can choose a "best fit" option for font size and the application chooses a size which allows the text to fit exactly. 在某些应用程序中(Powerpoint就是一个例子),您可以为字体大小选择“最适合”选项,并且应用程序选择允许文本准确拟合的大小。 I'm basically looking for either a CSS, JavaScript or PHP version of this. 我基本上都在寻找CSS,JavaScript或PHP版本。

Thanks in advance! 提前致谢!

EDIT: Here's the solution - http://jsfiddle.net/Cnkfn/1/ 编辑:这是解决方案 - http://jsfiddle.net/Cnkfn/1/

I feel your pain! 我感觉到你的痛苦! We also have clients who give us abstracts which are too long for the allotted area; 我们也有客户为我们提供的摘要对于分配的区域来说太长了; for some clients, it apparently doesn't even cross their mind that their text might be too long. 对于一些客户来说,显然他们甚至没有想到他们的文字可能太长了。 :-) :-)

What I'd do in your case is something like what stackoverflow does: show a box below the input area that shows their text updating onkeyup. 我在你的情况下做的是像stackoverflow那样:在输入区域下面显示一个框,显示他们的文本更新onkeyup。 With a fixed-height box, it'll be immediately obvious to them when their text is too long. 对于固定高度的盒子,当文本太长时,它们会立即变得明显。 In lieu of that, you could try something like the following: 取而代之的是,您可以尝试以下内容:

<style>
#outer {
  width:100px;
  height:40px;
  overflow-x:hidden;
  overflow-y:auto; /* To make it obvious the text is too long. */
  border:1px solid red;
}
#inner {
  width:100px;
}
</style>

<div id="outer">
  <div id="inner" style="font-size:16px">
    Lorem ipsum dolor sit amet, dui orci interdum sagittis,
    proin metus dui, justo in suspendisse dolor.
  </div>
</div>
<button onclick="checkFontSize()">Check font size</button>

<script>
function checkFontSize() {
  var o = document.getElementById('outer');
  var i = document.getElementById('inner');
  var fs = parseInt(i.style.fontSize);
  while(i.offsetHeight > o.offsetHeight) {
    fs--;
    i.style.fontSize = fs + 'px';
  }
}
</script>

It checks to see if the inner div's height is greater than the outer div's; 它检查内部div的高度是否大于外部div的高度; it works on the assumption that the inner div has no padding (you can tweak as necessary). 它的工作原理是内部div没有填充(你可以根据需要进行调整)。 It decreases the inner div's font-size by 1px till the inner div's height is no longer greater than the outer div's. 它将内部div的字体大小减少1px,直到内部div的高度不再大于外部div。

You could have code like this on the page that displays the client's text in which case you could just wrap it in an anonymous function that executes ondomready. 你可以在显示客户端文本的页面上有这样的代码,在这种情况下你可以将它包装在一个执行ondomready的匿名函数中。 I trust you can modify this to suit your implementation. 我相信你可以修改它以适合你的实现。

Haven't tried this in HTML, but you might loop and check the scrollHeight property and see if it's bigger than you expect, and if so set the font one point smaller, and repeat until scrollHeight is what you expect. 没有在HTML中尝试过这个,但你可以循环并检查scrollHeight属性,看看它是否比你预期的要大,如果是这样,将字体设置为小一点,然后重复直到scrollHeight达到预期效果。

https://developer.mozilla.org/en/DOM/element.scrollHeight https://developer.mozilla.org/en/DOM/element.scrollHeight

Public Function GetStringSize(ByVal Str As String, _
                                         ByVal FName As String, _
                                         ByVal FSize As Integer, _
                                         ByVal FStyle As System.Drawing.FontStyle) As System.Drawing.SizeF

 Using graphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(New System.Drawing.Bitmap(1, 1))
     Dim size As System.Drawing.SizeF = graphics.MeasureString(Str, New System.Drawing.Font(FName, FSize, FStyle, System.Drawing.GraphicsUnit.Pixel))
     Return size
 End Using

End Function

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

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