angulartypescriptangular6google-material-iconsligature

angular 6 custom material icons with ligature support


Is it possible to create custom material icons with ligature support. I am currently using svgIcon for getting Custom Icons, Is there any way to achieve ligature supported custom icons?

My current code snippet is given as below

app.component.ts

import { Component } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { MatIconRegistry } from '@angular/material';

  @Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss']
  })
  export class AppComponent {
    title = 'app';

    constructor(
      iconRegistry: MatIconRegistry,
      sanitizer: DomSanitizer
    ) {
      iconRegistry.addSvgIcon('key',
        sanitizer.bypassSecurityTrustResourceUrl('assets/icons/key.svg')
      );
      iconRegistry.addSvgIcon('user',
        sanitizer.bypassSecurityTrustResourceUrl('assets/icons/user.svg')
      );
    }

  }

login.component.html

<mat-form-field>
  <input matInput placeholder="Username" formControlName="username">
  <mat-icon matPrefix svgIcon="user"></mat-icon>
  <mat-error *ngIf="username.invalid">Username is required</mat-error>
</mat-form-field>

<mat-form-field>
  <input matInput type="password" placeholder="Password" formControlName="password">
  <mat-icon matPrefix svgIcon="key"></mat-icon>
  <mat-error *ngIf="password.invalid">Password is required</mat-error>
</mat-form-field>

Solution

  • Sure its possible:

    Step 1) Get all the SVG icons.

    Step 2) Create your font with a custom ligature.

    This is a tool, to create your own font library with ligature support. https://icomoon.io

    Step 3:

    Declare your own font.

    @font-face {
      font-family: 'icomoon';
      src: url('path.ttf') format('truetype');
      font-weight: normal;
      font-style: normal;
    }
    

    Step 4: Override the .material-icons with you own font

    .material-icons {
      font-family: 'MyFontFamily';
       ...
    }
    

    or declare your own fontSet so you can work with both.

    // Register to AppComponent
    
     constructor(iconRegistry: MatIconRegistry) {
        iconRegistry.registerFontClassAlias('MyFontFamily', 'MyIconClass');
     }
    

    Last step.

    <mat-icon>LigatureToDisplay</mat-icon>
    

    OR

    <mat-icon fontSet="MyIconClass">LigatureOrUnicodeToDisplay</mat-icon>
    

    Here is a small tutorial.

    https://medium.com/@buddhiv/add-a-custom-icon-font-in-your-application-b1e07a687953