如何在不更改文本字体的情况下使用FontAwesome图标

问题描述

我正在按钮和标签中放置图标,但这是结果:

enter image description here

显示图标,但会更改文本。如何在不更改文本的情况下显示图标?

 @Configuration
 class WebConfig implements WebMvcConfigurer {    

   HttpMessageConverter messagePackMessageConverter() {
         MediaType msgPackMediaType = new MediaType("application","x-msgpack");
         return new AbstractJackson2HttpMessageConverter( new ObjectMapper( new 
            MessagePackFactory()),msgPackMediaType) {
         };
     }

     @Override
     public void extendMessageConverters(List<HttpMessageConverter<?>> converters) 
     {
          converters.add(messagePackMessageConverter());
     }
 } 

解决方法

Button可以有Text和一个ImageSourceFontImageSource

ContentLayout设置图像相对于文本的位置和间距,其默认值显示在XAML下方。

可能的位置值为LeftRightTopBottom

位置值为LeftRight的文本垂直居中。

位置值为TopBottom时,文本水平居中。

  <Button Text="fa-list" ContentLayout="Left,10">
    <Button.ImageSource>
      <FontImageSource Glyph="&#xf022;" FontFamily="{StaticResource FaRegular4}"/>
    </Button.ImageSource>
  </Button>

Label可以有一个或多个Span

    <Label>
      <Label.FormattedText>
        <FormattedString>
          <Span Text="f0A4 " />
          <Span Text="&#xf0A4;" FontFamily="{StaticResource FaRegular4}" />
          <Span Text=" f0A5 " />
          <Span Text="&#xf0A5;" FontFamily="{StaticResource FaRegular4}" />
        </FormattedString>
      </Label.FormattedText>
    </Label>
,

我认为最简单的方法是改用按钮Image。 下载图标并将其添加到iOS中的“资源”文件夹和Android中的Drawable中。 Download image from Fontawesome's website

然后按如下所示在您的按钮中使用它:

<Button Text="Edit" ImageSource="edit_icon" />

顺便说一句,您需要将按钮的字体更改为其他字体或保留默认字体。

,

我可以想到的一种解决方法是将图标和文本分开:

<StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand" BackgroundColor="Purple">

    <Button Text="&#xf0e2;" FontFamily="FontAwesome" TextColor="White" WidthRequest="25" HorizontalOptions="Center"
           VerticalOptions="Center" />

    <Label Text="Load Items" TextColor="White" WidthRequest="90"
           HorizontalOptions="Center"
           VerticalOptions="Center" />

</StackLayout>