Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Custom Icon in Ionic 2

I am using Ionic 2 for developing my App. I want to use my custom icons in my app like we use ionic 2 icons using tag. Eg:

<ion-icon name="my-custom-icon"></ion-icon> 

How can i achieve this? Is there any recommended way to doing this?

like image 732
Prerak Tiwari Avatar asked Jul 19 '16 15:07

Prerak Tiwari


People also ask

How do I use SVG in ion icon?

Custom icons To use a custom SVG, provide its url in the src attribute to request the external SVG file. The src attribute works the same as <img src="..."> in that the url must be accessible from the webpage that's making a request for the image.

How do I add custom icons to ionic 4?

Grab your icon, and put it in your custom-ion-icons folder. Next, you'll have to name them appropriately. Each version of the icon must be named with their platform name preceding the icon name.


1 Answers

This is the easiest way I've found, from the forums at https://forum.ionicframework.com/t/anyway-to-custom-the-tabbars-icon-with-my-own-svg-file/46131/36.

In your app.scss file, add the following code:

ion-icon {     &[class*="appname-"] {         // Instead of using the font-based icons         // We're applying SVG masks         mask-size: contain;         mask-position: 50% 50%;         mask-repeat: no-repeat;         background: currentColor;         width: 1em;         height: 1em;     }     // custom icons     &[class*="appname-customicon1"] {         mask-image: url(../assets/img/customicon1.svg);     }     &[class*="appname-customicon2"] {         mask-image: url(../assets/img/customicon2.svg);     }     &[class*="appname-customicon3"] {         mask-image: url(../assets/img/customicon3.svg);     } } 

Then in your templates, you can use the following HTML to create the icon:

<ion-icon name="appname-customicon"></ion-icon> 

This is far less complicated than using the font-based methods. As long as you're not adding hundreds of icons you should be okay with this method. However each image is sent as a separate request, where as with the font methods all the images are contained in one file, so it would be a little more efficient.

like image 80
adjwilli Avatar answered Sep 25 '22 22:09

adjwilli