How to add new font to label in WPF? - c#

I need to use a pixel font "8bitoperator_jve" in label in WPF from https://ru.fonts2u.com/8bitoperator-jve-regular.%D1%88%D1%80%D0%B8%D1%84%D1%82. I've downloaded .ttf file with font and put it to Resources, set 'Resource' in Build Action and tried the following code:
<Label FontFamily="./#8bitoperator_jve" FontSize="100" Foreground="White"> Check </Label>
It wouldn't work. How can I fix it?

I suggest you use it directly after installation
this.label1.FontFamily = new FontFamily("8bitoperator JVE Regular");

You are inserting an incorrect font name. You must enter the actual font name, not the filename. Open the font and copy the Font name. Try this:
<Label FontFamily="./#8bitoperator JVE" FontSize="100" Foreground="White"> Check </Label>
I'm not sure if ./ is correct, if the code above won't work, try to remove this and leave just #8bitoperator JVE

Related

UWP C# How to embed costum font

So i wanted to use a costum font on my uwp app, i download the font from google fonts then added to the folder Fonts the file as been set as content and the file gets copied on the output directory everytime which i guess is the app package?
<StackPanel>
<TextBlock Text="Cantami Diva" FontSize="72"/>
<TextBlock Text="Cantami Diva" FontSize="72" FontFamily="ms-appx:///Assets/Fonts/Arvo-Regular.tff#Arvo"/>
</StackPanel>
here is the problem, the second textblock where the font is applied is getting displayed the same as the first, i guess it cant find the font, but if i install the font on my computer it works perfectly, is there no way to use the font without having the user to install it?
The True Type Font extension is .TTF not .TFF
I did a quick test and after changing the typo you had it worked fine for me.
<StackPanel>
<TextBlock Text="Cantami Diva" FontSize="24"/>
<TextBlock Text="Cantami Diva" FontSize="24" FontFamily="ms-appx:///Assets/Fonts/Arvo-Regular.ttf#Arvo"/>
</StackPanel>

WPF - Import custom fonts in C#

I would like to import custom fonts on my WPF application so that they work without having the client to install them.
All the answers I have found so far are in XAML, I would like to do it only in C#.
My fonts are in Resources/Fonts/.
I have already tried this :
Fonts.GetFontFamilies(new Uri("pack://application:,,,/Resources/Fonts/#"));
But it didn't work.
I did everything bluetoothfx said but it still did not work.
Then I changed the Build action of my fonts (it was to Content), to Embedded Resource, and it worked. Resource works also for me.
Thanks anyway.
I think the way that you are working will not work.
At first create a folder name fonts then Add the font to your project, change its Build Action to Content.
Now you need to find the internal name (Real name) of the font not the font-file name. You can have it by opening the font file and you can see it on top.
Now edit App.xaml
<Application.resources>
<style x:key="MYFONT_INTERNAL_NAME">
<setter property="TextElement.FontFamily"
value="pack://application:,,,/fonts/#MYFONT_INTERNAL_NAME" />
</style>
Now use it in your code like:
<TextBlock Style="{StaticResource MYFONT_INTERNAL_NAME}" FontSize="16" Text="Font Style" />
To know more search here:
http://www.alteridem.net/2014/02/24/custom-fonts-in-wpf-applications/

Adding custom font in silverlight

I tried to add google font to my silverlight project. so i downloaded the zip and added the fonts to Fonts folder:
I tried to load it like this:
<controls:DynamicTextBlock Grid.Column="2"
Width="200"
HorizontalAlignment="Left"
FontSize="{StaticResource FontSize6}"
FontFamily="/EZTrader;Component/Fonts/#RobotoLight"
Foreground="White"
Text="{Binding UserFullName}"
ToolTipService.ToolTip="{Binding UserFullName}" />
and nothing happened.
what should i do to fix this?
thanks!
If you are using windows, Open the font you want to use with Windows Font Viewer
Check the Font name:. That name is what you will use when referencing it. Note: the font name may not always match the filename of the .ttf and can also include spaces.
You want to make sure that the `Build Action' of the included file in the project is set to Resource as you want to be able to reference it from xaml.
You can create a static resource for the FontFamily in your App.xaml so you can reference it throughout your project.
Assuming the name of the assembly for your project is EzTrader.dll
<FontFamily x:Key="RobotoLightFontFamily">/EzTrader;component/Fonts/RobotoLight.ttf#[Font name here]</FontFamily>
<FontFamily x:Key="RobotoThinFontFamily">/EzTrader;component/Fonts/Roboto-Thin.ttf#[Font name here]</FontFamily>
<!-- other font resources -->
Then build the project.
From there you should be able to reference it like so
<controls:DynamicTextBlock Grid.Column="2"
Width="200"
HorizontalAlignment="Left"
FontSize="{StaticResource FontSize6}"
FontFamily="{StaticResource RobotoLightFontFamily}"
Foreground="White"
Text="{Binding UserFullName}"
ToolTipService.ToolTip="{Binding UserFullName}" />
Reference:
How to use your own fonts within Silverlight
Using Custom Fonts in Silverlight
Using built-in, embedded and streamed fonts in Silverlight
If you can, check what the display name of the font is, instead of the file name for the part after the #. It may actually be Roboto Light.
<controls:DynamicTextBlock FontFamily="/EZTrader;component/Fonts/RobotoLight.ttf#Roboto Light" />
You may also need to change the build properties on your font file as seen here: http://geekswithblogs.net/mamta_m/archive/2010/07/01/adding-custom-fonts-to-your-silverlight-application.aspx.

Add icon to button with Visual Studio

I have a simple question. I want to add an icon to a C# WPF Button control. I do not want to have to write C# code, or edit XAML to do this.
However, when I click on the button in the Designer, there is no option under properties to set an image. How do you do this through the Visual Studio GUI?
The easiest/best way to do this is to add an Image control as the Content of the Button.
The property window is somewhat limited in what it can do, and only supports text for that property. This does include bindings, so you could use an Image StaticResource. I couldn't find an easy way to create one from the property designer either though.
So basically, you are stuck with editing XAML. Either with a direct Content property or by creating an element in Resources Its not that bad! Just write:
<Button>
<Button.Content>
<Image ImageSource="..."/>
</Button.Content>
</Button>
Now of course, you could create a custom button that exposed that property via the designer, but thats even more XAML. Its WPF, you are going to have to write XAML, so learning how should be a priority.
Visual Studio 2015:
create a button.
create an image.
set the source of the image to the right file of your resources (e.g. a .png file that you included to your project)
drag the image over the button.
A text shows that asks you to press ALT to replace the text of the button with the image.
I currently don't know how to get both, image and text, for a button.
in XAML, it looks like this:
<Button x:Name="button12" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75">
<Image x:Name="image" HorizontalAlignment="Left" Height="24" VerticalAlignment="Top" Width="24" Source="gfx/new.png"/>
</Button>
you could install FontAwesome on your Computer
Goto http://fontawesome.io and download. Open the downloaded ZIP file and inside the font folder there should be .otf file, Install that!
and in the content of the button you could simply type the desired icons code!
For eg:
if you want your button to look like this
then install FontAwesome Font
and in your button tag type Content="" FontFamily="FontAwesome"
More codes can be found here http://fontawesome.io/cheatsheet/

WPF: how to set infinity symbol as content for a label?

I have a label. As the content I want to set the infinity symbol. How can I achieve that?
<Label Content="∞" ... />
FYI: XML Character Entities and XAML
Like this:
<Label Content="∞"/>
The infinity symbol is represented by the unicode character 221e. If you follow the link it shows fonts that support the character. It seems like most popular fonts do, including Arial, which I think is the default font for labels.
Somethig like this (might need to specify size of the image):
<Label>
<Label.Content>
<Image Source="URI to Image"/>
</Label.Content>
</Label>
Edit: Since you posted a picture i assumed you have an image, if you want the symbol as text say so.
See here: http://www.fileformat.info/info/unicode/char/221e/index.htm

Categories