Draw Multi-Line Text to Canvas

Draw multi-line text to Canvas

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);

Android Programming: How to draw multiline text in a rectangle?

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();

canvas drawtext with multiline

You need to use StaticLayout:

TextPaint mTextPaint=new TextPaint();
StaticLayout mTextLayout = new StaticLayout("my text\nNext line is very long text that does not definitely fit in a single line on an android device. This will show you how!", 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 = 100;
textY = 100;

canvas.translate(textX, textY);
mTextLayout.draw(canvas);
canvas.restore();

Draw text in a Canvas and multiple lines

yes, you can manage this with a StaticLayout or DynamicLayout

Multiline text on HTML5 canvas

You can create a function that draws text given your desired styling arguments:

Sample Image





var canvas=document.getElementById("canvas");

var ctx=canvas.getContext("2d");

var cw=canvas.width;

var ch=canvas.height;


drawText('This is the top',canvas.width/2,20,24,'verdana');

drawText('This is the bottom',canvas.width/2,canvas.height-20,16,'Courier');


function drawText(text,centerX,centerY,fontsize,fontface){

ctx.save();

ctx.font=fontsize+'px '+fontface;

ctx.textAlign='center';

ctx.textBaseline='middle';

ctx.fillText(text,centerX,centerY);

ctx.restore();

}
body{ background-color: ivory; }

#canvas{border:1px solid red; margin:0 auto; }
<canvas id="canvas" width=300 height=300></canvas>

Canvas drawtext multiline centered

This is a late answer so I apologise if it is no longer needed but after looking for an answer I decided to employ some maths to solve this issue.

In order to center my multiline text, I take the width of the canvas, subtract the width of the text, and then divide by 2. This number is then used as the x offset of the drawText method.

Here is my full example:

public static Bitmap textAsBitmap(String text, float textSize, int textColor, Context mContext) {
Paint paint = new Paint(ANTI_ALIAS_FLAG);
int pixel = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, textSize, mContext.getResources().getDisplayMetrics());

String[] str = text.split(",");

paint.setTextSize(pixel);
paint.setTypeface(FontUtils.getRalewayExtraBold(mContext));
paint.setColor(textColor);
paint.setTextAlign(Paint.Align.LEFT);
float baseline = -paint.ascent(); // ascent() is negative
int width = (int) (paint.measureText(text) + 0.0f); // round

int height = (int) (baseline + paint.descent() + 0.0f);
Bitmap image = Bitmap.createBitmap((width/str.length)+10, (height*str.length), Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(image);

for (int i = 0; i < str.length; i++){
//Center text here
float textOffset = (canvas.getWidth()-paint.measureText((str[i])))/2;
canvas.drawText(str[i], textOffset, ((i+1)*baseline), paint);
}

return image;
}


Related Topics



Leave a reply



Submit