Java获取给定高度的字体大小

发布于 2024-12-01 04:18:07 字数 257 浏览 5 评论 0原文

有没有一种(简洁的)方法,不是获取特定字体大小的高度,而是获取产​​生给定高度的特定字体(在本例中为 SansSerif)的字体大小?

我当然可以循环字体大小或使用某种形式的二进制印章,但如果可能的话,我想使用一些更干净且资源消耗更少的东西。我发现的最好的方法是使用类似 this 的东西。

Is there a (neat) way, instead of getting the height of a particular font size, getting a font size of a particular font (SansSerif in this case) that produces a given height?

I could of course loop through font sizes or use some form of binary chop, but if possible I would like to use something a bit cleaner and less resource intensive. The best way I've found is using something like this.

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

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

发布评论

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

评论(3

や三分注定 2024-12-08 04:18:07

没有简单的方法。

首先,您知道 java 仅提供五种逻辑字体系列。不保证所有其他字体都存在于您将运行该程序的系统中。

那么,不,系统上不会自动映射字体属性。您必须加载所有这些并循环搜索您想要的度量。

使用 此代码,您可以循环可用字体:

import java.awt.Font;
import java.awt.GraphicsEnvironment;
public class MainClass {
  public static void main(String[] a) {
    GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Font[] fonts = e.getAllFonts(); // Get the fonts
    for (Font f : fonts) {
      System.out.println(f.getFontName());
    }
  }
}

然后选择适合您需求的一款。更改代码以显示您想要的信息:例如重量。

编辑:请注意 它们只是 Java 运行时识别的字体类型名称,必须映射到系统上安装的某种物理字体 观察结果“http://download.oracle.com/javase/1.3/docs/guide/intl/addingfonts.html” rel="nofollow">http://download.oracle.com/javase/1.3/docs/guide/intl/addingfonts.html

即使逻辑字体也不能保证始终相同。您可以做的是获取 10pt 的大小并计算字体大小。就像

font_size_in_points = ((10 * desidered_measure) / equal_measure_of_the_10pt)

There's no easy way.

First, you know that java provides just five logical font families. All other fonts aren't garanteed to be present in the system that you'll run the program.

Then, no, there's no automatically mapping of the fonts' properties on your system. You'll have to load them all and loop searching the measure that you want.

with this code you can loop the available fonts:

import java.awt.Font;
import java.awt.GraphicsEnvironment;
public class MainClass {
  public static void main(String[] a) {
    GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Font[] fonts = e.getAllFonts(); // Get the fonts
    for (Font f : fonts) {
      System.out.println(f.getFontName());
    }
  }
}

and then choose the one that suits your needs. Change the code to display the information you want: weight, for example.

Edit: Notice the They are merely font-type names recognized by the Java runtime which must be mapped to some physical font that is installed on your system observation in http://download.oracle.com/javase/1.3/docs/guide/intl/addingfonts.html.

Even the logical fonts aren't garanteed to be always the same. What you could do is the get the size of 10pt and calculate the font size. Like

font_size_in_points = ((10 * desidered_measure) / equivalent_measure_of_the_10pt)

仙气飘飘 2024-12-08 04:18:07

搜索时, TextLayout,如图此处此处,将为给定文本提供最严格的界限。

When searching, TextLayout, shown here and here, will provide the tightest bounds for a given text.

你好,陌生人 2024-12-08 04:18:07
FontMetrics metric = new FontMetrics(new Font("font"), Font.BOLD, 12);
metrics.getHeight()

可能就是您正在寻找的东西。

更多信息: http://download.oracle.com/ javase/6/docs/api/java/awt/FontMetrics.html

FontMetrics metric = new FontMetrics(new Font("font"), Font.BOLD, 12);
metrics.getHeight()

Might be what you are looking for.

More info: http://download.oracle.com/javase/6/docs/api/java/awt/FontMetrics.html

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