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?
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();
You'll need to measure the text and then break it yourself in code. Paint.measureText is what you need.
public class MutilineText {
private String mText;
private int fontSize = 50;
public MutilineText(String text) {
this.mText = text;
}
public String getText() {
return mText;
}
public void setText(String text) {
mText = text;
}
public void draw(Canvas canvas, Rect drawSpace) {
Paint paintText = new Paint(Paint.ANTI_ALIAS_FLAG);
paintText.setAntiAlias(true);
paintText.setDither(true);
paintText.setColor(Color.BLACK);
paintText.setStyle(Paint.Style.FILL);
paintText.setStrokeWidth(3);
paintText.setTextSize(fontSize);
drawMultilineText(mText, drawSpace.left, drawSpace.top + 15, paintText, canvas, fontSize, drawSpace);
}
private void drawMultilineText(String str, int x, int y, Paint paint, Canvas canvas, int fontSize, Rect drawSpace) {
int lineHeight = 0;
int yoffset = 0;
String[] lines = str.split("\n");
lineHeight = (int) (calculateHeightFromFontSize(str, fontSize) * 1.4);
String line = "";
for (int i = 0; i < lines.length; ++i) {
if (calculateWidthFromFontSize(line, fontSize) <= drawSpace.width()) {
canvas.drawText(line, x + 30, y + yoffset, paint);
yoffset = yoffset + lineHeight;
line = lines[i];
} else {
canvas.drawText(divideString(line, drawSpace.width()), x + 30, y + yoffset, paint);
}
}
}
private String divideString(String inputString, int bound) {
String ret = inputString;
while (calculateWidthFromFontSize(ret, fontSize) >= bound) {
ret = ret.substring(0, (ret.length() - 1));
}
ret = ret.substring(0, ret.length() - 3) + "...";
return ret;
}
private int calculateWidthFromFontSize(String testString, int currentSize) {
Rect bounds = new Rect();
Paint paint = new Paint();
paint.setTextSize(currentSize);
paint.getTextBounds(testString, 0, testString.length(), bounds);
return (int) Math.ceil(bounds.width());
}
private int calculateHeightFromFontSize(String testString, int currentSize) {
Rect bounds = new Rect();
Paint paint = new Paint();
paint.setTextSize(currentSize);
paint.getTextBounds(testString, 0, testString.length(), bounds);
return (int) Math.ceil(bounds.height());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With