Reputation: 5444
I have a custom OTF font in the Resources folder in my project which is not installed on my Windows. The Build Action of the font is already set to Resource.
Now I want to use DrawText
method and FormattedText
class to write some text on visual layer.
How can I use this custom font for the FormattedText
. I already know how to do this say for a TextBlock in XAML using the below code. But what about code-behind?
<TextBlock FontFamily="pack://application:,,,/Resources/#Quicksand Light">
StackOverflow
</TextBlock>
Here is the code I'm using to define my FormattedText object.
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));
My problem is is defining the font for typeface so that I can use it in fotmattedtext.
Upvotes: 2
Views: 2026
Reputation: 672
Something like this:
var typeface = new Typeface(new FontFamily(new Uri("pack://application:,,,/"), "/Resources/#Quicksand Light"), FontStyles.Normal, FontWeights.Regular, FontStretches.Normal);
Upvotes: 4