如何在sap.ui.core.Icon中显示glyphicon/font-awesome?

How to display glyphicon/font-awesome in sap.ui.core.Icon?

由于 SAPUI5/OpenUI5 中的图标集合非常有限,我想在 sap.ui.core.Icon 中显示字形图标和/或超棒的字体图标。

如何实现?

为了在现有控件中使用外部图标,您可以使用 sap.ui.core.IconPool 控件。控件提供了addIcon方法来添加一个Icon。

  1. 列表项

    在您的 CSS

    中声明 font-face 标签
    font-face {
      font-family: 'My-Icons'; 
      src: url('_PATH_TO_EOT_FILE_');
      src: url('_PATH_TO_EOT_FILE_?#iefix') format('embedded-opentype'), /*?#iefix is required to be added to the end of the path of eot file here*/
         url('_PATH_TO_TTF_FILE_') format('truetype');
      font-weight: normal;
      font-style: normal;
    };
    

    如果您使用的是超赞字体,则可以在您的清单中包含超赞字体样式表。样式表将包含在字体声明中,有点像这样:

    @font-face {
     font-family: 'FontAwesome';
     src: url('../fonts/fontawesome-webfont.eot?v=4.7.0');
     src: url('../fonts/fontawesome-webfont.eot?#iefix&v=4.7.0') format('embedded-opentype'), url('../fonts/fontawesome-webfont.woff2?v=4.7.0') format('woff2'), url('../fonts/fontawesome-webfont.woff?v=4.7.0') format('woff'), url('../fonts/fontawesome-webfont.ttf?v=4.7.0') format('truetype'), url('../fonts/fontawesome-webfont.svg?v=4.7.0#fontawesomeregular') format('svg');
     font-weight: normal;
     font-style: normal;
    }
    
  2. 致电sap.ui.core.IconPool.addIcon添加您的图标。你可以声明 这在你身上 Component.js

    sap.ui.define([
      "sap/ui/core/UIComponent",
      "sap/ui/core/IconPool"],
     function(UIComponent, IconPool){
      "use strict";
     return UIComponent.extend("com.sap.app.Component",{
     metadata : {
        manifest: "json"
     },
     init : function(){
        //call the init function of the parent
        UIComponent.prototype.init.apply(this, arguments);
        //Init Router
        this.getRouter().initialize();
    
        IconPool.addIcon("battery", "fa", {
            fontFamily : "FontAwesome",
            content : "f241" 
        });
      }
     }); 
    });
    
  3. 您现在可以在您的控件中使用此图标

    <mvc:View controllerName="com.sap.app.controller.App" 
      xmlns:mvc="sap.ui.core.mvc" 
      xmlns:core="sap.ui.core" 
      xmlns="sap.m">
      <core:Icon src="sap-icon://fa/battery" color="#031E48" ></core:Icon>
      <Button icon="sap-icon://fa/battery" press="onPress"></Button>
    </mvc:View>
    

您也可以参考这里的文档:https://help.sap.com/saphelp_uiaddon10/helpdata/en/21/ea0ea94614480d9a910b2e93431291/content.htm

免责声明:无耻的自我推销。

对于 Font Awesome(仅限免费版本),我专门为此制作了一个插件:ui5-fontawesome

如果在 IconPool.registerFont() 不可用的旧 UI5 版本上,插件会在内部调用 IconPool.registerFont() for you, or IconPool.addIcon()

现在您所要做的就是将插件包含在您的 html 中,您可以通过直接调用图标名称来开始使用图标(例如 <core:Icon src="sap-icon://fa-brands/font-awesome"/>

sap.ui.getCore().attachInit(function() {
  sap.ui.define([
    'sap/ui/core/mvc/Controller', 'sap/ui/core/mvc/XMLView'
  ], function(Controller, XMLView) {
    XMLView.create({
      controller: new(Controller.extend('ui5.demo.controller', {}))('ui5.demo.controller'),
      definition: jQuery('#view').html(),
    }).then(function(oView) {
      oView.placeAt('content');
    });
  });
});
<body class="sapUiBody" id="content">
  <script
    id="sap-ui-bootstrap"
    src="https://openui5.hana.ondemand.com/resources/sap-ui-core.js"
    data-sap-ui-theme="sap_belize"
    data-sap-ui-libs="sap.m"
    data-sap-ui-compatversion="edge"
    data-sap-ui-preload="async"
    data-sap-ui-resourceroots='{
      "ui5.demo": "./"
    }'>
  </script>
  <script id="view" type="text/xml">
    <mvc:View
      controllerName="ui5.demo.controller"
      xmlns="sap.m"
      xmlns:core="sap.ui.core"
      xmlns:mvc="sap.ui.core.mvc"
      displayBlock="true">
      <Shell>
        <App>
          <Page title="UI5-FontAwesome test">
            <core:Icon src="sap-icon://fa-brands/font-awesome" size="2.5rem" color="#339AF0" class="sapUiTinyMargin"/>
          </Page>
        </App>
      </Shell>
    </mvc:View>
  </script>
  <script
    src="https://unpkg.com/ui5-fontawesome@^1/js/ui5-fontawesome.min.js"
    id="ui5-fontawesome"
    data-resourceroot="https://unpkg.com/ui5-fontawesome@^1/">
  </script>
</body>