原文:
WPF字体图标——FontAwesom
版权声明:本文为【CSDN博主:松一160】原创文章,未经允许不得转载。 https://blog.csdn.net/songyi160/article/details/54907492
一、字体图标概述
FontAwesome是迄今为止最出色的图标字体(没有之一),优点是图标多、图标美观、兼容各种应用场景等。
二、获取FontAwesome字体图标库
三、加压下载到的字体库压缩包,得到字体图标库文件
四、像WPF字体图标——IconFont提供的步骤调用即可,只需要将样式引用的字体名称改为:#FontAwesome,注意名称大小写,否则找不到对应的图标
①项目目录结构
②MyFontAwesome.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:FontAwesomeDemo">
<Style x:Key="iFont" TargetType="TextBlock">
<Setter Property="FontFamily" Value="/FontAwesomeDemo;component/Resources/#FontAwesome"/>
<Setter Property="TextAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="FontSize" Value="20"/>
</Style>
</ResourceDictionary>
③App.xaml
<Application x:Class="FontAwesomeDemo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:FontAwesomeDemo"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyFontAwesome.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
④MainWindow.xaml
<Window x:Class="FontAwesomeDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:FontAwesomeDemo"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Background="Blue">
<StackPanel Orientation="Horizontal">
<TextBlock Text="" Style="{StaticResource iFont}" FontSize="50" Margin="3" Foreground="White"></TextBlock>
<TextBlock Text="" Style="{StaticResource iFont}" FontSize="60" Margin="3" Foreground="SandyBrown"></TextBlock>
<TextBlock Text="" Style="{StaticResource iFont}" FontSize="70" Margin="3" Foreground="#FB0AE8"></TextBlock>
<TextBlock x:Name="qq" Style="{StaticResource iFont}" FontSize="80" Margin="3" Foreground="Chartreuse"></TextBlock>
<TextBlock x:Name="refresh" Style="{StaticResource iFont}" FontSize="90" Margin="3" Foreground="#FEDB11"></TextBlock>
</StackPanel>
</Window>
很奇怪Text属性在网页上无法显示,三个属性分别为:Text="" Text="" Text=""
⑤MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace FontAwesomeDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
qq.Text = "\xf1d6";
refresh.Text = "\xf021";
}
}
}
⑥效果演示