package com.eqot.fontawesome;

import android.content.Context;
import android.graphics.Typeface;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.view.View;
import android.widget.TextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FontAwesome {
    private static final String FONT_FILENAME = "fontawesome-webfont.ttf";

    private static Typeface sTypeface = null;
    private static final Pattern pattern = Pattern.compile("([^\\{]*)\\{([\\w\\-]+)\\}(.*)");

    public static void apply(Context context, View view) {
        if (sTypeface == null) {
            sTypeface = Typeface.createFromAsset(context.getAssets(), FONT_FILENAME);
        }

        final TextView textView = (TextView) view;
        String text = (String) textView.getText();

        final SpannableStringBuilder sb = new SpannableStringBuilder();
        while (true) {
            final Matcher matcher = pattern.matcher(text);
            if (!matcher.find()) {
                sb.append(text);
                break;
            }

            sb.append(matcher.group(1));

            final String orgCode = matcher.group(2);
            final String code = orgCode.replace("-", "_");
            final int id = context.getResources().getIdentifier(code, "string", context.getPackageName());
            final String string = context.getResources().getString(id);
            sb.append(string, new CustomTypefaceSpan("", sTypeface), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            text = matcher.group(3);
        }

        textView.setText(sb);
    }
}

@@ -56,16 +56,54 @@
 ontext;%0A
+import android.content.res.Resources;%0A
 import a
@@ -1192,17 +1192,49 @@
 ing 
-orgCode =
+character = getCharacterFromCode(context,
  mat
@@ -1246,16 +1246,17 @@
 group(2)
+)
 ;%0A      
@@ -1271,46 +1271,78 @@
 nal 
-String code = orgCode.replace
+CustomTypefaceSpan typefaceSpan = new CustomTypefaceSpan
 (%22
--
 %22, 
-%22_%22
+sTypeface
 );%0A 
@@ -1356,288 +1356,441 @@
     
-final int id = context.getResources().getIdentifier(code, %22string%22, context.getPackageName());%0A            final String string = context.getResources().getString(id);%0A            sb.append(
+sb.append(character, typefaceSpan, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);%0A%0A            text = matcher.group(3);%0A        %7D%0A%0A        textView.setText(sb);%0A    %7D%0A%0A    private static String getCharacterFromCode(Context context, String code) %7B%0A        code = code.replace(%22-%22, %22_%22);%0A%0A        final Resources resources = context.getResources();%0A        final int id = resources.getIdentifier(code, %22
 string
+%22
 , 
-new CustomTypefaceSpan(%22%22, sTypeface), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
+context.getPackageName()
 );%0A
-%0A
         
     
@@ -1789,76 +1789,83 @@
     
-    text = matcher.group(3);%0A        %7D%0A%0A        textView.setText(sb)
+final String character = resources.getString(id);%0A%0A        return character
 ;%0A  

Refactor code