How to show emoji in angular/ionic app

ぐ巨炮叔叔 提交于 2020-05-13 19:19:07

问题


In my mobile app using angular5/ionic3 app I want to show emoji icon.

So I used something like

<span>&#x1F601;</span>

But in rendering it will show it as empty square.

I already have <meta charset="UTF-8"> in my index.html and html file is also saved as utf-8.

I thought it could be related to sanitizing, so I created below pipe

@Pipe({
  name: 'safeHtml',
})
export class SafeHtmlPipe implements PipeTransform {
    constructor(private domSanitizer: DomSanitizer) { }
    transform(html) {
    return this.domSanitizer.bypassSecurityTrustHtml(html);
    }
}

and then I used it as

 <span [innerHTML]="'&#x1F601;' | safeHtml"></span>   

but still result is the same

What did I miss?

Plunker


回答1:


This is usually happening when you don't have a font to display the character. On my computer Chrome uses the Symbola font to render this character. You can download it from here. Or you can find another font that supports emojis. Than you add the font in CSS using @font-face

@font-face {
  font-family: "Symbola";
  src: url("/fonts/Symbola-Emoji.woff") format("woff"),
    url("/fonts/Symbola-Emoji.ttf") format("ttf");
}

Then you can use this font on a CSS class.

.emoji {
    font-family: "Symbola"
}

Then you add that class to your span:

<span class="emoji">&#x1F601;</span>

UPDATE:

If you assign &#x1F601; to a variable and then read it inside the template it works fine:

<span [innerHTML]="someVariable | safeHtml"></span><!--this will work-->

But your code breaks: the U+1F601 character (a.k.a. 😁) becomes U+F601 and since you don't have a font to display it you get an empty square. It looks like a bug in Angular's template rendering engine because even if I do <span [innerHTML]="(someVariable + '&#x1F601;') | safeHtml"></span> it still breaks. I get the 😁 and the U+F601 character. So this is not the sanitizer problem.



来源:https://stackoverflow.com/questions/48052383/how-to-show-emoji-in-angular-ionic-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!