Example usage for android.text SpannableString SpannableString

List of usage examples for android.text SpannableString SpannableString

Introduction

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

Prototype

public SpannableString(CharSequence source) 

Source Link

Document

For the backward compatibility reasons, this constructor copies all spans including android.text.NoCopySpan .

Usage

From source file:Main.java

public static SpannableString setTextBackground(String content, int startIndex, int endIndex,
        int backgroundColor) {
    if (TextUtils.isEmpty(content) || startIndex < 0 || endIndex >= content.length()
            || startIndex >= endIndex) {
        return null;
    }//ww w  .j ava  2  s  .  c o  m

    SpannableString spannableString = new SpannableString(content);
    spannableString.setSpan(new BackgroundColorSpan(backgroundColor), startIndex, endIndex,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    return spannableString;
}

From source file:Main.java

public static SpannableString setTextImg(String content, int startIndex, int endIndex, Drawable drawable) {
    if (TextUtils.isEmpty(content) || startIndex < 0 || endIndex >= content.length()
            || startIndex >= endIndex) {
        return null;
    }/*w w  w .  j  av a 2s .c  o  m*/

    SpannableString spannableString = new SpannableString(content);
    spannableString.setSpan(new ImageSpan(drawable), startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    return spannableString;
}

From source file:Main.java

public static CharSequence replace(Context ctx, String text) {
    SpannableString spannableString = new SpannableString(text);
    Matcher matcher = pattern.matcher(text);
    while (matcher.find()) {
        String factText = matcher.group();
        String key = factText.substring(1);
        if (emotionTexts.contains(factText)) {
            Bitmap bitmap = getDrawableByName(ctx, key);
            ImageSpan image = new ImageSpan(ctx, bitmap);
            int start = matcher.start();
            int end = matcher.end();
            spannableString.setSpan(image, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }//from  w ww .  j a v a  2 s .com
    }
    return spannableString;
}

From source file:Main.java

public static Spannable applyKerning(CharSequence src, float kerning, int start, int end) {
    if (src == null)
        return null;
    final int srcLength = src.length();
    if (srcLength < 2)
        return src instanceof Spannable ? (Spannable) src : new SpannableString(src);
    if (start < 0)
        start = 0;//from   ww  w.  ja va2  s  . co m
    if (end > srcLength)
        end = srcLength;

    final String nonBreakingSpace = "\u00A0";
    final SpannableStringBuilder builder = src instanceof SpannableStringBuilder ? (SpannableStringBuilder) src
            : new SpannableStringBuilder(src);
    for (int i = src.length(); i >= 1; i--) {
        builder.insert(i, nonBreakingSpace);
        builder.setSpan(new ScaleXSpan(kerning), i, i + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    return builder;
}

From source file:Main.java

public static SpannableString replaceImageSpan(CharSequence charSequence, String regPattern,
        Drawable drawable) {//www.  j a  v a  2 s.co  m
    SpannableString ss = charSequence instanceof SpannableString ? (SpannableString) charSequence
            : new SpannableString(charSequence);
    try {
        ImageSpan is = new ImageSpan(drawable);
        Pattern pattern = Pattern.compile(regPattern);
        Matcher matcher = pattern.matcher(ss);
        while (matcher.find()) {
            String key = matcher.group();
            ss.setSpan(is, matcher.start(), matcher.start() + key.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        }
    } catch (Exception ex) {
        // Logger.e(TAG, ex);
    }

    return ss;
}

From source file:Main.java

/** Strips all rich text except spans used to provide compositional hints. */
public static CharSequence stripRichText(CharSequence str, int start, int end) {
    String plainText = str.toString();
    SpannableString ret = new SpannableString(plainText);
    if (str instanceof Spanned) {
        List<Object> keyboardHintSpans = getComposingSpans((Spanned) str, start, end);
        copySpans((Spanned) str, ret, keyboardHintSpans);
    }/*from   w w  w.ja v a  2  s.  c  om*/
    return ret;
}

From source file:Main.java

public static CharSequence replace(Context ctx, String text) {
    if (text == null) {
        return null;
    }/*from w w  w. ja  v a  2 s .  c o m*/
    SpannableString spannableString = new SpannableString(text);
    Matcher matcher = pattern.matcher(text);
    while (matcher.find()) {
        String factText = matcher.group();
        String key = factText.substring(1);
        if (emotionTexts.contains(factText)) {
            Bitmap bitmap = getDrawableByName(ctx, key);
            ImageSpan image = new ImageSpan(ctx, bitmap);
            int start = matcher.start();
            int end = matcher.end();
            spannableString.setSpan(image, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
    return spannableString;
}

From source file:Main.java

/**
 * toSpannableString//w ww .  j ava2s  . c o  m
 * @return SpannableString
 * @throws
 */
public static SpannableString toSpannableString(Context context, String text) {
    if (!TextUtils.isEmpty(text)) {
        SpannableString spannableString = new SpannableString(text);
        int start = 0;
        Pattern pattern = Pattern.compile("\\\\ue[a-z0-9]{3}", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(text);
        while (matcher.find()) {
            String faceText = matcher.group();
            String key = faceText.substring(1);
            BitmapFactory.Options options = new BitmapFactory.Options();
            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
                    context.getResources().getIdentifier(key, "drawable", context.getPackageName()), options);
            ImageSpan imageSpan = new ImageSpan(context, bitmap);
            int startIndex = text.indexOf(faceText, start);
            int endIndex = startIndex + faceText.length();
            if (startIndex >= 0)
                spannableString.setSpan(imageSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            start = (endIndex - 1);
        }

        return spannableString;
    } else {
        return new SpannableString("");
    }
}

From source file:Main.java

public static CharSequence highlightText(String search, String originalText) {
    if (search != null && !search.equalsIgnoreCase("")) {
        String normalizedText = Normalizer.normalize(originalText, Normalizer.Form.NFD)
                .replaceAll("\\p{InCombiningDiacriticalMarks}+", "").toLowerCase();
        int start = normalizedText.indexOf(search);
        if (start < 0) {
            return originalText;
        } else {//w ww  .  ja  v  a  2  s.  c  o m
            Spannable highlighted = new SpannableString(originalText);
            while (start >= 0) {
                int spanStart = Math.min(start, originalText.length());
                int spanEnd = Math.min(start + search.length(), originalText.length());
                highlighted.setSpan(new ForegroundColorSpan(Color.BLUE), spanStart, spanEnd,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                start = normalizedText.indexOf(search, spanEnd);
            }
            return highlighted;
        }
    }
    return originalText;
}

From source file:Main.java

/**
 * Generate a SpannableString that styles a specific sub-sequence of a CharSequence object.
 *
 * @param sequence    The original text.
 * @param subSequence A sub-sequence of the original text.
 * @param styleSpan   The style to be applied to the sub-sequence.
 * @return A SpannableString with the style applied to only the sub-sequence of the original
 * text./*from   w ww . ja v  a 2s  .c o  m*/
 */
public static SpannableString spanSubstring(CharSequence sequence, CharSequence subSequence,
        StyleSpan styleSpan) {
    SpannableString spannableString = new SpannableString(sequence);
    int startIndex = spannableString.toString().indexOf(subSequence.toString());
    int endIndex = startIndex + subSequence.toString().length();
    spannableString.setSpan(styleSpan, startIndex, endIndex, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    return spannableString;
}