Search in sources :

Example 21 with SpannedString

use of android.text.SpannedString in project react-native-navigation by wix.

the class TextViewUtils method getTextSize.

public static float getTextSize(TextView view) {
    SpannedString text = new SpannedString(view.getText());
    AbsoluteSizeSpan[] spans = text.getSpans(0, text.length(), AbsoluteSizeSpan.class);
    return spans.length == 0 ? -1 : spans[0].getSize();
}
Also used : SpannedString(android.text.SpannedString) AbsoluteSizeSpan(android.text.style.AbsoluteSizeSpan)

Example 22 with SpannedString

use of android.text.SpannedString in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class SpannableStringUtils method concatWithNonParagraphSuggestionSpansOnly.

/**
 * Returns a CharSequence concatenating the specified CharSequences, retaining their
 * SuggestionSpans that don't have the PARAGRAPH flag, but not other spans.
 *
 * This code is almost entirely taken from {@link TextUtils#concat(CharSequence...)}, except
 * it calls copyNonParagraphSuggestionSpansFrom instead of {@link TextUtils#copySpansFrom}.
 */
public static CharSequence concatWithNonParagraphSuggestionSpansOnly(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) {
            copyNonParagraphSuggestionSpansFrom((Spanned) text[i], 0, len, ss, off);
        }
        off += len;
    }
    return new SpannedString(ss);
}
Also used : SpannableString(android.text.SpannableString) SpannedString(android.text.SpannedString) Spanned(android.text.Spanned)

Example 23 with SpannedString

use of android.text.SpannedString in project android_packages_apps_Dialer by LineageOS.

the class DialerBidiFormatter method format.

/**
 * Divides the given text into segments, applies LTR formatting and adds TTS span to segments that
 * are phone numbers, then reassembles the text.
 *
 * <p>Formatted phone numbers usually contain one or more whitespaces (e.g., "+1 650-253-0000",
 * "(650) 253-0000", etc). The system mistakes such a number for tokens separated by whitespaces.
 * Therefore, these numbers can't be correctly shown in a RTL context (e.g., "+1 650-253-0000"
 * would be shown as "650-253-0000 1+".)
 *
 * <p>This method wraps phone numbers with Unicode formatting characters LRE & PDF to ensure phone
 * numbers are always shown as LTR strings.
 *
 * <p>Note that the regex used to find phone numbers ({@link Patterns#PHONE}) will also match any
 * number. As this method also adds TTS span to segments that match {@link Patterns#PHONE}, extra
 * actions need to be taken if you don't want a number to be read as a phone number by TalkBack.
 */
public static CharSequence format(@Nullable CharSequence text) {
    if (TextUtils.isEmpty(text)) {
        return text;
    }
    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
    // Find the start index and the end index of each segment matching the phone number pattern.
    Matcher matcher = Patterns.PHONE.matcher(text.toString());
    int currIndex = 0;
    while (matcher.find()) {
        int start = matcher.start();
        int end = matcher.end();
        // Handle the case where the input text doesn't start with a phone number.
        if (currIndex < start) {
            spannableStringBuilder.append(text.subSequence(currIndex, start));
        }
        // For a phone number, wrap it with Unicode characters LRE & PDF so that it will always be
        // shown as a LTR string.
        spannableStringBuilder.append(PhoneNumberUtils.createTtsSpannable(TextUtils.concat(String.valueOf(LRE), text.subSequence(start, end), String.valueOf(PDF))));
        currIndex = end;
    }
    // Handle the case where the input doesn't end with a phone number.
    if (currIndex < text.length()) {
        spannableStringBuilder.append(text.subSequence(currIndex, text.length()));
    }
    return new SpannedString(spannableStringBuilder);
}
Also used : Matcher(java.util.regex.Matcher) SpannedString(android.text.SpannedString) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 24 with SpannedString

use of android.text.SpannedString in project AndroidLife by CaMnter.

the class Util method getTextViewImageSpanBitmap.

private static List<Pair<String, Bitmap>> getTextViewImageSpanBitmap(TextView textView) {
    List<Pair<String, Bitmap>> bitmaps = new ArrayList<>();
    try {
        CharSequence text = textView.getText();
        if (text instanceof SpannedString) {
            Field mSpansField = Class.forName("android.text.SpannableStringInternal").getDeclaredField("mSpans");
            mSpansField.setAccessible(true);
            Object[] spans = (Object[]) mSpansField.get(text);
            for (Object span : spans) {
                if (span instanceof ImageSpan) {
                    bitmaps.add(new Pair<>("SpanBitmap", getDrawableBitmap(((ImageSpan) span).getDrawable())));
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmaps;
}
Also used : Field(java.lang.reflect.Field) SpannedString(android.text.SpannedString) ArrayList(java.util.ArrayList) Pair(android.util.Pair) ImageSpan(android.text.style.ImageSpan)

Example 25 with SpannedString

use of android.text.SpannedString in project SmartNews by JavenLu.

the class SearchView method setHint.

private void setHint() {
    SpannableString string = new SpannableString(textHint);
    AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(hintTextSize, false);
    string.setSpan(absoluteSizeSpan, 0, string.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    editTextClear.setHint(new SpannedString(string));
}
Also used : SpannableString(android.text.SpannableString) SpannedString(android.text.SpannedString) AbsoluteSizeSpan(android.text.style.AbsoluteSizeSpan)

Aggregations

SpannedString (android.text.SpannedString)41 Test (org.junit.Test)15 Spanned (android.text.Spanned)12 Subtitle (com.google.android.exoplayer2.text.Subtitle)12 SpannableStringBuilder (android.text.SpannableStringBuilder)10 SpannableString (android.text.SpannableString)9 LocaleData (libcore.icu.LocaleData)6 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)4 TextPaint (android.text.TextPaint)2 ForegroundColorSpan (android.text.style.ForegroundColorSpan)2 ArrayList (java.util.ArrayList)2 SuppressLint (android.annotation.SuppressLint)1 NotificationChannel (android.app.NotificationChannel)1 ConversationChannel (android.app.people.ConversationChannel)1 ShortcutInfo (android.content.pm.ShortcutInfo)1 NonNull (android.support.annotation.NonNull)1 Spannable (android.text.Spannable)1 StaticLayout (android.text.StaticLayout)1 ImageSpan (android.text.style.ImageSpan)1 Pair (android.util.Pair)1