Add Custom Icon in Ionic 2

后端 未结 10 2020
既然无缘
既然无缘 2020-12-02 06:35

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:



        
10条回答
  •  独厮守ぢ
    2020-12-02 07:06

    Been trying to implement Anjum Nawab shaikh answer on top with the icons sass sheet from icomoon.

    I have been able to get it working with version 2.2.2.

    In the www/fonts of the project I had added the icomoon files:

    icomoon.svg
    icomoon.ttf
    icomoon.woff
    icomoon.eot
    icomoon.scss
    

    In icomoon.scss I added the following scss:

    @font-face {
      font-family: 'icomoon';
      src:  url('../fonts/icomoon.eot?tclihz');
      src:  url('../fonts/icomoon.eot?tclihz#iefix') format('embedded-opentype'),
        url('../fonts/icomoon.ttf?tclihz') format('truetype'),
        url('../fonts/icomoon.woff?tclihz') format('woff'),
        url('../fonts/icomoon.svg?tclihz#icomoon') format('svg');
      font-weight: normal;
      font-style: normal;
    }
    
    [class^="icon-"],
    [class*=" icon-"],
    [class^="ion-ios-icon"],
    [class*="ion-ios-icon"],
    [class^="ion-md-icon"],
    [class*="ion-md-icon"]{
    
    /* Didn't feel the need to @extend since this just adds to already existing .ion
    code which I believe is replaced to just ion-icon tag selector in 
    www/assets/fonts/ionicons.scss */
    
      font-family: "Ionicons", "icomoon" !important; //So just add this
    }
    
    //Mixin
    @mixin makeIcon($arg, $val) {
      .ion-ios-#{$arg}:before ,
      .ion-ios-#{$arg}-circle:before ,
      .ion-ios-#{$arg}-circle-outline:before ,
      .ion-ios-#{$arg}-outline:before ,
      .ion-md-#{$arg}:before ,
      .ion-md-#{$arg}-circle:before ,
      .ion-md-#{$arg}-circle-outline:before ,
      .ion-md-#{$arg}-outline:before  {
        //important to overwrite ionic icons with your own icons
        content: $val !important; 
        font-size: 26px;
      }
    }
    
    
    //Adding Icons
    @include makeIcon(email, '\e900');
    ...
    

    Then I did an import to the variables.scss

    @import "../www/fonts/icomoon";
    

    Now we can add this to the html template:

    
    

提交回复
热议问题