In Sencha Touch 2.2 instead of using icons with the -webkit-mask trick, Touch is moving to a more cross-browser solution with icon fonts. You can read more about this at Steve Drucker blog Changes to Sencha Touch 2.2 Theming Part 1: Using Iconography.
As Steve mentions on his blog, Touch SDK ships with the Pictos Font, and it already loads some basic icons for you to use directly on buttons with iconCls.
{ xtype: 'button', iconCls: 'home' }
If you want to define more icons from the same Pictos fonts, you just have to use the mixin icon():
@include icon('network');
{ xtype: 'button', iconCls: 'network', iconAlign: 'top', text: 'Net' }
Touch Theming Layer already has a big object on SASS mapping keys into characters. So when you inform the key “network”, the theme knows that it has to use the key “J”.
Adding Custom Icons
To add more icons you’ll need to generate your own font! You might be question yourself – How do I create a new font? Sounds complicated but it’s fairly easy.
There’s a few tools, but my favorite is IcoMoon. With this tool you can select from dozens of fonts and create your own font package, or even import your own SVG vectors.
All you need to do is select your icons, click generate font, and download!
After you have your font, it’s time to add to your project. Add the font into {touch_app}/resources/fonts/, and add this path to your config.rb:
# Compass configurations sass_path = dir css_path = File.join(dir, "..", "css") fonts_path = File.join(dir, "..", "fonts")
Hint: Because we’re defining the fonts_path I had an error like “Pictos fonts not found”. It’s also good to copy the pictos folder from touch/resources/themes/fonts to this new fonts/ directory.
For the last step you just need to include the font and use the icons on your app.scss, like this:
@include icon-font('SenchaConIcons', inline-font-files( 'senchacon-icons/senchacon-icons.woff', woff, 'senchacon-icons/senchacon-icons.ttf', truetype, 'senchacon-icons/senchacon-icons.svg', svg )); @include icon("menu" , "\e009"); @include icon("search", "\e000"); @include icon("star" , "\e001");
Updated June/5: In 2.2.1 you have to specify your font along with icon() mixin. Otherwise it will fallback to ‘Pictos’
@include icon("menu" , "\e009", "SenchaConIcons"); @include icon("search", "\e000", "SenchaConIcons"); @include icon("star" , "\e001", "SenchaConIcons");
Recap
- Use a tool to pick icons and generate the font (IcoMoon, Fontello)
- Add font into new directory {your_app}/resources/fonts
- Copy Pictos font from Touch directory to the same {your_app}/resources/fonts to avoid errors
- Add fonts_path into config.rb so Compass finds your font
- Import the font icon in your app.scss
What about PNG?
PNG is bitmap based, while Fonts are vectorial based. The above mentioned tools provide hundreds of icons, so it’s pretty easy to find an existent font icon pre-built. If that’s not the case, you can convert PNG to SVG and then import into IcoMoon. There are tools for that, like VectorMagic, Online Image Converter or Inkscape. I haven’t try any of these but If you have, please let us know via comment.
RT @pdehaan: Adding Custom Font Icons to #SenchaTouch 2.2 http://t.co/NWxW4weQQN by @extdesenv
Thanks ! Very useful topic !
I have an issue by the way.
When I import my own icons, all the default icons are now define by the return value (for exemple for my home icon is now a big H).
I just want to add new icons, not delete the older, have you an idea to fix this ?
Hey Adrien, that’s a good question that I forgot to mention. The mixin icon-font() will load your font – @include font-face($name, $font-files, $eot) – but also reassign button and tabs default icon font.
Since Touch doesn’t know which icon is coming from which font, it will use a default, but you can override:
@include icon('home', null, 'Pictos');
or
.x-tab .x-button-icon.home:before, .x-button .x-button-icon.home:before {
font-family: 'Pictos';
}
This process will certainly get better on future versions.
Regards!
I lilaletry jumped out of my chair and danced after reading this!
Hi,
I am trying to get FontAwesome Working with Sencha Touch.
i downloaded the font and included as per your tutorial.
@include icon-font(‘FontAwesome’, inline-font-files(
‘FontAwesome/fontawesome-webfont.woff’, woff,
‘FontAwesome/fontawesome-webfont.ttf’, truetype,
‘FontAwesome/fontawesome-webfont.svg’, svg
));
i am confused as to how should i include the icon and use it for a button.
@include icon(“icon-reorder”,”FontAwesome”); This just puts in the icon as “icon-reorder” , How does it know what content to return? it should be returning “f0c9” as expected.
Please let me know where am i going wrong.
Only Picto icons have the mapping name -> key. So if you include a picto icons like this @include icon(‘home’); it knows that “home” maps to “H” (you can find the @function icon-character-for-name inside _Class.scss)
For your icons, you need to inform directly the key. In my example @include icon(“menu”, “e009”);.
Thanks Bruno !
It’s work, that’s perfect !
Regards.
Adding Custom Font Icons to #Sencha Touch 2.2 http://t.co/5f1RLYQSbw
Updated June/5: In 2.2.1 you have to specify your font along with icon() mixin. Otherwise it will fallback to ‘Pictos’
[…] http://bruno.tavares.me/theming/adding-custom-font-icons-to-sencha-touch-2-2/?lang=pt […]
I do this process, it is good in android 4 and above, but in android 2.2, images are appeared like an Chinese word.
What is problem?
This sounds like an issue with the font or the way it’s being included. The Pictos font that ships with the framework works on Android 2.2, so there’s got to be something different going on in your case.
But If you’re sure all is fine in your side, I’d suggest open a bug report on Sencha Touch Forum.
Same problem here.
The only solution I had was to use png instead.
Hi, I not really understand. Here is my sass file
My tabpanel
But I still not able to get the icon. Any solution?
I don’t have a solution. As I mentioned, the best we can do is open a bug report on the forum, attaching the font files you generated and a test case so engineering can test this on the Android 2.2.
Here -> Touch 2 Bug Forum
By @Techtigre comment right below, add \ before the font-icon hexadecimal values. Instead
Try
Great tutorial, however keep in mind that to represent a character, you have to start with a backslash followed by the hexadecimal number that represents the character’s Unicode code point value. So your code: @include icon(“menu” , “e009”, “SenchaConIcons”); should have “\e009” instead.
Good catch, my mistake… Fixed!
Posted more info about theming for devices at http://druckit.wordpress.com/2013/08/12/sencha-touch-2-theming-apps-for-multiple-devices/
can’t get this to work :-(
Do you have the source for this so i can investigate myself? Also, ‘SenchaConIcons’ is user defined right?
I don’t, it’s just snippets. And yes, SenchaConIcons is user defined. What is the error?
Nice to see u
Hi Bruno,
I got this working in my ST 2.2 app, but after porting it to ST 2.3 custom font icons stop working :(
Now I only see the char associated to every font-icon and not the icon itself…
Have you already experienced some problem with the new framework release?
Thanks,
Matteo
any fix for some fonts from icomoom that not perfectly center inside button, such as “calculate” icon ?
Hey man, Would you send me a video about this subject?
Or woff editing?
I’ve recently created this new tool that will help you to prepare icons for your Sencha Touch apps. The README explains the steps for creating icons at the Ico Moon web site and converting the Ico Moon project for use in Sencha Touch.
https://github.com/tohagan/sencha-ico-moon