4.4-字体图像集成Font&Image
一、字体集成:MAUI默认使用OpenSans字体,通过引入新的字体库和图标字体库为案例说明
1、案例的字体库,均在www.iconfont.cn下载
1)字体库为阿里巴巴普惠体的Thin35,Alibaba_PuHuiTi_2.0_35_Thin_35_Thin.ttf
(2)字体图标库的下载方式详见【三、字体图标下载方式】,将下载的字体图标库重命名为MyIconFont.ttf
2、将字体库ttf文件,复制到Resources\Fonts文件夹下
(1)复制到Resources\Fonts文件夹的ttf字体库,文件属性的生成操作将自动设置为 MauiFont
(2)如果要复制到其它文件夹,或Fonts文件夹下的子文件夹,需要修改两个地方:一是将文件属性的生成操作设置为 MauiFont,二是添加项目属性的,如下:
<!--推荐:模板默认设置,载入Resources\Fonts文件夹下的所有ttf字体库-->
<ItemGroup>
<MauiFont Include="Resources\Fonts\*" />
</ItemGroup>
<!--载入Resources\Fonts文件夹及其子文件夹下的所有ttf字体库-->
<ItemGroup>
<MauiFont Include="Resources\Fonts\**\*" />
</ItemGroup>
<!--载入Resources\MyFonts文件夹下的所有ttf字体库-->
<ItemGroup>
<MauiFont Include="Resources\MyFonts\*" />
</ItemGroup>
3、在MauiProgram.cs入口文件中,注册字体库
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); //模板默认注册的字体库
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold"); //模板默认注册的字体库
fonts.AddFont("MyIconFont.ttf", "MyIconFont"); //注册MyIconFont.ttf字体库,别名为MyIconFont
fonts.AddFont("Alibaba_PuHuiTi_2.0_35_Thin_35_Thin.ttf", "AliThin35");//注册Alibaba_PuHuiTi_2.0_35_Thin_35_Thin.ttf字体库,别名为AliThin35
});
#if DEBUG
builder.Logging.AddDebug();
#endif
return builder.Build();
}
}
4、使用字体
<ContentPage
......">
<StackLayout>
<!--
①FontFamily设置字体,使用别名AliThin35;
②FontAttributes设置斜体、加粗等属性
③FontSize设置字体大小
④FontAutoScalingEnabled设置文本缩放首选项
-->
<Label
FontAttributes="Bold,Italic"
FontAutoScalingEnabled="True"
FontFamily="AliThin35"
FontSize="30"
Text="字体基本使用" />
<!--
①通过元素属性设置FontFamily属性
②OnPlatform为泛型类,实例化时,通过x:TypeArguments设置泛型参数
③OnPlatform的IList<On>属性,设置不同平台的字体
-->
<Label FontSize="30" Text="每个平台使用不一样的字体">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<On Platform="iOS" Value="OpenSansRegular" />
<On Platform="Android" Value="AliThin35" />
<On Platform="WinUI" Value="OpenSansSemibold" />
</OnPlatform>
</Label.FontFamily>
</Label>
<!--
①ImageButton的Source属性为ImageSource类型,可以设置字体图标,有ImageSource类型属性的控件,还包括Image等
②FontFamily设置字体,使用别名MyIconFont
③Glyph设置图标的unicode字符值
④Size和Color,设置大小和颜色
-->
<ImageButton>
<ImageButton.Source>
<FontImageSource
FontFamily="MyIconFont"
Glyph=""
Size="20"
Color="DarkBlue" />
</ImageButton.Source>
</ImageButton>
</StackLayout>
</ContentPage>
二、图像集成
1、基本介绍
- 图像集成的主要功能是,在项目的单个位置指定的图像,可以根据不同平台要求,自动生成不同分辨率的图像,在应用图标、初始画面、导航图标等功能上会使用到。
- 建议使用SVG矢量格式图像,如使用SVG图像,在生成不同平台要求的图像是,会自动转为png图片格式。如在应用中引用图片资源,应将SVG图像的后缀修改为png,如dotnet_bot.svg矢量图,使用时用dotnet_bot.png
- 建议将SVG图像复制到默认的文件夹,如应用图标放到Resources\AppIcon文件夹下,初始画面放到Resources\Splash,其它图像放到Resources\Images,这样系统会将图像属性的生成操作自动设置为相应格式,否则就要手动设置,其中应用图标设置为MauiIcon,初始画面设置为MauiSplashScreen,集成图像设置为MauiImage。
- 为兼容Android,图像名称应使用小写字母,如dotnet_bot.svg
2、基本使用:双击项目,设置项目属性,在ItemGroup元素中,进行图像集成的设置
<ItemGroup>
<!--
①Include属性设置应用图标的背景图
②ForegroundFile属性设置应用图标的前景图
③Color重新着色背景
④TintColor重新着色前景
-->
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" TintColor="#66B3FF"/>
<!--
①Include设置初始画面,BaseSize设置图像基准大小,Color设置背景色,TintColor添加新的色调
②通过设置Resize="false",禁止调整矢量图的大小
-->
<MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" TintColor="#66B3FF" BaseSize="128,128" />
<!--
①【Include="Resources\Images\*"】,加载Images文件夹下的所有图像
②【Update="Resources\Images\dotnet_bot.svg"】,加载指定图像,BaseSize设置图像基准大小
③使用dotnet_bot.svg,【<Image Aspect="Center" Source="dotnet_bot.png" />】
-->
<MauiImage Include="Resources\Images\*" />
<MauiImage Update="Resources\Images\dotnet_bot.svg" BaseSize="168,208" />
......
</ItemGroup>