set Text Size For Width - Android Graphics

Android examples for Graphics:Paint

Description

set Text Size For Width

Demo Code


//package com.java2s;

import android.graphics.Paint;

import android.graphics.Rect;

public class Main {
    private static void setTextSizeForWidth(String text, Paint paint,
            float desiredWidth) {

        final float testTextSize = 48f;
        paint.setTextSize(testTextSize);
        Rect bounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), bounds);
        float desiredTextSize = testTextSize * desiredWidth
                / bounds.width();//from  w w w  .ja  va  2  s  .c  o m
        paint.setTextSize(desiredTextSize);
    }
}

Related Tutorials