Use Custom Font in DrawText Wpf C# - c#

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.

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

Related

Change color in RichTextBox

I am using this simple example from MSDN
to insert lines in a RichTextBox.
FlowDocument myFlowDoc = new FlowDocument();
Run myRun = new Run("This is flow content and you can ");
Bold myBold = new Bold(new Run("edit me!"));
Paragraph myParagraph = new Paragraph();
myParagraph.Inlines.Add(myRun);
myParagraph.Inlines.Add(myBold);
myFlowDoc.Blocks.Add(myParagraph);
RichTextBox myRichTextBox = new RichTextBox();
myRichTextBox.Document = myFlowDoc;
I want to apply a chosed color to the lines of text, but how to do it?
The Paragraph or Run classes doesn't have any direct method to change the color.
EDIT
I don't want to use all the awkard SelectionStart, SelectionEnd stuff as posted on the linked post!.
My case is different and is much more simple: the solution posted from mm8 explains it and is very elegant.
One single line of code and that is!
Please see the answer!
The Paragraph or Run classes doesn't have any direct method to change the color.
The Run class inherits from TextElement and this class has a Foreground property that you can set to a Brush:
Run myRun = new Run("This is flow content and you can ") { Foreground = Brushes.Red };
Bold myBold = new Bold(new Run("edit me!") { Foreground = Brushes.Gray });
You can get/set text color via Foreground property of the rich text box. As bellow example, I changed the text color of rich text box to blue:
myRichTextBox.Foreground = Brushes.Blue;
Happy coding!

How to create unstable font sizes?

I have created a TextBox on the code
RichTextBox tb3 = new RichTextBox();
Then I wrote an other code (in the same void) to change it's font:
tb3.Font.Size(FontSize.Text);
But the following error appeares : "non-invocable member 'Font Size' cannot be used like a method.
Note: "FontSize" is an ID for a textbox.
How can I do it without creating a new Font?
Please help!
tb3.Font.Size isn't a method, it is
public float Size { get; }
and you can't change it so.
if you want to change font see it -> link1
and it link2
you should create new font, for example:
tb3.Font = new Font(tb3.Font.FontFamily, 23, FontStyle.Regular);

How to set DefaultFont to custom font in OxyPlot?

I am using OxyPlot in a WPF application to plot a simple LineSeries. I also defined Roboto as the standard font of my application. However my PlotView does not use this font but uses its default font Segoe UI.
The PlotModel offers a DefaultFont property to change this font. This property does not accept a FontFamily but only a string. So if I try to modify the font by entering a FontFamily name which is installed on my system it works:
var model = new PlotModel
{
DefaultFont = "Verdana"
};
If I try to do the same with Roboto it does not:
var model = new PlotModel
{
DefaultFont = "./Resources/Roboto/#Roboto"
};
or
var model = new PlotModel
{
DefaultFont = "/MyNamespace;component/Resources/Roboto/#Roboto"
};
What am I missing here? Is there maybe another way to accomplish this?
Note: This is my first question, please tell me what I can improve in the future.

Adding multiple external fonts to my winform application

I would like to add or deploy the app with more than 10 external fonts from my resource folder. I have gone through various SO questions but none of them suits my requirement except this SO answer.
Now I have implemeted the same thing in my winform app and I would like to add these fonts to combo box with their styles such as shown below.
Any suggestions are most welcome.
You have to draw the items in the ComboBox yourself if you want to show them in the drop down in their appropriate font. This isn't hard, the following code does the bare minimum:
var comboBox = new ComboBox();
comboBox.DisplayMember = "Name";
comboBox.Items.AddRange(FontFamily.Families);
comboBox.DrawMode = DrawMode.OwnerDrawFixed;
comboBox.DrawItem += (s, e) => {
var fontFamily = (FontFamily) comboBox.Items[e.Index];
var itemText = comboBox.GetItemText(fontFamily);
// Some fonts don't work with a regular style, if they don't have a
// regular style, we'll default to the provided font.
if (!fontFamily.IsStyleAvailable(FontStyle.Regular))
{
TextRenderer.DrawText(
e.Graphics, itemText, e.Font, e.Bounds, e.ForeColor, e.BackColor
TextFormatFlags.VerticalCenter | TextFormatFlags.Left);
}
else
{
using (var font = new Font(fontFamily, comboBox.Font.Size, FontStyle.Regular))
{
TextRenderer.DrawText(
e.Graphics, itemText, font, e.Bounds, e.ForeColor, e.BackColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Left);
}
}
};
This implementation handles a FontFamily not having a regular font style by falling back to the ComboBox's default font. You can handle this more gracefully by attempting to find a valid style for the FontFamily before creating the font.

RichTextBox font set to all lines in C#

I changed the font of a RichTextBox, but this change does not set to all of its characters properly. Indeed there exist two font for the RichTextBox and that is not beautiful. Is there a solution for this problem!?
This code does not work correctly:
this.richTextBox1.Font = new System.Drawing.Font("Maiandra GD", 12);
Set the selection to the entire box and set the SelectionFont.
this.richTextBox1.SelectionStart = 0;
this.richTextBox1.SelectionLength = this.richTextBox1.SelectionLength;
this.richTextBox1.SelectionFont = new System.Drawing.Font("Maiandra GD", 12);

Categories