Android Programming: How to draw multiline text in a rectangle?
android, android-canvas, android-layout
Solution
You can use StaticLayout.
RectF rect = new RectF(....)
StaticLayout sl = new StaticLayout("This is my text that must fit to a rectangle", textPaint, (int)rect.width(), Layout.Alignment.ALIGN_CENTER, 1, 1, false);
canvas.save();
canvas.translate(rect.left, rect.top);
sl.draw(canvas);
canvas.restore();
Problem
I've seen many posts dealing with similar problems but none of them worked for me. In `Canvas`, I have a rectangle of size, let's say, 200px by 200px, and I want to write text in this rectangle. The text doesn't need to fill the entire rectangle, but the important thing is that there should automatically be a line break when it reaches the end of the rectangle. How can I do this in Android?