Font Awesome and Material Icons used in ngFor (Angular 2) - css

I have a dynamic list of Icons that are displayed in a right side bar. The Icons are passed into the sidebar depending on what the user does. This dynamic array of icons is displayed using ngFor.
Unfortunately, some of the icons are from Font Awesome and some are from Google Material.
<!--Font Awesome-->
<i class="{{myIcon}}"></i>
<!--Material-->
<md-icon>{{myIcon}}></md-icon>
<!--Or-->
<i class="material-icon">{{myIcon}}</i>
Seeing that they are not an exact match, how do you get them to display both kinds using an ngFor with nothing but the string of the icon's name.

I have a solution for you. I just implemented exactly this.
Now I know you asked for just what goes between the *ngFor to dynamically select and render the appropriate icon based on the icon string, but I am going to document the complete solution including getting font awesome registered, for others.
Let us start at the top by making sure you have font awesome css included in your header. You can have fontawesome email you a link to their CDN version here http://fontawesome.io/get-started/ and include this in your header.
<script src="https://use.fontawesome.com/c26638a4cc.js"></script>
Next create and register an alias for fontawesome in your app.module. You can do this by first importing these:
import { MdIconModule, MdIconRegistry } from '#angular/material';
Don't forget to include MdIconRegistry in your providers
providers: [
...
MdIconRegistry,
...
],
Then let us register fontawesome with MdIconRegistry in our AppModule like so..
constructor(...,
public mdIconRegistry: MdIconRegistry) {
mdIconRegistry.registerFontClassAlias('fontawesome', 'fa');
...
}
What we have done so far allows us to use font awesome icons in our application as follows:
<md-icon class="some-class" fontSet="fa" fontIcon="fa-trophy"></md-icon>
Now let us answer the original question i.e. how to dynamically display either a material design icon or a font-awesome icon based on just the icon string.
To do this, I am going to rely on font-awesome icons starting with a 'fa-' prefix. If we are good with this, then I can check for 'fa-' and use that to set fontSet and fontIcon appropriately.
<md-list-item *ngFor="let menuitem of menuList">
<button md-button ...>
<md-icon [fontSet]="menuitem.icon.substr(0,3) === 'fa-' ? 'fa' : ''"
[fontIcon]="menuitem.icon.substr(0,3) === 'fa-' ? menuitem.icon : ''"
[ngClass]="{'your-fa-custom-class': true }">
<span>{{ menuitem.name }} </span>
</button>
ngClass includes .your-fa-custom-class where you can change the font-size for your font awesome icons specifically.
You are all set!

Related

How to provide custom colors to "mat-fab, mat-fab-mini, mat-button" | Angular material

I`m facing difficulties in using my custom color for the mat-fab buttons of Angular Material.
The official doc that I am refering is this
In the official docs, we have only 3 color options:
Primary
Accent
Warn
I figured out just by leaving color property empty we could make the mat-button/fab color as
"white".
Code - <button mat-mini-fab color="" aria-label="Icon Button background"> </button>
Its looks like :
While coding as well, I could see only above 3 mentioned colors :
I tired to change the css that mat-button / fab are using but nothing is working out for me.
It would be helpful if someone from the community could support me on this.
For adding white color you could leave the 'color' property as empty.
As I shared the code above <button mat-mini-fab color="" aria-label="Icon Button background"> </button>
For the custom color:
I`ve my CSS file as follows:
HTML file :
Using Attribute directive for showing different colors.
Output:
In the nutshell, rather then providing color property in the CSS file you have to provide background color property.
Hope, this will be helpful for you.
Thank you.

Font awesome 5 listen for unknown icons

I'm using font awesome 5 on my websites. Let's say I miss-spell a icon like this:
<i class="fa fa-map-markerzzzz"></i>
Font Awesome code must detect that it's an unknown icon as it displays ? and ! symbols.
Any idea how I can listen in for this? I'd like to add error reporting. We've an error logging system, and I'd like to send an error to that for every broken icon.
Any ideas how to do that?
It's not so much that FontAwesome detects this, since it's a collection of CSS classes backed by a font file and not a piece of code. Therefore, unfortunately there is no error event that you can listen for.
A similar question has been asked before, though not for FontAwesome. It appears that your best bet would be to retrieve a list of existing FontAwesome classes, then compare those to the classes used in your code.
If you use Angular or React, you may be able to wrap the FontAwesome icons in a component and do the error checking there.
You can do something like this:
<style>
.fa:before {
content:"?"; /*if the content exist it will be replaced later, if not you will see this*/
}
</style>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" >
<i class="fa fa-map-markerzzzz"></i>
<i class="fa fa-user"></i>

Material Icons vs Material Design Icons

There are two ways to use Materilize Icon:
<i class="mdi-ICON-NAME"></i>
<i class="material-icons">ICON_NAME</i>
In the official site I did not find guide or icon chart for using the font in this way <i class="mdi-ICON-NAME"></i> but there is guide on how to use the icon in this way <i class="material-icons">ICON_NAME</i> here.
So, when official site doesn't have guide on how to use the icon in this way <i class="mdi-ICON-NAME"></i>. Then why is it defined all over the stylesheet (materialize.css).
Also, if I need to call the icon this way <i class="material-icons">ICON_NAME</i> then I need to link one for font file. Eg.<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">. Hence, I feel that 2 fonts will be loaded into the DOM which is not needed, right?
My quesiton is: Where can I find Icon chart if I need to use icon in this way <i class="mdi-ICON-NAME"></i>?
I got it answered by Materialize team:
https://github.com/Dogfalo/materialize/issues/1824#issuecomment-125949178
For question: Where can I find Icon chart if I need to use icon in this way <i class="mdi-ICON-NAME"></i>
I got answer: The mdi type icons are being phased out for the official google sources, that's why you won't find the old chart in the documentation. A complete list of the new icons can be found in https://www.google.com/design/icons/
For question: Why not just delete all the mdi icons and css defined as new icons are already offered by google, isn't it?
I got answer: Because that would suddenly break all the user implementations, so this way it can be phased out gradually, giving people time to update their icons.
If you really need to check the old implementation, you can go through the commit history from https://github.com/Dogfalo/materialize/blob/master/icons.html and look at a version before the google icons were added.
You can find the full list of icons here https://www.google.com/design/icons/
The naming is mdi-category-iconname.
An example:
<i class="mdi-action-search"></i>

Glyph shows over pull down menu

I am updating an Angular app to use Bootstrap 3 glyphicons instead of some images, and have run into the following difficulty:
I have this glyph in a view that's in my content section:
<i class="glyphicon glyphicon-play"></i>
and this one in a pull-down menu:
<i class="glyphicon glyphicon-log-out"></i>
It seems that when I pull down the menu, the "-play" glyph is not hidden and still shows through it.
I'm not 100% sure what the issue is without seeing more of your code, but you could add some quick jQuery to show/hide the appropriate icon on click.
$('.the-dropdown').on('click', function() {
$('i.glyphicon-play').hide();
});

HTML/CSS: Font-Awesome Icons Not Appearing

For some reason the font-awesome social media icons refuse to show up on any browser. All other icons seem to work perfectly fine though... I've included a photo of the page opened in Mozilla with inspect element(linked due to no reputation). The only loaded css files are bootstrap and awesome-font.
It looks like you thought the text description of stacks was the class names. Icon stacks should look like this, there is no on class:
<span class="icon-stack">
<i class="icon-minus icon-stack-base"></i>
<i class="icon-camera icon-light"></i>
</span>
In your example, the second class overwrites the first class. This is because your using both classes on the same element, instead of stacking them.

Resources