Search in sources :

Example 36 with SpannableString

use of android.text.SpannableString in project Jota-Text-Editor-old by jiro-aqua.

the class TextUtils method ellipsize.

/**
     * Returns the original text if it fits in the specified width
     * given the properties of the specified Paint,
     * or, if it does not fit, a copy with ellipsis character added 
     * at the specified edge or center.
     * If <code>preserveLength</code> is specified, the returned copy
     * will be padded with zero-width spaces to preserve the original
     * length and offsets instead of truncating.
     * If <code>callback</code> is non-null, it will be called to
     * report the start and end of the ellipsized range.
     */
public static CharSequence ellipsize(CharSequence text, TextPaint p, float avail, TruncateAt where, boolean preserveLength, EllipsizeCallback callback) {
    if (sEllipsis == null) {
        Resources r = Resources.getSystem();
        sEllipsis = r.getString(R.string.ellipsis);
    }
    int len = text.length();
    if (!(text instanceof Spanned)) {
        float wid = p.measureText(text, 0, len);
        if (wid <= avail) {
            if (callback != null) {
                callback.ellipsized(0, 0);
            }
            return text;
        }
        float ellipsiswid = p.measureText(sEllipsis);
        if (ellipsiswid > avail) {
            if (callback != null) {
                callback.ellipsized(0, len);
            }
            if (preserveLength) {
                char[] buf = obtain(len);
                for (int i = 0; i < len; i++) {
                    buf[i] = '';
                }
                String ret = new String(buf, 0, len);
                recycle(buf);
                return ret;
            } else {
                return "";
            }
        }
        if (where == TruncateAt.START) {
            int fit = p.breakText(text, 0, len, false, avail - ellipsiswid, null);
            if (callback != null) {
                callback.ellipsized(0, len - fit);
            }
            if (preserveLength) {
                return blank(text, 0, len - fit);
            } else {
                return sEllipsis + text.toString().substring(len - fit, len);
            }
        } else if (where == TruncateAt.END) {
            int fit = p.breakText(text, 0, len, true, avail - ellipsiswid, null);
            if (callback != null) {
                callback.ellipsized(fit, len);
            }
            if (preserveLength) {
                return blank(text, fit, len);
            } else {
                return text.toString().substring(0, fit) + sEllipsis;
            }
        } else /* where == TruncateAt.MIDDLE */
        {
            int right = p.breakText(text, 0, len, false, (avail - ellipsiswid) / 2, null);
            float used = p.measureText(text, len - right, len);
            int left = p.breakText(text, 0, len - right, true, avail - ellipsiswid - used, null);
            if (callback != null) {
                callback.ellipsized(left, len - right);
            }
            if (preserveLength) {
                return blank(text, left, len - right);
            } else {
                String s = text.toString();
                return s.substring(0, left) + sEllipsis + s.substring(len - right, len);
            }
        }
    }
    // But do the Spanned cases by hand, because it's such a pain
    // to iterate the span transitions backwards and getTextWidths()
    // will give us the information we need.
    // getTextWidths() always writes into the start of the array,
    // so measure each span into the first half and then copy the
    // results into the second half to use later.
    float[] wid = new float[len * 2];
    TextPaint temppaint = new TextPaint();
    Spanned sp = (Spanned) text;
    int next;
    for (int i = 0; i < len; i = next) {
        next = sp.nextSpanTransition(i, len, MetricAffectingSpan.class);
        Styled.getTextWidths(p, temppaint, sp, i, next, wid, null);
        System.arraycopy(wid, 0, wid, len + i, next - i);
    }
    float sum = 0;
    for (int i = 0; i < len; i++) {
        sum += wid[len + i];
    }
    if (sum <= avail) {
        if (callback != null) {
            callback.ellipsized(0, 0);
        }
        return text;
    }
    float ellipsiswid = p.measureText(sEllipsis);
    if (ellipsiswid > avail) {
        if (callback != null) {
            callback.ellipsized(0, len);
        }
        if (preserveLength) {
            char[] buf = obtain(len);
            for (int i = 0; i < len; i++) {
                buf[i] = '';
            }
            SpannableString ss = new SpannableString(new String(buf, 0, len));
            recycle(buf);
            copySpansFrom(sp, 0, len, Object.class, ss, 0);
            return ss;
        } else {
            return "";
        }
    }
    if (where == TruncateAt.START) {
        sum = 0;
        int i;
        for (i = len; i >= 0; i--) {
            float w = wid[len + i - 1];
            if (w + sum + ellipsiswid > avail) {
                break;
            }
            sum += w;
        }
        if (callback != null) {
            callback.ellipsized(0, i);
        }
        if (preserveLength) {
            SpannableString ss = new SpannableString(blank(text, 0, i));
            copySpansFrom(sp, 0, len, Object.class, ss, 0);
            return ss;
        } else {
            SpannableStringBuilder out = new SpannableStringBuilder(sEllipsis);
            out.insert(1, text, i, len);
            return out;
        }
    } else if (where == TruncateAt.END) {
        sum = 0;
        int i;
        for (i = 0; i < len; i++) {
            float w = wid[len + i];
            if (w + sum + ellipsiswid > avail) {
                break;
            }
            sum += w;
        }
        if (callback != null) {
            callback.ellipsized(i, len);
        }
        if (preserveLength) {
            SpannableString ss = new SpannableString(blank(text, i, len));
            copySpansFrom(sp, 0, len, Object.class, ss, 0);
            return ss;
        } else {
            SpannableStringBuilder out = new SpannableStringBuilder(sEllipsis);
            out.insert(0, text, 0, i);
            return out;
        }
    } else /* where = TruncateAt.MIDDLE */
    {
        float lsum = 0, rsum = 0;
        int left = 0, right = len;
        float ravail = (avail - ellipsiswid) / 2;
        for (right = len; right >= 0; right--) {
            float w = wid[len + right - 1];
            if (w + rsum > ravail) {
                break;
            }
            rsum += w;
        }
        float lavail = avail - ellipsiswid - rsum;
        for (left = 0; left < right; left++) {
            float w = wid[len + left];
            if (w + lsum > lavail) {
                break;
            }
            lsum += w;
        }
        if (callback != null) {
            callback.ellipsized(left, right);
        }
        if (preserveLength) {
            SpannableString ss = new SpannableString(blank(text, left, right));
            copySpansFrom(sp, 0, len, Object.class, ss, 0);
            return ss;
        } else {
            SpannableStringBuilder out = new SpannableStringBuilder(sEllipsis);
            out.insert(0, text, 0, left);
            out.insert(out.length(), text, right, len);
            return out;
        }
    }
}
Also used : SpannableString(android.text.SpannableString) Resources(android.content.res.Resources) SpannedString(android.text.SpannedString) SpannableString(android.text.SpannableString) Spanned(android.text.Spanned) TextPaint(android.text.TextPaint) MetricAffectingSpan(jp.sblo.pandora.jota.text.style.MetricAffectingSpan) TextPaint(android.text.TextPaint)

Example 37 with SpannableString

use of android.text.SpannableString in project Jota-Text-Editor-old by jiro-aqua.

the class TextUtils method concat.

/**
     * Returns a CharSequence concatenating the specified CharSequences,
     * retaining their spans if any.
     */
public static CharSequence concat(CharSequence... text) {
    if (text.length == 0) {
        return "";
    }
    if (text.length == 1) {
        return text[0];
    }
    boolean spanned = false;
    for (int i = 0; i < text.length; i++) {
        if (text[i] instanceof Spanned) {
            spanned = true;
            break;
        }
    }
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < text.length; i++) {
        sb.append(text[i]);
    }
    if (!spanned) {
        return sb.toString();
    }
    SpannableString ss = new SpannableString(sb);
    int off = 0;
    for (int i = 0; i < text.length; i++) {
        int len = text[i].length();
        if (text[i] instanceof Spanned) {
            copySpansFrom((Spanned) text[i], 0, len, Object.class, ss, off);
        }
        off += len;
    }
    return new SpannedString(ss);
}
Also used : SpannableString(android.text.SpannableString) SpannedString(android.text.SpannedString) Spanned(android.text.Spanned) TextPaint(android.text.TextPaint)

Example 38 with SpannableString

use of android.text.SpannableString in project Jota-Text-Editor-old by jiro-aqua.

the class TextView method onSaveInstanceState.

@Override
public Parcelable onSaveInstanceState() {
    Parcelable superState = super.onSaveInstanceState();
    // Save state if we are forced to
    boolean save = mFreezesText;
    int start = 0;
    int end = 0;
    if (mText != null) {
        start = getSelectionStart();
        end = getSelectionEnd();
        if (start >= 0 || end >= 0) {
            // Or save state if there is a selection
            save = true;
        }
    }
    if (save) {
        SavedState ss = new SavedState(superState);
        // XXX Should also save the current scroll position!
        ss.selStart = start;
        ss.selEnd = end;
        if (mText instanceof Spanned) {
            /*
                 * Calling setText() strips off any ChangeWatchers;
                 * strip them now to avoid leaking references.
                 * But do it to a copy so that if there are any
                 * further changes to the text of this view, it
                 * won't get into an inconsistent state.
                 */
            Spannable sp = new SpannableString(mText);
            for (ChangeWatcher cw : sp.getSpans(0, sp.length(), ChangeWatcher.class)) {
                sp.removeSpan(cw);
            }
            ss.text = sp;
        } else {
            ss.text = mText.toString();
        }
        if (isFocused() && start >= 0 && end >= 0) {
            ss.frozenWithFocus = true;
        }
        ss.error = mError;
        // Jota Text Editor
        ss.undoBuffer = mUndoBuffer;
        // Jota Text Editor
        ss.redoBuffer = mRedoBuffer;
        // Jota Text Editor
        ss.imeShown = isImeShown();
        return ss;
    }
    return superState;
}
Also used : SpannableString(android.text.SpannableString) Parcelable(android.os.Parcelable) Spanned(android.text.Spanned) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) Spannable(android.text.Spannable)

Example 39 with SpannableString

use of android.text.SpannableString in project ZhihuDailyPurify by izzyleung.

the class IzzySearchView method getDecoratedHint.

private CharSequence getDecoratedHint(CharSequence hintText) {
    Spannable ssb = new SpannableString(hintText);
    //noinspection deprecation
    ssb.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.search_view_hint_color)), 0, hintText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return ssb;
}
Also used : SpannableString(android.text.SpannableString) ForegroundColorSpan(android.text.style.ForegroundColorSpan) Spannable(android.text.Spannable)

Example 40 with SpannableString

use of android.text.SpannableString in project Talon-for-Twitter by klinker24.

the class EmojiUtils method getSmiledText.

public static Spannable getSmiledText(Context context, CharSequence text) {
    try {
        Spannable spannable = spannableFactory.newSpannable(text);
        addSmiles(context, spannable);
        return spannable;
    } catch (Exception e) {
        e.printStackTrace();
        return new SpannableString("");
    }
}
Also used : SpannableString(android.text.SpannableString) Spannable(android.text.Spannable)

Aggregations

SpannableString (android.text.SpannableString)325 TextView (android.widget.TextView)61 Spannable (android.text.Spannable)60 View (android.view.View)57 StyleSpan (android.text.style.StyleSpan)53 ForegroundColorSpan (android.text.style.ForegroundColorSpan)47 TextPaint (android.text.TextPaint)25 Spanned (android.text.Spanned)24 TextAppearanceSpan (android.text.style.TextAppearanceSpan)21 Paint (android.graphics.Paint)20 RelativeSizeSpan (android.text.style.RelativeSizeSpan)19 ImageView (android.widget.ImageView)19 Bundle (android.os.Bundle)17 TypedValue (android.util.TypedValue)17 SmallTest (android.test.suitebuilder.annotation.SmallTest)16 Intent (android.content.Intent)15 SpannableStringBuilder (android.text.SpannableStringBuilder)15 ClickableSpan (android.text.style.ClickableSpan)14 URLSpan (android.text.style.URLSpan)13 LayoutInflater (android.view.LayoutInflater)13