Let me start off by saying that the folks over at Ionic are doing a great job. They are responsive to user issues, requests, and questions.
However, one thing that I believe the Ionic 2 Framework needs more of are... icons.
How could you have too many, right?
Here, I document the way I set up an Ionic project to handle the element:
to render something like
thumbs_up_downfacethumbs_up_down
for offline use.
Summary
- Start an Ionic project
- Get the Material Icons font files, and place them in /app
- Modify gulpfile.js to accept your new Icon files
- Add CSS in order to declare the HTML elements
- Declare the Material Icons in your project HTML
The lines
I assume you have node.js, npm and Ionic configured for simple Ionic Project generation. If not, I will be including a short description on how to get started (or you can visit the Ionic Quickstart Page).
Depending on the way you set up NPM on your device, you might get an error
and you will have to use
Your project should be up, running and viewable in your browser.
Getting the icons
Navigate to the Material-Icons repository, and download the latest woff, woff2, wot, ttf, svg, and css file.
Once downloaded, we need to include them in our app. To do that, we need to understand how the app works.
In Ionic2, the file structure setup when you run the command , is relatively straight forward. For our purposes in this tutorial, the basic structure goes like this:
Yes, I know that there are other things in your file. But let's KISS (keep it stupid simple), and pretend we can not see those other files at the moment.
Now, you may be asking yourself, "why don't we just put all code into the /www folder?"
Experiment Time!
I strongly encourage you to try to put the Material Icons files we downloaded earlier, directly into the /www file.
If you do, make sure to copy the files and don't just drag and drop. You'll thank me later.
Once you re-start the server and see the app in your browser, look back in your editor. Checkout your /www file and search for the Material Icons file you placed in there. You will notice something that really frustrated me the first time I tried this, but it taught me a lot about the process of building an app.
If you do end up going on with this experiment, let us know what happened!
Note: I doubt anything destructive will occur, but if you do something and you can't get your app to function anymore, don't sweat! Just revert back to your last commit, or restart the app.
Put them in the right place
While placing the font files into /www is interesting, it is clearly not the right place.
If you did the experiment, you should have noticed that the Material Icons folders disappeared when the changes were saved. The reason lies in gulpfile.js and we will get to that in a bit.
The "right" place to put the font-files is is not well documented at this point in Ionic2. Therefore, I went ahead and created a folder in /app called /md-icons. That is where I dropped them off at. If you follow this method, the rest will work. But if you find a different place, please change the paths respectively in your code.
And just a recap of what our project looks like now:
Adjusting your gulpfile.js
As we said earlier, the gulpfile is responsible for taking our code or scripts, minifying, and then placing them into the /www. From there, the browser is served the files in the /www folder that it needs to produce a view or function.
The issue with our gulpfile.js file, and the reason our files disappeared from the /www folder is that we never told the gulpfile that we wanted those files processed and placed into the /www folder. That is what we are now going to do.
Add this to your gulpfile.js:
This basically says, that all those Material-Icon files should be copied and placed in your /build directory.
We add the 'customFonts' argument into the 'watch' and 'build' phases of the gulptask.js
The home stretch
We are almost there!
Now, we just have to specify in our CSS what it means when we declare an element to have class="material-icons".
Open your /app/theme/app.core.scss and place the following toward the bottom:
Finally, you should be able to call the element and have it render:
starsflarestars