Skip to content

Instantly share code, notes, and snippets.

@oscarcidcoder
Created April 17, 2019 13:41
Show Gist options
  • Save oscarcidcoder/20da459f32956dc872bfce61e7426e33 to your computer and use it in GitHub Desktop.
Save oscarcidcoder/20da459f32956dc872bfce61e7426e33 to your computer and use it in GitHub Desktop.
Class to support TextView with FontAwesome
/**
* Clase para cargar los iconos de FontAwesome v4.7.0
* Usar {@link FontAwesomeIcon#setCodeText(String)} para hacer el set del texto en el codigo
* @author oscarcid
*/
public class FontAwesomeIcon extends AppCompatTextView {
int c1,c2,c3;
Shader shader;
public FontAwesomeIcon(Context context) {
super(context);
intitTypeFace();
}
public FontAwesomeIcon(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
intitTypeFace();
}
public FontAwesomeIcon(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
intitTypeFace();
}
private void intitTypeFace(){
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),"fontawesome-webfont.ttf");
setTypeface(tf);
setLayerType(LAYER_TYPE_SOFTWARE,null);
createShader();
}
public void setCodeText(String textCode) {
if (!textCode.equalsIgnoreCase("null")) {
String finalText = "";
if (textCode.contains("&#")) {
String replaceText = textCode.replace("&#x","").replace(";","");
finalText = new String(Character.toChars(Integer.parseInt(replaceText,16)));
} else if (textCode.length()>4) {
int sizeText = textCode.length();
String replaceText = textCode.substring(sizeText - 4);
finalText = new String(Character.toChars(Integer.parseInt(replaceText,16)));
} else {
finalText = new String(Character.toChars(Integer.parseInt(textCode,16)));
}
setText(finalText);
}
}
private void createShader(){
c1 = getResources().getColor(R.color.gradient_color_top);
c2 = getResources().getColor(R.color.gradient_color_middle);
c3 = getResources().getColor(R.color.gradient_color_bottom);
/*shader = new LinearGradient(0, 0, getTextSize(), getTextSize(),
new int[]{c3, c2, c1},
null, Shader.TileMode.CLAMP);*/
shader = new LinearGradient(0, 0, 0, getTextSize(),
new int[]{c3, c2, c1},
null, Shader.TileMode.MIRROR);
getPaint().setShader(shader);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment