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);
...
I was hoping this would result in line breaks, but instead I am seeing cryptic characters where the \n
would be.
Any pointers appreciated.
Paul
I found another way using static layouts. The code is here for anyone to refer to:
TextPaint mTextPaint=new TextPaint();
StaticLayout mTextLayout = new StaticLayout(mText, mTextPaint, canvas.getWidth(), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
canvas.save();
// calculate x and y position where your text will be placed
textX = ...
textY = ...
canvas.translate(textX, textY);
mTextLayout.draw(canvas);
canvas.restore();