JavaFX:带有自动换行的TextFlow的最佳宽度 [英] JavaFX: optimal width of TextFlow with word-wrap

查看:1953
本文介绍了JavaFX:带有自动换行的TextFlow的最佳宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Textflow.setMaxWidth(double)我可以实现文本换行。

With Textflow.setMaxWidth(double) I can achieve text wrapping.

但是如何调整宽度 TextFlow 之后是否基于实际的包装位置?

But how can I adjust the width of TextFlow afterwards so that it is based on the actual wrapping position?

换句话说,如何让 TextFlow 边界对其所有子节点 Text 绑定以摆脱右边的空白区域:

In other words, how to let the TextFlow bounds snap to all of its children Text bounds to get rid of the empty space on the right:

**编辑** 我在这个问题上取得了一些进展。我的类派生自 TextFlow 现在包含:

**Edit** I have made some progress on this issue. My class derived from TextFlow now contains:

    double maxChildWidth = 0;
    for (Node child : getManagedChildren()) {
        double childWidth = child.getLayoutBounds().getWidth();
        maxChildWidth = Math.max(maxChildWidth, childWidth);
    }
    double insetWidth = getInsets().getLeft() + getInsets().getRight();
    double adjustedWidth = maxChildWidth + insetWidth;
    setMaxWidth(adjustedWidth);

不幸的是,这种方法似乎还不准确,因为它会导致第二次文本流更改在某些情况下。

Unfortunately, this approach does not seem to be accurate yet, since it results in a second text flow change in some cases.

推荐答案

您在问题中发布的解决方案似乎对我的测试工作正常。

The solution you posted in your question seemed to work OK for me on testing.

在下图中,场景已调整大小以触发TextFlow的换行(以红色显示)。在右侧,TextFlow并不完全包裹在最后一个可见字符处,因为行中的最后一个字符是空格,因此包装发生在空格之后。考虑到TextFlow边界是根据需要刷新所有文本。

In the image below the scene has been resized to trigger wrapping of the TextFlow (shown in red). On the right, the TextFlow does not exactly wrap at the last visible character, because the last character in the line is a space, so the wrapping occurs after the space. Taking into account the TextFlow bounds are flush to all of the text as required.

如果松开最大宽度,则会得到文本框的默认行为,即宽度增大(就像你一样)可以在下面看到。)

If you unclamp the max width, you get the default behavior of the text box which is to have the width grow (as you can see below).

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

public class TextFlowWrapper extends Application {

  @Override
  public void start(Stage stage) {
    TextFlow textFlow = new TextFlow(
      new Text(
        "Box with long description that should be wrapped"
      )
    ) {
      @Override
      protected void layoutChildren() {
        super.layoutChildren();

        double maxChildWidth = 0;
        for (Node child : getManagedChildren()) {
          double childWidth = child.getLayoutBounds().getWidth();
          maxChildWidth = Math.max(maxChildWidth, childWidth);
        }
        double insetWidth = getInsets().getLeft() + getInsets().getRight();
        double adjustedWidth = maxChildWidth + insetWidth;

        setMaxWidth(adjustedWidth);
      }
    };

    textFlow.setStyle("-fx-background-color: red");
    textFlow.setMaxWidth(Control.USE_PREF_SIZE);
    textFlow.setMaxHeight(Control.USE_PREF_SIZE);

    Button unclamp = new Button("Unclamp max width");
    unclamp.setOnAction(e -> textFlow.setMaxWidth(Double.MAX_VALUE));

    StackPane wrappedText = new StackPane(textFlow);

    VBox vbox = new VBox(
        unclamp,
        wrappedText
    );
    VBox.setVgrow(wrappedText, Priority.ALWAYS);

    Scene scene = new Scene(
        vbox
    );

    stage.setScene(scene);
    stage.show();
  }

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

解决方案有点像黑客,你可能想要向一位JavaFX开发人员询问有关 openjfx开发人员列表<的一些意见/ A>。也许一些额外的api就像一个布尔包属性,有条件地更新内部文本流布局算法以自动将文本流打包到最小尺寸将是一种更好地做到这一点的方法。部分困难在于,以这样一种方式编写这个问题很容易理解是一个难题。还很难说这是否是某种角落情况,或者是否会有更多人会遇到这种情况(因此可能证明额外API的复杂性)。

The solution is a little bit of a hack, you might want to ask one of the JavaFX developers for some input on the openjfx developer list. Maybe some additional api like a boolean pack property which would conditionally update the internal textflow layout algorithm to automatically pack the textflow to a minimal size would be a way to do this better. Part of the difficulty is that it is a hard question to write in such a way that the issue is easily understandable. It is also hard to say if this is some kind of corner case or if it is something that more people will encounter (hence perhaps justifying the complexity of an additional API).

这篇关于JavaFX:带有自动换行的TextFlow的最佳宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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