一、字體圖標概述
①字體圖標其實就是把矢量圖形打包到字體文件里,以后就可以像使用一般外置字體一樣的使用它,因此Winform、WPF中都是可以用的。
②可以在很多地方使用圖標字體,包括自定義控件、自定義樣式、模板等。
③字體圖標優點:
字體文件非常小,比使用png等圖片文件要小很多和普通字體一樣,是矢量的,可任意放大縮小,且不會失真網上開源字體圖標很多,很容易獲取,項目開發中需要的絕大部分圖標都可以找到,非常方便二、在WPF中使用字體圖標
①獲取字體圖標,推薦阿里巴巴開源字體,如何下載字體參考它網站的下載說明,解壓下載的字體會得到以下文件:
iconfont.tff是我們需要的字體圖標庫文件
demo_unicode.html是字體庫對應的字體的標識,如下圖:
以后通過使用上圖紅色方框中的標識,即可獲得對應的字體圖標
②將字體圖標添加到項目新建的Resources文件夾中,并設置其生成操作為"Resource",如下圖:
③定義樣式
使用TextBlock作為圖標顯示的容器,因此定義一個TextBlock的樣式即可,如下所示。其中"SK2015"為字體名稱,以前阿里巴巴開源字體庫下載的時候可以修改字體名稱,現在好像修改不了,默認字體名稱為"iconfont",有朋友發現如何修改字體名稱的話,請在下面給我留言,謝謝!
MyIconFontStyle.xaml代碼如下:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/PResentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:IconFontDemo"> <Style x:Key="iFont" TargetType="TextBlock"> <Setter Property="FontFamily" Value="/IconFontDemo;component/Resources/#SF2015"/> <Setter Property="TextAlignment" Value="Center"/> <Setter Property="HorizontalAlignment" Value="Center"/> <Setter Property="VerticalAlignment" Value="Center"/> <Setter Property="FontSize" Value="20"/> </Style></ResourceDictionary>注意:上面的FontFamily屬性也可以這樣設置<Setter Property="FontFamily" Value="/Resources/#SF2015"/>但是為了以后將字體樣式定義到另外一個程序集,還是推薦使用全的相對路徑,否則會出現找不到資源的問題。④在App.xaml中引用定義的樣式資源
<application x:Class="IconFontDemo.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:IconFontDemo" StartupUri="MainWindow.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="MyIconFontStyle.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources></Application>⑤在xaml中使用字體圖標,MainWindow.xaml代碼<Window x:Class="IconFontDemo.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:IconFontDemo" 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="ios" Style="{StaticResource iFont}" FontSize="80" Margin="3" Foreground="Chartreuse"></TextBlock> <TextBlock x:Name="android" Style="{StaticResource iFont}" FontSize="90" Margin="3" Foreground="#FEDB11"></TextBlock> </StackPanel></Window>很奇怪Text屬性在網頁上無法顯示,三個屬性分別為:Text="" Text="" Text=""
⑥在CS代碼中使用字體圖標,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 IconFontDemo{ /// <summary> /// MainWindow.xaml 的交互邏輯 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); ios.Text = "/xe602"; android.Text = "/xe60c"; } }}⑦最終效果演示
新聞熱點
疑難解答