get Multi Line Text Bounds - Android java.lang

Android examples for java.lang:Math Geometry

Description

get Multi Line Text Bounds

Demo Code


//package com.java2s;

import android.graphics.Paint;
import android.graphics.Rect;

public class Main {
    public static void getMultiLineTextBounds(Paint paint, char[] text,
            Rect bounds, float lineHeight) {
        getMultiLineTextBounds(paint, new String(text), bounds, lineHeight);
    }/*from  ww w.  j  ava  2 s.  com*/

    public static void getMultiLineTextBounds(Paint paint, String text,
            Rect bounds, float lineHeight) {

        bounds.set(0, 0, 0, 0);
        Rect tmpRect = new Rect();

        String[] lines = text.split("\n");
        for (String line : lines) {
            paint.getTextBounds(text, 0, line.length(), tmpRect);
            bounds.right = Math.max(tmpRect.width(), bounds.width());
            bounds.bottom += (-paint.ascent() + paint.descent())
                    * lineHeight;
        }

    }
}

Related Tutorials