【问题标题】:Use Custom Font in DrawText Wpf C#在 DrawText Wpf C# 中使用自定义字体
【发布时间】:2014-04-02 15:48:35
【问题描述】:

我在我的项目的 Resources 文件夹中有一个自定义 OTF 字体,它没有安装在我的 Windows 上。 字体的Build Action 已经设置为Resource

现在我想使用DrawText 方法和FormattedText 类在可视层上写一些文本。

如何为FormattedText 使用此自定义字体。我已经知道如何使用以下代码对 XAML 中的 TextBlock 执行此操作。但是代码隐藏呢?

<TextBlock FontFamily="pack://application:,,,/Resources/#Quicksand Light"> 
    StackOverflow
</TextBlock>

这是我用来定义我的 FormattedText 对象的代码。

var f = new FontFamily("pack://application:,,,/Resources/#Quicksand Light");
var typeface = new Typeface(f, new FontStyle(), new FontWeight(), new FontStretch());
var cultureinfo = new CultureInfo("en-us");
var ft = new FormattedText("Stackoverflow", cultureinfo, FlowDirection.LeftToRight,
    typeface, 28, Brushes.White)
dc.DrawText(ft, new Point(0,0));

我的问题是定义字体的字体,以便我可以在 fotmattedtext 中使用它。

【问题讨论】:

    标签: c# wpf xaml fonts


    【解决方案1】:

    类似这样的:

    var typeface = new Typeface(new FontFamily(new Uri("pack://application:,,,/"), "/Resources/#Quicksand Light"), FontStyles.Normal, FontWeights.Regular, FontStretches.Normal);
    

    【讨论】:

    • 非常感谢。这正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 2014-05-05
    • 1970-01-01
    • 2010-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-12
    • 2011-02-27
    相关资源
    最近更新 更多