有没有办法将字体作为资源加载并设置其基线?
以下作品:
<FontFamily x:Key="DefaultFontFamily">/Swift.UiResources;component/Resources/Fonts/#Meta Offc</FontFamily>
但是像这样添加基线:
<FontFamily x:Key="DefaultFontFamily" Baseline="0.9">/Swift.UiResources;component/Resources/Fonts/#Meta Offc</FontFamily>
导致以下错误:
TypeConverter syntax error encountered while processing intialization string '/Swift.UiResources;component/Resources/Fonts/#Meta Offc'. Element attributes are not allowed on objects created via TypeConverter.
我应该指出,我试图解决的问题是营销传下来的字体,当垂直居中时,渲染得比以前的字体更高。
最佳答案
您可以创建复合字体,并将所有 Unicode 范围映射到您的嵌入字体。您需要创建一个扩展名为 .CompositeFont
的新文件,并为其指定“资源”构建操作。其内容应该是:
<!-- File: "Resources/Fonts/Custom Font.CompositeFont" in Swift.UiResources -->
<FontFamily xmlns="http://schemas.microsoft.com/winfx/2006/xaml/composite-font"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
Baseline="0.9">
<FontFamily.FamilyNames>
<s:String x:Key="en-US">Custom Font</s:String>
</FontFamily.FamilyNames>
<FontFamily.FamilyMaps>
<FontFamilyMap Scale="1.0"
Target="/Swift.UiResources;component/Resources/Fonts/#Meta Offc"
Unicode="0000-007F" />
</FontFamily.FamilyMaps>
</FontFamily>
然后,在 Xaml 中引用您的字体时,请使用以下格式:
/Swift.UiResources;component/Resources/Fonts/#Custom Font
#
后面的字体名称应与您在上面的 FontFamily.FamilyNames
中设置的值匹配。
如果您想要映射超出标准 ASCII 范围的内容,可以向 Unicode
属性添加更多以逗号分隔的字符范围。
过去,我注意到,当您创建在一个或多个映射中使用资源 URI 的复合字体时,它无法在 Visual Studio 设计器中正确显示,尽管它在运行时正常工作。自从我报告后,他们可能已经解决了这个问题。
关于wpf - 加载字体资源并设置基线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26846144/