Example usage for android.text SpannableStringBuilder insert

List of usage examples for android.text SpannableStringBuilder insert

Introduction

In this page you can find the example usage for android.text SpannableStringBuilder insert.

Prototype

public SpannableStringBuilder insert(int where, CharSequence tb, int start, int end) 

Source Link

Usage

From source file:com.jecelyin.editor.v2.core.text.TextUtils.java

/**
 * @hide/*from   w ww .  j a  v  a2 s. c  o  m*/
 */
public static CharSequence commaEllipsize(CharSequence text, TextPaint p, float avail, String oneMore,
        String more, TextDirectionHeuristic textDir) {

    MeasuredText mt = MeasuredText.obtain();
    try {
        int len = text.length();
        float width = setPara(mt, p, text, 0, len, textDir);
        if (width <= avail) {
            return text;
        }

        char[] buf = mt.mChars;

        int commaCount = 0;
        for (int i = 0; i < len; i++) {
            if (buf[i] == ',') {
                commaCount++;
            }
        }

        int remaining = commaCount + 1;

        int ok = 0;
        String okFormat = "";

        int w = 0;
        int count = 0;
        float[] widths = mt.mWidths;

        MeasuredText tempMt = MeasuredText.obtain();
        for (int i = 0; i < len; i++) {
            w += widths[i];

            if (buf[i] == ',') {
                count++;

                String format;
                // XXX should not insert spaces, should be part of string
                // XXX should use plural rules and not assume English plurals
                if (--remaining == 1) {
                    format = " " + oneMore;
                } else {
                    format = " " + String.format(more, remaining);
                }

                // XXX this is probably ok, but need to look at it more
                //                    tempMt.setPara(format, 0, format.length(), textDir); //jec-
                float moreWid = tempMt.addStyleRun(p, tempMt.mLen, null);

                if (w + moreWid <= avail) {
                    ok = i + 1;
                    okFormat = format;
                }
            }
        }
        MeasuredText.recycle(tempMt);

        SpannableStringBuilder out = new SpannableStringBuilder(okFormat);
        out.insert(0, text, 0, ok);
        return out;
    } finally {
        MeasuredText.recycle(mt);
    }
}