3

I want to show fontAwesome icons in my app.

I can show with string type icons like this:

<Text style={{fontFamily: 'fontAwesome'}}>&#xf0e8;</Text> print icon. Working well.

But I need to show icons with variable like this:

let icon2 = "&#xf0e8;";
<Text style={{fontFamily: 'fontAwesome'}}>{icon}</Text>

then print to screen &#xf0e8; not icon.

I share with you expo snack link. You can try this easily.

https://snack.expo.io/@wyrustaaruz/Zm9udG

Hasan Rıza Uzuner
  • 445
  • 1
  • 7
  • 21

1 Answers1

9

Instead of

let icon2 = "&#xf0e8;";

you need to define it as;

let icon2 = "\uf0e8";

In javascript, you need to define unicode characters with "\u" when you assign it to a variable. So it can recognize and parse unicode characters properly.

Edited for your question in your comment;

Your icons are hexadecimal. So we can delete first 3 character from icon. After that, we parse that unicode value as integer and we can convert to a unicode string by String.fromCharCode.

icon2 = icon2.substr(3);
icon2 = String.fromCharCode(parseInt(icon2, 16));
sdkcy
  • 3,528
  • 1
  • 16
  • 23
  • I need to replace text with this code. ```icon2 = icon2.replace("","\u"); icon2 = icon2.replace(";","");``` but getting error ```Parsing error: Bad character escape sequence``` because of "\u" – Hasan Rıza Uzuner Feb 07 '19 at 12:50
  • i answered you question in edited answer. Good luck. – sdkcy Feb 07 '19 at 13:13
  • for some who has a unicode like this "0xe926", you can delete 2 characters and it will work perfect like. icon2 = icon2.substr(2); icon2 = String.fromCharCode(parseInt(icon2, 16)); – engr.waqas Mar 01 '20 at 05:47