WPF TTF-font not applying - c#

I trying write app in WPF (C#) and I would like use ttf font for digital clock style.
I get font from internet, in archive i have 4 files. Add it to project into Font folder. Actions for file is Resource and Copy always. Then i add font-resource for my control in xaml:
<Control.Resources>
<FontFamily x:Key="DS-Digital">.\..\Fonts\#DS-Digital</FontFamily>
</Control.Resources>
And use it to TextBlock:
<TextBlock FontFamily="{StaticResource DS-Digital}"
Foreground="GreenYellow"
Text="{Binding GameTime, Converter={StaticResource SecondsCoverter}}"/>
It work. But it font not monospace. Then I downloaded another font. It monospace. In new archive i had only one file. Add it to project and use like previous font:
<Control.Resources>
<FontFamily x:Key="DS-Digital">.\..\Fonts\#DS-Digital</FontFamily>
<FontFamily x:Key="MonoDigital">.\..\Fonts\#Digital-7</FontFamily>
</Control.Resources>
and
<TextBlock FontFamily="{StaticResource MonoDigital}"
Foreground="GreenYellow"
Text="{Binding GameTime, Converter={StaticResource SecondsCoverter}}"/>
But it not applied. What i doing wrong?
Thank you for answer and sorry my english.

Related

Custom font work in the editor but dont show up on the software [duplicate]

This question already has answers here:
Using a custom font in WPF
(4 answers)
Closed 1 year ago.
I have a custom font that I add on my project and I make it work.
This is how it look like in the editor with the WPF code :
<TextBlock Style="{StaticResource ArabicFont1}" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="30" HorizontalAlignment="Right">
<Run Text="{Binding text_arabe}"/>
</TextBlock>
Font displayed on the editor
But when I start the app, this is what font I have, with the exact same text :
Font when the app is started
So as you can see it's two different font, but in the editor, it show the right one and in the software the default one
So I don't really understand, can someone help me with this, please?
Thank you
If it can help- here is the app.xml
<Application.Resources>
<Style x:Key="ArabicFont1">
<Setter Property="TextElement.FontFamily"
Value="pack://application:,,,/fonts/#noorehidayat" />
</Style>
</Application.Resources>
You need to make sure that the font is installed on the executing system or the font is found in the execution folder of your application.
Please look at this example, maybe this will help you:
Use custom fonts in wpf c# applications

WPF and FontAwesome problem with hyphenated icons

I am using FontAwesome in my WPF app for icons.
Everything works as expected with single name icons, but whenever I try to use a hyphenated icon name, the font does not understand this, and either does not draw anything, or it draws the two icons (left and right of the hyphen) individually (if they exist).
This draws the user icon. Great.
<!--FontAwesome Font applied in style-->
<TextBlock Style="{DynamicResource mainButtonImage}" Margin="5" Text="user"></TextBlock>
This, draws the User icon, a "-" and a circle icon. It should be the "user-circle" icon.
<TextBlock Style="{DynamicResource mainButtonImage}" Margin="5" Text="user-circle"></TextBlock>
It should be drawing this: https://fontawesome.com/icons/user-circle?style=regular
When I test in notepad on my system, it works as expected.
Any ideas?
I implemented the "user-circle" icon in two ways below:
Method 1:
1.You can install FontAwesome.WPF in project's Package NuGet Manager.
2.Import xmlns:fa="http://schemas.fontawesome.io/icons/" into your XAML code.
3.Use it into your TextBlock like this:
<fa:FontAwesome Icon="UserCircle" FontSize="100"></fa:FontAwesome>
Method 2:
1.Download font awesome at fontawesome.
2.Unzip the file and copy font to the project as a resource. The path is \Font\fa-regular-400.ttf.
3.Add FontAwesome style in Window.Resources:
<Window.Resources>
<Style x:Key="FontAwesome">
<Setter Property="TextElement.FontFamily" Value="pack://application:,,,/Font/#Font Awesome 5 Free Regular" />
</Style>
</Window.Resources>
4.Use style in TextBlock and write the Unicode code of user-circle in Text like this:
<TextBlock Text="" Style="{DynamicResource FontAwesome}" />
Maybe you can refer to these two methods to implement your program.

C# UWP dynamically change global font

I have created a global font resource in App.xaml like this:
<Application.Resources>
<ResourceDictionary>
<FontFamily x:Key="GlobalFontLight">Arial</FontFamily>
<FontFamily x:Key="GlobalFont">Segoe UI</FontFamily>
<FontFamily x:Key="GlobalFontBold">Caibri</FontFamily>
</ResourceDictionary>
</Application.Resources>
In MainPage I have added a TextBlock:
<TextBlock Text="Some text" FontFamily="{StaticResource GlobalFont}" Foreground="Black"/>
<TextBlock Text="Some text 2" FontFamily="{StaticResource GlobalFontLight}" Foreground="Black"/>
<TextBlock Text="Some text 3" FontFamily="{StaticResource GlobalFontBold}" Foreground="Black"/>
And it is ok, TextBlock use my global font.
Now, I want to change that global font in Application Resources. I have tried next code:
Application.Current.Resources["GlobalFont"] = new FontFamily("Arial");
But nothing happens, TextBlock still use the old font. If I run this code before InitializeComponent(); then it is working as I want, but after that no. Anyone knows what do I do wrong? How to achieve this dynamic change of font?
Because UWP does not support DynamicResource this is quite a problem. The StaticResource and ThemeResource extensions won't save you here, because they are bound only when evaluated and will not update for the already-evaluated properties when the underlying resource changes.
The first option would be to navigate back and navigate to the same page again, so that the controls get reloaded and the resources evaluated anew.
If you want something more dynamic, please check out my answer on this SO question. If you follow that solution, you can create a class implementing INotifyPropertyChanged that will contain a property of type FontFamily, store this instance in a StaticResource and then use binding instead of StaticResource like this:
<TextBlock Text="{Binding Font, Source={StaticResource CustomUISettings}}" />

How to use a custom font with Windows Universal app?

Like for my other windows phone 8 projects I wanted to use a custom font. But with the new Universal app architecture I struggle to put that in place.
I have created a "Fonts" folder in the shared project, I added the fonts files with the property Build Action to "Content".
I also created a "Themes" folder in the shared project and I added a ResourceDictionnary to it ("Generic.xaml").
I added a FontFamily resource :
<FontFamily x:Key="RexBoldFontFamily">/Fonts/Rex Bold.otf#Rex Bold</FontFamily>
I referenced it in the App.xaml like that:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/Generic.xaml"/>
</ResourceDictionary.MergedDictionaries>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="true" />
</ResourceDictionary>
</Application.Resources>
In my MainPage.xaml on my Windows Phone project I tried to use it like this:
<TextBlock Text="{Binding HelloWorld}" Foreground="{ThemeResource RedBrush}" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="{StaticResource RexBoldFontFamily}" FontSize="22"/>
I know for sure that this is the name of the font because this works on one of my windows phone app. And all of this is well wired because it works well with a color resource.
Any one succeded to share the font and use it,
You're doing it all right, except for the font name (part after the '#'). It seems to be Rex, not Rex Bold.
<FontFamily x:Key="RexBoldFontFamily">/Fonts/Rex Bold.otf#Rex</FontFamily>
Use this code in ur xaml
<TextBlock x:Name="txtblk" HorizontalAlignment="Left" TextWrapping="Wrap" FontFamily="Assets/filename.ttf#Font Name"/>

Adding a custom font to a windows phone 8 app

I've read articles such as this 31 days of windows phone day 24 embedding-fonts and this windows 8 xaml tips custom fonts that explain very clearly and simply how to do this.
But for some reason I can't figure out, my custom font doesn't show on my app, whether in the designer view or when I run it.
To be sure I haven't left any step out, I'm showing what I did here for any suggestions on what my issue might be:
1. First, this is my xaml textblock code
<StackPanel Grid.Row="1" x:Name="BottomPanel" Height="120" Width="800" Orientation="Horizontal">
<StackPanel.Background>
<SolidColorBrush Color="{StaticResource PhoneSemitransparentColor}"/>
</StackPanel.Background>
<TextBlock x:Name="MainTextBlock" FontFamily="/Fonts/KOMIKAX_.ttf" TextWrapping="Wrap" Text="Hello" Width="680" Height="120" Margin="10,0,0,0" FontSize="25" Padding="20,10"/>
</StackPanel>
2. I created a folder in my project, called it "Fonts" and that is where I copied my font file.
3. I changed the Build Action of the font file to Content. I left the Copy To Output Directory property as None at first, then changed it to Copy If Newer. No success.
What have I done wrong or what did I leave out?
This work for me.
First make a folder in your App with the name Fonts, then Add font to it, then use the following syntax. I used it, it works perfectly.
<TextBlock FontFamily="/Fonts/Comic.ttf#Comic" TextWrapping="Wrap" Text="Hello" FontSize="25"/>
Make sure that the Build Action must be set to Content,
Right Click on font in folder and click property, then select Build Action as Content, otherwise it will not work on Emulator and Device.
Try using this
<TextBlock x:Name="MainTextBlock" FontFamily=".\Fonts\KOMIKAX_.ttf#Komika Axis" TextWrapping="Wrap" Text="Hello" Width="680" Height="120" Margin="10,0,0,0" FontSize="25" Padding="20,10"/>
In case this doesn't work, remember the format is .\FontPath\FontFileName.ttf#FontName

Categories

Resources