Add Custom Icon in Ionic 2

后端 未结 10 2021
既然无缘
既然无缘 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:03

    If you want to use custom fonts in ionic 2+ you can do it with following.

    Step 01:

    • Have/create custom font SVG files using tools such as XD.
    • Go to awesome online tool https://icomoon.io to generate font icons out of your SVG files. It's free tool and very good, I am using it for a while.
    • Download your custom font file.
    • Your file will look like something below.
    [class^="icon-"], [class*=" icon-"] {
      font-family: 'icomoon' !important;
      speak: none;
      font-style: normal;
      font-weight: normal;
      font-variant: normal;
      text-transform: none;
      line-height: 1;
    }
    

    replace above code with :

    [class^="icon-"],
    [class*=" icon-"],
    [class^="ion-ios-icon"],
    [class*="ion-ios-icon"],
    [class^="ion-md-icon"],
    [class*="ion-md-icon"]{
      @extend .ion;
      font-family: 'icomoon' !important;
      speak: none;
      font-style: normal;
      font-weight: normal;
      font-variant: normal;
      text-transform: none;
      line-height: 1;
    }
    

    Step 02:

    • We can use SASS @mixing for repetitive work
      @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  {
        content: $val;
        font-size: 26px;
      }
    }
    

    we can use our sass mixing in our sass file like :

    @include makeIcon(icon-new-home, '\e911')
    

    Step 03

    Use it like

    
        
        
     
    

    and its even support android ripple effect, which kinda looks cool

    [Updated] 30 Nov 2017

    @ionic/app-scripts : 3.1.2
    Ionic Framework    : ionic-angular 3.9.2
    

    For Ionic 3.1.2

    You need to add @import "ionicons"; inside your /src/theme/variables.scss file which will fix below error

    "[class^="icon-"]" failed to @extend ".ion". The selector ".ion" was not found. Use "@extend .ion !optional" 
            if the extend should be able to fail. 
    

提交回复
热议问题