Search in sources :

Example 61 with Spanned

use of android.text.Spanned in project weex-example by KalicyZhou.

the class WXTextDomObject method truncate.

@NonNull
public String truncate(@Nullable String source, @NonNull TextPaint paint, int desired, @Nullable TextUtils.TruncateAt truncateAt) {
    if (!TextUtils.isEmpty(source)) {
        StringBuilder builder;
        Spanned spanned;
        StaticLayout layout;
        for (int i = source.length(); i > 0; i--) {
            builder = new StringBuilder(i + 1);
            builder.append(source, 0, i);
            if (truncateAt != null) {
                builder.append(ELLIPSIS);
            }
            spanned = createSpanned(builder.toString());
            layout = new StaticLayout(spanned, paint, desired, Layout.Alignment.ALIGN_NORMAL, 1, 0, true);
            if (layout.getLineCount() <= 1) {
                return spanned.toString();
            }
        }
    }
    return "";
}
Also used : StaticLayout(android.text.StaticLayout) Spanned(android.text.Spanned) TextPaint(android.text.TextPaint) NonNull(android.support.annotation.NonNull)

Example 62 with Spanned

use of android.text.Spanned in project android_frameworks_base by DirtyUnicorns.

the class TextView method removeSuggestionSpans.

/**
     * Removes the suggestion spans.
     */
CharSequence removeSuggestionSpans(CharSequence text) {
    if (text instanceof Spanned) {
        Spannable spannable;
        if (text instanceof Spannable) {
            spannable = (Spannable) text;
        } else {
            spannable = new SpannableString(text);
            text = spannable;
        }
        SuggestionSpan[] spans = spannable.getSpans(0, text.length(), SuggestionSpan.class);
        for (int i = 0; i < spans.length; i++) {
            spannable.removeSpan(spans[i]);
        }
    }
    return text;
}
Also used : SpannableString(android.text.SpannableString) SuggestionSpan(android.text.style.SuggestionSpan) Spanned(android.text.Spanned) Spannable(android.text.Spannable) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 63 with Spanned

use of android.text.Spanned in project android_frameworks_base by ParanoidAndroid.

the class Editor method extractTextInternal.

private boolean extractTextInternal(ExtractedTextRequest request, int partialStartOffset, int partialEndOffset, int delta, ExtractedText outText) {
    final CharSequence content = mTextView.getText();
    if (content != null) {
        if (partialStartOffset != EXTRACT_NOTHING) {
            final int N = content.length();
            if (partialStartOffset < 0) {
                outText.partialStartOffset = outText.partialEndOffset = -1;
                partialStartOffset = 0;
                partialEndOffset = N;
            } else {
                // Now use the delta to determine the actual amount of text
                // we need.
                partialEndOffset += delta;
                // Adjust offsets to ensure we contain full spans.
                if (content instanceof Spanned) {
                    Spanned spanned = (Spanned) content;
                    Object[] spans = spanned.getSpans(partialStartOffset, partialEndOffset, ParcelableSpan.class);
                    int i = spans.length;
                    while (i > 0) {
                        i--;
                        int j = spanned.getSpanStart(spans[i]);
                        if (j < partialStartOffset)
                            partialStartOffset = j;
                        j = spanned.getSpanEnd(spans[i]);
                        if (j > partialEndOffset)
                            partialEndOffset = j;
                    }
                }
                outText.partialStartOffset = partialStartOffset;
                outText.partialEndOffset = partialEndOffset - delta;
                if (partialStartOffset > N) {
                    partialStartOffset = N;
                } else if (partialStartOffset < 0) {
                    partialStartOffset = 0;
                }
                if (partialEndOffset > N) {
                    partialEndOffset = N;
                } else if (partialEndOffset < 0) {
                    partialEndOffset = 0;
                }
            }
            if ((request.flags & InputConnection.GET_TEXT_WITH_STYLES) != 0) {
                outText.text = content.subSequence(partialStartOffset, partialEndOffset);
            } else {
                outText.text = TextUtils.substring(content, partialStartOffset, partialEndOffset);
            }
        } else {
            outText.partialStartOffset = 0;
            outText.partialEndOffset = 0;
            outText.text = "";
        }
        outText.flags = 0;
        if (MetaKeyKeyListener.getMetaState(content, MetaKeyKeyListener.META_SELECTING) != 0) {
            outText.flags |= ExtractedText.FLAG_SELECTING;
        }
        if (mTextView.isSingleLine()) {
            outText.flags |= ExtractedText.FLAG_SINGLE_LINE;
        }
        outText.startOffset = 0;
        outText.selectionStart = mTextView.getSelectionStart();
        outText.selectionEnd = mTextView.getSelectionEnd();
        return true;
    }
    return false;
}
Also used : Spanned(android.text.Spanned) Paint(android.graphics.Paint)

Example 64 with Spanned

use of android.text.Spanned in project android_frameworks_base by ParanoidAndroid.

the class TextView method removeSuggestionSpans.

/**
     * Removes the suggestion spans.
     */
CharSequence removeSuggestionSpans(CharSequence text) {
    if (text instanceof Spanned) {
        Spannable spannable;
        if (text instanceof Spannable) {
            spannable = (Spannable) text;
        } else {
            spannable = new SpannableString(text);
            text = spannable;
        }
        SuggestionSpan[] spans = spannable.getSpans(0, text.length(), SuggestionSpan.class);
        for (int i = 0; i < spans.length; i++) {
            spannable.removeSpan(spans[i]);
        }
    }
    return text;
}
Also used : SpannableString(android.text.SpannableString) SuggestionSpan(android.text.style.SuggestionSpan) Spanned(android.text.Spanned) Spannable(android.text.Spannable) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 65 with Spanned

use of android.text.Spanned in project android_frameworks_base by ParanoidAndroid.

the class DrawableMarginSpan method drawLeadingMargin.

public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom, CharSequence text, int start, int end, boolean first, Layout layout) {
    int st = ((Spanned) text).getSpanStart(this);
    int ix = (int) x;
    int itop = (int) layout.getLineTop(layout.getLineForOffset(st));
    int dw = mDrawable.getIntrinsicWidth();
    int dh = mDrawable.getIntrinsicHeight();
    // XXX What to do about Paint?
    mDrawable.setBounds(ix, itop, ix + dw, itop + dh);
    mDrawable.draw(c);
}
Also used : Spanned(android.text.Spanned) Paint(android.graphics.Paint)

Aggregations

Spanned (android.text.Spanned)204 Paint (android.graphics.Paint)81 TextPaint (android.text.TextPaint)63 Spannable (android.text.Spannable)32 SpannableStringBuilder (android.text.SpannableStringBuilder)27 SpannableString (android.text.SpannableString)25 SuggestionSpan (android.text.style.SuggestionSpan)24 Editable (android.text.Editable)22 TextAppearanceSpan (android.text.style.TextAppearanceSpan)15 SpannedString (android.text.SpannedString)14 TypedArray (android.content.res.TypedArray)12 URLSpan (android.text.style.URLSpan)12 View (android.view.View)12 Context (android.content.Context)10 StyleSpan (android.text.style.StyleSpan)9 InputMethodManager (android.view.inputmethod.InputMethodManager)9 TextView (android.widget.TextView)9 Parcelable (android.os.Parcelable)8 WordIterator (android.text.method.WordIterator)7 CharacterStyle (android.text.style.CharacterStyle)7