绘制多行文本到画布

问题描述:

一个希望快速的问题,但我似乎无法找到任何的例子...我想通过写多行文本到自定义查看 画布,而在的OnDraw()我:

A hopefully quick question, but I can't seem to find any examples... I'd like to write multi-line text to a custom View via a Canvas, and in onDraw() I have:

...
String text = "This is\nmulti-line\ntext";
canvas.drawText(text, 100, 100, mTextPaint);
...

我希望这会导致换行符,而是我看到神秘的人物,其中

任何指针AP preciated。

Any pointers appreciated.

不幸的是Android不知道是什么是。什么,你需要做的是剥离,然后偏移在Y获得下一行文本。因此,像这样:

Unfortunately Android doesn't know what \n is. What you have to do is strip the \n and then offset the Y to get your text on the next line. So something like this:

canvas.drawText("This is", 100, 100, mTextPaint);
canvas.drawText("multi-line", 100, 150, mTextPaint);
canvas.drawText("text", 100, 200, mTextPaint);