Java获取给定高度的字体大小
有没有一种(简洁的)方法,不是获取特定字体大小的高度,而是获取产生给定高度的特定字体(在本例中为 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有简单的方法。
首先,您知道 java 仅提供五种逻辑字体系列。不保证所有其他字体都存在于您将运行该程序的系统中。
那么,不,系统上不会自动映射字体属性。您必须加载所有这些并循环搜索您想要的度量。
使用 此代码,您可以循环可用字体:
然后选择适合您需求的一款。更改代码以显示您想要的信息:例如重量。
编辑:请注意 它们只是 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:
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)
搜索时,
TextLayout
,如图此处和此处,将为给定文本提供最严格的界限。When searching,
TextLayout
, shown here and here, will provide the tightest bounds for a given text.可能就是您正在寻找的东西。
更多信息: http://download.oracle.com/ javase/6/docs/api/java/awt/FontMetrics.html
Might be what you are looking for.
More info: http://download.oracle.com/javase/6/docs/api/java/awt/FontMetrics.html