如何计算 JavaFX 中字符串的像素宽度? [英] How to calculate the pixel width of a String in JavaFX?

查看:43
本文介绍了如何计算 JavaFX 中字符串的像素宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Java FX 2.2 中似乎没有 API 调用来计算文本字符串的宽度(以像素为单位).在其他论坛上有一些解决方法的建议,但我创建或找到任何返回字符串宽度的代码的努力都失败了,无论是使用默认字体还是其他方式.任何帮助将不胜感激.

It appears that there is no API call to calculate the width (in pixels) of a text string in Java FX 2.2. There have been suggestions of workarounds on other forums, but my efforts to create or find any code that returns the width of a String, either using the default font or otherwise, have failed. Any help would be appreciated.

推荐答案

如果你只是测量没有 CSS 的默认字体:

If you are just measuring the default font without CSS:

  1. 将要测量的字符串放置在 Text 对象中.
  2. 获取 Text 对象布局边界的宽度.

如果您需要应用 CSS:

If you need to apply CSS:

  1. 将要测量的字符串放置在 Text 对象中.
  2. 创建一个一次性场景并将文本对象放置在场景中.
  3. 拍摄文本快照(如果您使用的是 Java 7)或调用 applyCss 用于 Java 8.
  4. 获取 Text 对象布局边界的宽度.
  1. Place the String to be measured in a Text object.
  2. Create a throwaway Scene and place the Text object in the Scene.
  3. Take a snapshot of the Text (if you are using Java 7) or call applyCss for Java 8.
  4. Get the width of the Text object's layout bounds.

这是有效的,因为它强制对计算其布局边界的 Text 进行布局传递.步骤 2 中的场景是必需的,因为这正是 CSS 处理器的工作方式(它需要一个位于场景中的节点才能完成其工作).如果您想进一步了解处理过程,请务必阅读 applyCss 的链接 javadoc.

This works because it forces a layout pass on the Text which calculates it's layout bounds. The scene in step 2 is required because that is just the way the CSS processor works (it needs a node to be located in a Scene to be able to do its job). Definitely read the linked javadoc for applyCss if you want to understand the processing further.

示例代码

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.text.Text;
import javafx.stage.Stage;

// displays the width in pixels of an arbitrary piece of text.
public class MeasureText extends Application {
  public static void main(String[] args) { launch(args); }
  @Override public void start(Stage stage) throws Exception {
    final Text text = new Text("XYZZY");
    new Scene(new Group(text));

    // java 7 => 
    //    text.snapshot(null, null);
    // java 8 =>
    text.applyCss(); 

    final double width = text.getLayoutBounds().getWidth();

    stage.setScene(new Scene(new Label(Double.toString(width))));
    stage.show();
  }
}

示例程序输出(以像素为单位显示任意一段文本的宽度):

Sample program output (displays the width in pixels of an arbitrary piece of text):

如果将文本打印到具有设置字体的图形上下文中,这将如何(如果有的话)改变?

How (if at all) would this change if the text was printed to a graphicscontext with a set font?

将字体应用于包含您将在画布上绘制的相同消息的文本对象.与测量绘制到场景图的文本不同,绘制到画布的项目没有应用 CSS,因此您无需在测量文本之前将 Text 对象放置在场景中并应用 CSS.您可以测量文本对象的布局边界,它将与使用相同字体在画布内绘制的文本边界相同.

Apply the font to a text object containing the same message you will plot to the canvas. Unlike when you are measuring text plotted to the scene graph, items plotted to a canvas do not have CSS applied to them, so you don't need to place the Text object in a scene and have CSS applied to it before measuring the text. You can measure the layout bounds of your text object and it will be the same as the bounds of the text plotted within the canvas with the same font.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.*;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.text.*;
import javafx.stage.Stage;

// displays the width in pixels of an arbitrary piece of text (which has been plotted on a canvas).
public class MeasureText extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        final String msg = "XYZZY";
        final Text text = new Text(msg);
        Font font = Font.font("Arial", 20);
        text.setFont(font);

        final double width = text.getLayoutBounds().getWidth();

        Canvas canvas = new Canvas(200, 50);
        GraphicsContext gc = canvas.getGraphicsContext2D();
        gc.setFont(font);
        gc.fillText(msg, 0, 40);

        stage.setScene(new Scene(
                new VBox(new Label(Double.toString(width)), canvas))
        );
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

这篇关于如何计算 JavaFX 中字符串的像素宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆