简体   繁体   English

在Adobe Flex 3中使用Font Awesome

[英]Using Font Awesome in Adobe Flex 3

Is it possible to use Font Awesome icons in Adobe Flex 3 since it doesn't allow inline CSS style? 是否可以在Adobe Flex 3中使用Font Awesome图标,因为它不允许内联CSS样式? If so, how to set the text of Button or Label to properly be shown as icon in Flex? 如果是这样,如何设置Button或Label的文本在Flex中正确显示为图标?

Currently, I have the following setup for Flex 3.6A and the button isn't showing the icon properly: 目前,我对Flex 3.6A进行了以下设置,并且该按钮未正确显示图标:

文件结构

Here is the simplify version of ...View.mxml placed in components package: 这是放置在组件包中的... View.mxml的简化版本:

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
       currentState="default">
<mx:Style source="../assets/styles.css"/>
<mx:states>
    <mx:State name="Hide"/>
    <mx:State name="default">
        <mx:AddChild>
            <mx:HBox>
                <mx:Button label="&#xf011;" styleName="complete" id="completeButton" fontFamily="FontAwesome"/>
                <mx:Button id="skipButton" />
                <mx:Button id="shareButton" />
                <mx:Button id="deleteButton" />
                <mx:Button id="progressButton" />
            </mx:HBox>
        </mx:AddChild>
    </mx:State>
    <mx:State name="Expand"/>
</mx:states>    
</mx:Canvas>

Here is the content of styles.css: 这是styles.css的内容:

@font-face {
    fontFamily:             FontAwesome;
    embed-as-cff:           true;
    src:                    url("font-awesome-4.7.0/fonts/fontawesome-webfont.ttf");
    fontStyle:              normal;
}
@font-face {
    fontFamily:             FontAwesomeNonCff;
    embed-as-cff:           false;
    src:                    url("font-awesome-4.7.0/fonts/FontAwesome.otf");
    fontStyle:              normal;
}

.complete {
    font-family: FontAwesome;
    color: red;
}

Here is what the button looks like: 这是按钮的样子: 完整按钮

So I just gave this a try and it seems to work. 所以我只是尝试了一下它似乎工作。 Please follow these steps to use font-awesome font in Flex. 请按照以下步骤在Flex中使用font-awesome字体。

First of all download the .ttf font from the font-awesome website. 首先从font-awesome网站下载.ttf字体。 Next, import the .ttf file into your Flex application preferably inside a package. 接下来,将.ttf文件导入Flex应用程序,最好是在包中。 I created a package called as assets/fonts , but it's totally upto you where you keep it in your project. 我创建了一个名为assets/fonts的软件包,但是完全取决于你将它保存在项目中的位置。

Once done, create a style sheet, I called it styles.css in any package of your choice. 完成后,创建一个样式表,我将其称为styles.css在您选择的任何包中。 I created it in a package named assets/css . 我在一个名为assets/css的包中创建了它。 Create your font-face style in the style sheet: 在样式表中创建字体样式:

@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";

@font-face 
{
    src:url("assets/fonts/fontawesome-webfont.ttf");
    fontFamily: fontawesome;
}

.buttons
{
    fontFamily:fontawesome;
    fontSize:20;
    color:#000000;
}

I also created a selector class named as buttons to be used on your buttons. 我还创建了一个名为buttons的选择器类,用于按钮。 Notice the path of the font file and make sure it matches your folder structure where it's kept. 注意字体文件的路径,并确保它与您保留的文件夹结构相匹配。 Also, notice that I am using the fontFamily as fontawesome in the buttons CSS selector. 另外,请注意我在buttons CSS选择器中使用fontFamily作为fontawesome

Next, import your css into the flex project and use the style name in your buttons: 接下来,将您的CSS导入flex项目并使用按钮中的样式名称:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx">
    <fx:Style source="assets/css/styles.css"/>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <s:Button width="100" height="40" top="50" left="50" label="&#xf2bb;" styleName="buttons"/>
</s:WindowedApplication> 

Important thing to note here is the label property of the button. 这里要注意的重点是按钮的label属性。 You have to use the Unicode value for the icon found next to the icon in the font-awesome cheatsheet . 您必须将Unicode值用于font-awesome cheatsheet中图标旁边的图标。

And once you run your code you should be able to see the font appearing in the button. 一旦运行代码,您应该能够看到按钮中出现的字体。 Attached screenshot below for your reference. 下面附上截图供您参考。

在此输入图像描述

PS: I am using Flex 4.x, so you may have to make some code adjustments, but the basic concept should remain the same. PS:我使用的是Flex 4.x,因此您可能需要进行一些代码调整,但基本概念应该保持不变。

UPDATE : For Flex 3.6A, make sure that you are adding the attribute of fontWeight:normal to your CSS selector to make it work. 更新 :对于Flex 3.6A,请确保将fontWeight:normal的属性添加到CSS选择器以使其工作。 It's strange, but without it, it does not seem to work. 这很奇怪,但没有它,它似乎不起作用。 So CSS would be: 所以CSS将是:

.buttons
{
    fontFamily:fontawesome;
    fontSize:20;
    fontWeight:normal;
    color:#000000;
}

Hope this helps. 希望这可以帮助。 Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM