android kotlin - Canvas draw text in rectangle

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.graphics.*
import android.os.Bundle
import android.text.TextPaint
import android.widget.ImageView


class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // get the widgets reference from XML layout
        val imageView = findViewById<ImageView>(R.id.imageView)


        // draw text on canvas inside rectangle and get bitmap
        val bitmap = drawTextInsideRectangle(
            textToDraw = "Text to draw inside rectangle",
            textSize = 185F,
            textColor = Color.parseColor("#F0FFFF"),
            rectangleColor = Color.parseColor("#333399"),
            canvasColor = Color.parseColor("#A2A2D0")
        )


        // show drawing on image view
        imageView.setImageBitmap(bitmap)
    }
}



// function to draw text inside rectangle
fun drawTextInsideRectangle(
    textToDraw:String = "Text to draw in rectangle",
    textSize:Float = 100F,
    textColor:Int = Color.BLACK,
    rectangleColor:Int = Color.LTGRAY,
    canvasColor:Int = Color.DKGRAY
):Bitmap?{
    val bitmap = Bitmap.createBitmap(
        1500,
        850,
        Bitmap.Config.ARGB_8888
    )


    // canvas to drawing
    val canvas = Canvas(bitmap).apply {
        drawColor(canvasColor)
    }


    // draw rectangle on canvas
    val rectF = RectF(
        200f,
        180f,
        canvas.width - 80F,
        canvas.height - 120F
    )

    canvas.drawRect(rectF,Paint().apply { color = rectangleColor })


    // text paint to draw text
    val textPaint = TextPaint().apply {
        color = textColor
        textAlign  = Paint.Align.CENTER
        this.textSize = textSize
        isAntiAlias = true
    }


    // calculate text y offset to center it vertically
    val textHeight = textPaint.descent() - textPaint.ascent()
    val textOffset = textHeight / 2 - textPaint.descent()


    // rectangle margin to draw text
    val margin = 130F


    // number of characters can draw inside rectangle
    var text = textToDraw
    val numOfChars = textPaint.breakText(
        text,
        true,
        rectF.width() - margin,
        null
    )

    if (text.length > numOfChars){
        text = text.substring(0, numOfChars) + ".."
    }


    // finally, draw the text inside rectangle
    canvas.drawText(
        text,
        rectF.centerX(),
        rectF.centerY() + textOffset,
        textPaint
    )

    return bitmap
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:padding="24dp">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
More android kotlin tutorials