Example usage for android.text TextPaint breakText

List of usage examples for android.text TextPaint breakText

Introduction

In this page you can find the example usage for android.text TextPaint breakText.

Prototype

public int breakText(CharSequence text, int start, int end, boolean measureForwards, float maxWidth,
        float[] measuredWidth) 

Source Link

Document

Measure the text, stopping early if the measured width exceeds maxWidth.

Usage

From source file:Main.java

public static int getTextViewLineCount(TextView textView, String text, int maxWidth) {
    if (textView == null)
        return 0;

    int index = 0;
    int lineCount = 0;
    TextPaint paint = textView.getPaint();
    while (index < text.length()) {
        index += paint.breakText(text, index, text.length(), true, maxWidth, null);
        lineCount++;//from   w w w  .  j a v  a2  s.  c om
    }
    return lineCount;
}

From source file:Main.java

public static String getEllipsisedText(TextView textView) {
    try {/*  w ww .ja  va  2s .  c o m*/
        String text = textView.getText().toString();
        int lines = textView.getLineCount();
        int width = textView.getWidth();
        int len = text.length();

        Log.d("Test", "text-->" + text + "; lines-->" + lines + "; width-->" + width + ";len-->" + len);
        TextUtils.TruncateAt where = TextUtils.TruncateAt.END;
        TextPaint paint = textView.getPaint();

        StringBuffer result = new StringBuffer();

        int spos = 0, cnt, tmp, hasLines = 0;

        while (hasLines < lines - 1) {
            cnt = paint.breakText(text, spos, len, true, width, null);
            if (cnt >= len - spos) {
                result.append(text.substring(spos));
                break;
            }

            tmp = text.lastIndexOf('\n', spos + cnt - 1);

            if (tmp >= 0 && tmp < spos + cnt) {
                result.append(text.substring(spos, tmp + 1));
                spos += tmp + 1;
            } else {
                tmp = text.lastIndexOf(' ', spos + cnt - 1);
                if (tmp >= spos) {
                    result.append(text.substring(spos, tmp + 1));
                    spos += tmp + 1;
                } else {
                    result.append(text.substring(spos, cnt));
                    spos += cnt;
                }
            }

            hasLines++;
        }

        if (spos < len) {
            result.append(TextUtils.ellipsize(text.subSequence(spos, len), paint, (float) width, where));
        }

        return result.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:enterprayz.megatools.Tools.java

public static int getTextHeight(String text, int maxWidth, float textSize, Typeface typeface) {
    TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
    paint.setTextSize(textSize);/*  ww  w .ja  v  a2 s . com*/
    paint.setTypeface(typeface);

    int lineCount = 0;

    int index = 0;
    int length = text.length();

    while (index < length - 1) {
        index += paint.breakText(text, index, length, true, maxWidth, null);
        lineCount++;
    }

    Rect bounds = new Rect();
    paint.getTextBounds("Py", 0, 2, bounds);
    return (int) Math.floor(lineCount * bounds.height());
}