Cut off text by paint width - Android android.graphics

Android examples for android.graphics:Paint

Description

Cut off text by paint width

Demo Code

import android.content.Context;
import android.graphics.Paint;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    public static String textCutoff(final String text, Paint paint,
            int width) {
        if (isEmpty(text) || paint == null || width <= 0) {
            return text;
        }//from w w  w . j  a  va2 s.  com

        String dstText = text;
        float fontSize = paint.measureText(dstText);

        while (fontSize > width) {
            dstText = dstText.substring(0, dstText.length() - 1);
            fontSize = paint.measureText(dstText);
        }

        return dstText;
    }

}

Related Tutorials