使用graphics.drawText(字符串,X,Y)上绘制几行字符串 [英] use graphics.drawText(String, x, y) to draw a string on several lines

查看:295
本文介绍了使用graphics.drawText(字符串,X,Y)上绘制几行字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个自定义字段重新presenting图像,并接近它的标签。
我对 graphics.drawBitmap绘制图像(...)

I am making a custom Field representing an image and a label near it. I am drawing the image by graphics.drawBitmap(...)

NW文本,我使用 graphics.drawText(字符串,X,Y); ,但如果文本比显示更大,它不返回到线路。我尝试添加 \\ n 但这并没有解决问题。

Nw for the text, I am using graphics.drawText(String, x, y); but if the text it bigger than the display, it doesn't return to the line. I tried to add \n but that didn't solve the problem.

我怎么可以借鉴,可以在多行扩展的文本?

How can I draw a text that can expand on several lines?

谢谢,

推荐答案

我找到了一个解决问题的办法。你可以找到它下面的那些有兴趣谁在这个问题:

I found a solution to the problem. You can find it below for those who are interested in this question:

public void drawMultipleLines(Graphics graphics, String text, int X, int Y, int XavailableSpace) {
    String[] texts = UiSpliter.split(text, " "); //UiSpliter is a class that will split the string into string array based on the ' ' character
    int x = X;
    int y = Y;
    for (int i = 0 ; i < texts.length ; i ++) {
        if (x + getFont().getAdvance(texts[i]) - X > XavailableSpace) {
            if (!(x == X)) {
                x = X;
                y = y + getFont().getHeight();
            }
        }
        graphics.drawText(texts[i] + " ", x, y);
        x += getFont().getAdvance(texts[i] + " ");
    }
}

这篇关于使用graphics.drawText(字符串,X,Y)上绘制几行字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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