This is a long shot, but does anyone know of an algorithm for estimating and categorising text width (for a variable width font) based on its contents?

这是一个很长的尝试,但是有谁知道一种基于内容估计和分类文本宽度的算法(对于可变宽度字体)?

For example, I'd like to know that iiiiiiii is not as wide as abcdefgh, which in turn is not as wide as WWWWWWWW, even though all three strings are eight characters in length.

例如,我想知道iiiiii不像abcdefgh那么宽,而abcdefgh又不像wwwwwwwwwwwwwwww那么宽,尽管这三个字符串都是8个字符的长度。

This is actually an attempt to build some smarts into a string truncation method, which at the moment is correctly truncating a visually wide string, but is also unnecessarily truncating a visually narrow string, because both strings contain the same number of characters. It's probably sufficient for the algorithm to categorise the input string as narrow, normal or wide and then truncate as appropriate.

这实际上是试图将一些smarts构建到一个字符串截断方法中,该方法目前正确地截断了一个可视范围的字符串,但也不必要地截断了一个可视的窄字符串,因为这两个字符串都包含相同数量的字符。对于算法来说,将输入字符串分类为窄的、正常的或宽的,然后截断为合适的,这可能已经足够了。

This question isn't really language-specific, but if there is an algorithm then I'll implement it in Java. This is for a web application. I'm aware that there are answers on SO that deal with this problem using JavaScript to obtain the width of a containing div element, but I wondered if a server-side solution is possible.

这个问题并不是特定于语言的,但是如果有一个算法,那么我将在Java中实现它。这是针对web应用程序的。我知道有一些答案,可以使用JavaScript来处理这个问题,以获得包含div元素的宽度,但是我想知道服务器端解决方案是否可行。

8 个解决方案

#1


15

Most GUI frameworks provide some way to calculate text metrics for fonts on given output devices.

大多数GUI框架提供了一些方法来计算给定输出设备上字体的文本度量。

Using java.awt.FontMetrics, for example, I believe you can do this:

使用java.awt。比如FontMetrics,我相信你可以做到:

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics; 

public int measureText(Graphics g, String text) {
   g.setFont(new Font("TimesRoman", Font.PLAIN, 12));
   FontMetrics metrics = g.getFontMetrics();

   return metrics.stringWidth(text);
}

Not tested, but you get the idea.

虽然没有经过测试,但是你知道了。


Under .Net you can use the Graphics.MeasureString method. In C#:

在。net下,你可以使用图形。MeasureString方法。在c#中:

private void MeasureStringMin(PaintEventArgs e)
{

    // Set up string.
    string measureString = "Measure String";
    Font stringFont = new Font("Arial", 16);

    // Measure string.
    SizeF stringSize = new SizeF();
    stringSize = e.Graphics.MeasureString(measureString, stringFont);

    // Draw rectangle representing size of string.
    e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F, 0.0F, stringSize.Width, stringSize.Height);

    // Draw string to screen.
    e.Graphics.DrawString(measureString, stringFont, Brushes.Black, new PointF(0, 0));
}

更多相关文章

  1. java7 switch语句使用字符串
  2. 在Java中修剪可能的字符串前缀
  3. 与==两个字符串的Java比较是假的? [重复]
  4. 将字符串拆分为字符串数组。
  5. java十进制字符串转十六进制字符串
  6. jQuery(JavaScript)获取文字(字符串)宽度(显示长度)
  7. Java将一个字符串中的多个连一起的空格变成单个空格

随机推荐

  1. js中ajax获取json数据遍历提示undefined
  2. 在单选按钮上选中/取消选中,加载/隐藏部分
  3. javascript 的MD5代码备份,跟java互通
  4. 使用AngularJS隐藏滚动div
  5. 简单实现一个文件上传的进度条
  6. 如何在当前视图中始终保持水平底部滚动条
  7. JavaScript(ES5)使用保留字作函数名
  8. JQuery纯前端导入Excel文件,兼容IE10及IE9
  9. 如何将图像(PNG)转换为2D数组(二进制图像)?
  10. 当鼠标悬停在顶部的对象上时,SVG悬停被取