Hope this help you, you can set text font, align text in canvas..
Typeface font = ResourcesCompat.getFont(this, R.font.sf_pro_text_bold);
Bitmap textBm = BitmapUtils.createBitmapFromText("This is longgggggg texttttttttttttttttttt \n This is line breakkkkkkkkkkk"
, width // Container width
, Color.BLACK
, 40
, font
, TextAlign.CENTER
, 10f
, true);
public enum TextAlign {
CENTER,
LEFT,
RIGHT
}
/**
* Create bit map from text with auto line break
* */
public static Bitmap createBitmapFromText(String text, int containerWidth, @ColorInt int textColor, int textSize
, @Nullable Typeface textFont, @Nullable TextAlign textAlign
, @Nullable Float lineSpace, @Nullable Boolean trimLine) {
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setTypeface(textFont);
textPaint.setTextSize(textSize);
textPaint.setColor(textColor);
ArrayList<String> linesText = splitTextToMultiLine(text, containerWidth, textSize, textFont);
if(lineSpace == null) {
lineSpace = 10f;
}
int bmWidth = containerWidth;
int bmHeight = 0;
for(String line : linesText) {
int lineHeight = (int)(calculateTextHeightFromFontSize(line, textSize, textFont)*1.1);
bmHeight += (lineHeight + lineSpace);
}
Bitmap result = Bitmap.createBitmap(bmWidth, bmHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
TextPaint staticLayoutPaint = new TextPaint();
staticLayoutPaint.setTextSize(textSize);
staticLayoutPaint.setTypeface(textFont);
Layout.Alignment align = Layout.Alignment.ALIGN_CENTER;
if(textAlign == TextAlign.LEFT) {
align = Layout.Alignment.ALIGN_NORMAL;
} else if(textAlign == TextAlign.RIGHT) {
align = Layout.Alignment.ALIGN_OPPOSITE;
}
StaticLayout.Builder staticLayoutBuilder = StaticLayout.Builder
.obtain("", 0, 0, staticLayoutPaint, containerWidth)
.setAlignment(align)
.setLineSpacing(lineSpace, 1)
.setText(text);
staticLayoutBuilder.build().draw(canvas);
} else {
float xOffset = 0;
float yOffset = -textPaint.ascent(); // ascent() is negative;
for(String lineText : linesText) {
if(trimLine) {
lineText = lineText.trim();
}
if(textAlign == TextAlign.RIGHT) {
xOffset = containerWidth - calculateTextWidthFromFontSize(lineText, textSize, textFont);
} else if (textAlign == TextAlign.CENTER){
xOffset = (containerWidth - calculateTextWidthFromFontSize(lineText, textSize, textFont)) / 2;
}
canvas.drawText(lineText, xOffset, yOffset, textPaint);
float nextLineOffset = calculateTextHeightFromFontSize(lineText, textSize, textFont) + lineSpace;
yOffset += nextLineOffset;
}
}
return result;
}
private static int calculateTextWidthFromFontSize(String text, int textSize, @Nullable Typeface textFont) {
String singleChar = text;
Rect bounds = new Rect();
Paint paint = new Paint();
paint.setTextSize(textSize);
paint.setTypeface(textFont);
paint.getTextBounds(singleChar, 0, singleChar.length(), bounds);
return bounds.width();
}
private static int calculateTextHeightFromFontSize(String text, int textSize, @Nullable Typeface textFont) {
String singleChar = text;
Rect bounds = new Rect();
Paint paint = new Paint();
paint.setTextSize(textSize);
paint.setTypeface(textFont);
paint.getTextBounds(singleChar, 0, singleChar.length(), bounds);
return bounds.height();
}
private static ArrayList<String> splitTextToMultiLine(String input, int containerWidth, int textSize, @Nullable Typeface textFont) {
ArrayList<String> result = new ArrayList<>();
//Split String by line break first
String[] multiLine = input.split("\n");
for(String line : multiLine) {
result.addAll(splitLongStringToMultiLine(line, containerWidth, textSize, textFont));
}
return result;
}
/**
* Split long string (without line break) to multi line
* */
private static ArrayList<String> splitLongStringToMultiLine(String input, int containerWidth, int textSize, @Nullable Typeface textFont) {
ArrayList<String> result = new ArrayList<>();
//Reduce loop performance
int singleTextWidth = calculateTextWidthFromFontSize("A", textSize, textFont);
int minTextPerLine = containerWidth/singleTextWidth;
if(minTextPerLine >= input.length()
|| calculateTextWidthFromFontSize(input, textSize, textFont) < containerWidth) {
result.add(input);
return result;
}
int startSplit = 0;
while (startSplit < input.length()) {
int addedTextPerLine = 0;
if(startSplit + minTextPerLine < input.length()) {
int endPos = startSplit + minTextPerLine;
String availableChild = input.substring(startSplit, endPos);
//Detect more character in line
int updatePos = endPos;
while (updatePos < input.length()
&& calculateTextWidthFromFontSize(availableChild, textSize, textFont) < containerWidth) {
availableChild = input.substring(startSplit, updatePos);
addedTextPerLine++;
updatePos = endPos + addedTextPerLine;
}
//Detect last space char and split
int spaceIndex = availableChild.lastIndexOf(" ");
if(spaceIndex > 0) {
//Update split point
startSplit -= (availableChild.length() - spaceIndex);
availableChild = availableChild.substring(0, spaceIndex);
} else{
//No space found ->
}
result.add(availableChild);
} else {
//Last line
String child = input.substring(startSplit);
result.add(child);
}
startSplit += minTextPerLine;
startSplit += addedTextPerLine;
addedTextPerLine = 0;
}
if(result.size() == 0) {
//Cheat
result.add(input);
}
return result;
}