2

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.

1 Answer 1

4

Something like this:

var typeface = new Typeface(new FontFamily(new Uri("pack://application:,,,/"), "/Resources/#Quicksand Light"), FontStyles.Normal, FontWeights.Regular, FontStretches.Normal);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.