Android Context Get getSmiledText(Context context, CharSequence text)

Here you can find the source of getSmiledText(Context context, CharSequence text)

Description

get Smiled Text

Declaration

public static Spannable getSmiledText(Context context, CharSequence text) 

Method Source Code

//package com.java2s;
import android.content.Context;
import android.text.Spannable;
import android.text.style.ImageSpan;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static final Spannable.Factory spannableFactory = Spannable.Factory
            .getInstance();//from   ww  w .j  av a 2  s.co m
    private static final Map<Pattern, Integer> emoticons = new HashMap<>(0);

    public static Spannable getSmiledText(Context context, CharSequence text) {
        Spannable spannable = spannableFactory.newSpannable(text);
        addSmiles(context, spannable);
        return spannable;
    }

    public static boolean addSmiles(Context context, Spannable spannable) {
        boolean hasChanges = false;
        for (Map.Entry<Pattern, Integer> entry : emoticons.entrySet()) {
            Matcher matcher = entry.getKey().matcher(spannable);
            while (matcher.find()) {
                boolean set = true;
                for (ImageSpan span : spannable.getSpans(matcher.start(),
                        matcher.end(), ImageSpan.class)) {
                    if (spannable.getSpanStart(span) >= matcher.start()
                            && spannable.getSpanEnd(span) <= matcher.end()) {
                        spannable.removeSpan(span);
                    } else {
                        set = false;
                        break;
                    }
                }
                if (set) {
                    hasChanges = true;
                    spannable.setSpan(
                            new ImageSpan(context, entry.getValue()),
                            matcher.start(), matcher.end(),
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
        return hasChanges;
    }
}

Related

  1. getSeed(Context context)
  2. getSelectedLangCodes(Context context, int[] indexes, boolean[] selectedItems, int codeResourceId)
  3. getServiceMetaData(Context context, Class serviceClass, String key)
  4. getSignatures(final Context context)
  5. getSizeName(Context context)
  6. getSmsId(Context context)
  7. getString(Context context, String key, String defValue)
  8. getString(Context context, int resourceId)
  9. getStringFromResource(int resource, Context context)