未使用 WPF 应用程序自定义 ttf 字体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12423281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF app custom ttf font not being used?
提问by Gaz83
I downloaded a custom font which displays EAN 128 barcode. I also downloaded some source code and sample program which converts a string to an encoded string to use with the font (includes check sum etc).
我下载了一种显示 EAN 128 条形码的自定义字体。我还下载了一些源代码和示例程序,它们将字符串转换为编码字符串以与字体一起使用(包括校验和等)。
I installed the font, opened my WPF app and set a textblock font to this new font and added some text. The text comes out as if it was Arial or something.
我安装了字体,打开了我的 WPF 应用程序并将文本块字体设置为这种新字体并添加了一些文本。文本出来时好像是 Arial 之类的。
I tried adding the font as a resource and referencing it that way but still not joy.
我尝试将字体添加为资源并以这种方式引用它,但仍然不高兴。
So I then ran the program that came with the font. The interesting part I found was that when you entered text, the program encoded it and set a label's text and the barcode showed. However, that app allowed you to print preview and print the bar code but when you did the font of the barcode in the print preview and printout changed to like Arial or what ever it was, same issue as I am seeing in my app.
所以我然后运行了字体附带的程序。我发现有趣的部分是,当您输入文本时,程序会对其进行编码并设置标签文本并显示条形码。但是,该应用程序允许您打印预览和打印条形码,但是当您在打印预览和打印输出中执行条形码字体时,它更改为 Arial 或其他任何内容,这与我在我的应用程序中看到的问题相同。
This is how the xaml looks at the moment but as I say, I have tried just setting the font in expression blend.
这就是 xaml 目前的样子,但正如我所说,我已经尝试在表达式混合中设置字体。
#Code 128 is the font name and not the file name.
#Code 128 是字体名而不是文件名。
<TextBlock x:Name="tbkBarCode" FontFamily="/Fonts/#Code 128" FontSize="24" HorizontalAlignment="Center"/>
Any ideas?
有任何想法吗?
回答by Eirik
Try this:
尝试这个:
<TextBlock x:Name="tbkBarCode" FontFamily="pack://application:,,,/Fonts/#Code 128" FontSize="24" HorizontalAlignment="Center"/>
EDIT:I tried a different font first and this way of doing it worked.
编辑:我首先尝试了一种不同的字体,这种方法奏效了。
I downloaded and tried it with a Code 128 font and it didn't work right away. To get it to work I had to change the Build Action to "Content" and the Copy to Output Directory to "Copy if newer".
我下载并尝试使用 Code 128 字体,但它没有立即起作用。为了让它工作,我必须将构建操作更改为“内容”,并将复制到输出目录更改为“如果更新则复制”。
回答by Colin Smith
If you create a test Windows Forms app, add a TextBox and select the "Code 128" font as the font to use, then it correctly shows the Barcode font.
如果您创建一个测试 Windows 窗体应用程序,添加一个 TextBox 并选择“Code 128”字体作为要使用的字体,则它会正确显示 Barcode 字体。
With WPF it looks the rendering system inside that that deals with fonts doesn't like that particular font for some reason...maybe some information it expects in the file isn't there.
使用 WPF,它看起来内部处理字体的渲染系统由于某种原因不喜欢该特定字体......也许它期望文件中的某些信息不存在。
Even "Glyphs" has trouble getting anything sensible out of that font file:
甚至“Glyphs”也难以从该字体文件中获取任何合理的信息:
<Glyphs UnicodeString="ABCD1234567890" FontUri="c:\windows\fonts\code128.ttf" Height="50" Fill="#FF000000" FontRenderingEmSize="25"/>
it just returns "blocks".....which means no-character.
它只返回“块”.....这意味着没有字符。


When you use a TextBox/TextBlock etc....the fallback font is used to display the content (because it is unable to display it with the Code123 font)......which is why you see the text reverting to the Arial font (or whatever your fallback font is defined to be).
当您使用 TextBox/TextBlock 等时……使用后备字体显示内容(因为它无法使用 Code123 字体显示)……这就是为什么您看到文本恢复为Arial 字体(或任何您定义的后备字体)。
From where did you download the custom font and sample program...was it this place?
你从哪里下载自定义字体和示例程序......是这个地方吗?
Or from here?:
还是从这里?:
Are you sure the font name is #Code 128 ?
您确定字体名称是 #Code 128 吗?
If you copy the .ttf font into your \Windows\Fontsdirectory and then use CharacterMap, what font names can you see?
如果将.ttf 字体复制到您的\Windows\Fonts目录中,然后使用CharacterMap,您可以看到哪些字体名称?
After installation with the barcoderesource font, I have these fonts listed:
使用条形码资源字体安装后,我列出了这些字体:


That would mean using:
这意味着使用:
FontFamily="CCode128_S3_Trial"
to refer to the font IF it was installed in Windows
如果它安装在 Windows 中,则引用字体
or
或者
FontFamily="/Fonts/#CCode128_S3_Trial"
to refer to a .ttf font file embedded into your application.
引用嵌入到应用程序中的 .ttf 字体文件。
Did you add the .TTF font file to your project in a folder called "Fonts" and set the Build Type="Resource"?
您是否将 .TTF 字体文件添加到名为“Fonts”的文件夹中的项目并设置了 Build Type="Resource"?
回答by Franck Charlier
Here is the method I used to do that...
这是我用来做的方法...
- I add the Font on my project
- I Set property "Build action" of the Font on "Resource"
- Finally, I used the Font like that :
- 我在我的项目中添加了字体
- 我在“资源”上设置字体的属性“构建操作”
- 最后,我使用了这样的字体:
TextBlock Grid.Column="2" HorizontalAlignment="Center" FontSize="32" VerticalAlignment="Stretch" FontFamily="./#Code 128" Grid.Row="5" Margin="1" Text="{Binding Checksum}"/

