【问题标题】:How to calculate a text size?如何计算文本大小?
【发布时间】:2019-07-01 13:04:47
【问题描述】:

显然com.codename1.ui.plaf.LookAndFeel.getTextAreaSize(TextArea, boolean) 不会返回精确的文本大小。

我想确定对话泡泡的大小,或对话泡泡中可能多行文本的大小。

我该怎么做? CN1 中的某个地方有这样的实用方法吗?

【问题讨论】:

    标签: codenameone


    【解决方案1】:

    这将返回首选大小。对于多行文本,我们根据行数/列数来调整大小,因为文本是可修改和可滚动的,因此计算时并未完全考虑其内容。即使考虑到这一点,这也不是 100% 准确的,因为即使是 1 个像素的宽度差异也会导致断线,从而重新排列所有内容。文本大小涉及许多特殊情况,因此您不应依赖准确性。尝试使用setActAsLabelSpanLabel

    【讨论】:

    • 两种建议的方法 TextArea.setActAsLabel(true)Spanlabel 都不会计算正确的大小,因为它们包含多于一行 - 至少在模拟器中是这样。
    • 我想我注意到那里有几个错误。这是关于模拟器的:The simulator does not render TextArea correctly #2862
    • 正如我所解释的,这个概念本质上是有问题的。您正在询问文本区域的大小,但它不知道它可以使用多少宽度...您期望什么?文本区域需要有一个有效的父级/宽度才能为您提供适当的高度值。
    • 使用 Swing 似乎很容易 - 首先设置宽度以获得高度。为什么不应该与代号一一样工作。您可以看到它正在尝试计算首选尺寸 - 只是它做错了。
    【解决方案2】:

    显然,目前代号一中没有文本区域组件可用于调整其内容的大小。

    这是一个自定义的:

        private class Hint extends Component {
            final String text;
            final int rowsGap = new TextArea().getRowsGap();
    
            private Hint(String aText) {
                text = aText;
            }
    
            @Override
            protected Dimension calcPreferredSize() {
                int prefW = 0;
                int prefH = 0;
                Style style = getStyle();
                Font font = style.getFont();
                List<String> lines = StringUtil.tokenize(text, "\n");
                int tally = 0;
                for (String line: lines) {
                    tally++;
                    prefW = font.stringWidth(line);
                }
                prefH = tally * (font.getHeight() + rowsGap);
                prefW += getUnselectedStyle().getHorizontalPadding() + 5;
                prefH += style.getPaddingTop() + style.getPaddingBottom();
                if(style.getBorder() != null) {
                    prefW = Math.max(style.getBorder().getMinimumWidth(), prefW);
                    prefH = Math.max(style.getBorder().getMinimumHeight(), prefH);
                }
                if(getUIManager().getLookAndFeel().isBackgroundImageDetermineSize() && style.getBgImage() != null) {
                    prefW = Math.max(style.getBgImage().getWidth(), prefW);
                    prefH = Math.max(style.getBgImage().getHeight(), prefH);
                }
                return new Dimension(prefW, prefH);
            }
    
            @Override
            public void paint(Graphics aGraphics) {
                Style style = getStyle();
                int xLeft = style.getPaddingLeft(false), yTop = style.getPaddingTop();
                List<String> lines = StringUtil.tokenize(text, "\n");
                int y = yTop;
                aGraphics.setColor(0x000000);
                for (String line: lines) {
                    aGraphics.drawString(line, getX() + xLeft, getY() + y);
                    y += style.getFont().getHeight() + rowsGap;
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2018-10-25
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多