简体   繁体   English

Gef / draw2d:如何获取自动换行文本的高度

[英]Gef/draw2d: How to get the height of word wrapped text

So I'm making a draw2d program. 所以我正在制作一个draw2d程序。 I have some inputted text which I word wrap by doing: 我有一些输入的文本,可以通过以下方式自动换行:

    TextFlow textWrapper = new TextFlow(_title);
    FlowPage flowPage = new FlowPage();
    flowPage.add(textWrapper);

Now I need to find the height of the text inside it's container with a fixed width. 现在,我需要找到固定宽度的容器内文本的高度。 So far I haven't been able to find a way to do so. 到目前为止,我还没有找到一种方法。 Other threads I've looked at have mentioned using the getPreferredSize() method but this fails to return the correct height. 我看过的其他线程都提到使用getPreferredSize()方法,但这无法返回正确的高度。

Any advice would be helpful, thanks. 任何建议都会有所帮助,谢谢。

You should be able to get this value by invoking getPreferredSize(<your width>, -1) . 您应该可以通过调用getPreferredSize(<your width>, -1)来获取此值。 Worked for me, as you can read here . 您可以在这里阅读,为我工作。 Just remember that you have to invalidate the container for it to work, otherwise it will take values from it's internal cache (at least that is what I remember). 请记住,您必须使容器无效才能使其正常工作,否则它将从其内部缓存中获取值(至少我记得这一点)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM