J2ME 中 Canvas 上的文本换行

发布于 2024-09-12 18:25:35 字数 1436 浏览 19 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好菇凉咱不稀罕他 2024-09-19 18:25:35

这是我在应用程序中使用的代码,它将包裹线条向量,您可以在画布中的任何 X 点绘制。

public static Vector wrapToLines(String text, Font f, int maxWidth) {
        Vector lines = new Vector ();
        boolean paragraphFormat = false;
        if (text == null) {
            return lines;
        }
        if (f.stringWidth(text) < maxWidth) {
            lines.add(text);
            return lines;
        } else {

            char chars[] = text.toCharArray();
            int len = chars.length;
            int count = 0;
            int charWidth = 0;
            int curLinePosStart = 0;
            while (count < len) {
                if ((charWidth += f.charWidth(chars[count])) > (maxWidth - 4) || count == len - 1) // wrap to next line
                {
                    if (count == len - 1) {
                        count++;
                    }
                    String line = new String(chars, curLinePosStart, count - curLinePosStart);
                    if (paragraphFormat) {
                        int lastSpacePosition = line.lastIndexOf(" ");
                        String l = new String(chars, curLinePosStart, (lastSpacePosition != -1) ? lastSpacePosition + 1 : (count == len - 1) ? count - curLinePosStart + 1 : count - curLinePosStart);
                        lines.add(l);
                        curLinePosStart = (lastSpacePosition != -1) ? lastSpacePosition + 1 : count;
                    } else {
                        lines.add(line);
                        curLinePosStart = count;
                    }
                    charWidth = 0;
                }
                count++;
            }
            return lines;

        }
    }

而只是运行 for 循环

int y=0;
int linespacing=4;
  for(int i=0;i<lines.size();i++)
  {
     g.drawString((String)lines.get(i),10,y,0);
     y+=(i!=lines.size()-1)?(font.getHeight()+linespacing):0;
   }

享受:)

This is my code i used in my app it will wrapped lines vector and u can draw at any X point in canvas.

public static Vector wrapToLines(String text, Font f, int maxWidth) {
        Vector lines = new Vector ();
        boolean paragraphFormat = false;
        if (text == null) {
            return lines;
        }
        if (f.stringWidth(text) < maxWidth) {
            lines.add(text);
            return lines;
        } else {

            char chars[] = text.toCharArray();
            int len = chars.length;
            int count = 0;
            int charWidth = 0;
            int curLinePosStart = 0;
            while (count < len) {
                if ((charWidth += f.charWidth(chars[count])) > (maxWidth - 4) || count == len - 1) // wrap to next line
                {
                    if (count == len - 1) {
                        count++;
                    }
                    String line = new String(chars, curLinePosStart, count - curLinePosStart);
                    if (paragraphFormat) {
                        int lastSpacePosition = line.lastIndexOf(" ");
                        String l = new String(chars, curLinePosStart, (lastSpacePosition != -1) ? lastSpacePosition + 1 : (count == len - 1) ? count - curLinePosStart + 1 : count - curLinePosStart);
                        lines.add(l);
                        curLinePosStart = (lastSpacePosition != -1) ? lastSpacePosition + 1 : count;
                    } else {
                        lines.add(line);
                        curLinePosStart = count;
                    }
                    charWidth = 0;
                }
                count++;
            }
            return lines;

        }
    }

and while just run in for loop

int y=0;
int linespacing=4;
  for(int i=0;i<lines.size();i++)
  {
     g.drawString((String)lines.get(i),10,y,0);
     y+=(i!=lines.size()-1)?(font.getHeight()+linespacing):0;
   }

Enjoy :)

东走西顾 2024-09-19 18:25:35

需要自己计算要绘制的字符串的宽度并另起一行
(分割字符串)每次达到画布的最大宽度时。

void paint(Graphics _g) {
  String t = "text to draw";
  int px_consumed = _g.getFont().substringWidth(t, 0, t.length())}
}

You need to calculate the width of the string to be drawn yourself and start a new line
(split the string) each time you reach the max width of the canvas.

void paint(Graphics _g) {
  String t = "text to draw";
  int px_consumed = _g.getFont().substringWidth(t, 0, t.length())}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文