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.
Related
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
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.
this might be a very typical requirement, but has anyone ever tried to have a coloured film/cover on application. So I want when user performs an action the entire application get a red film on it. its not simply changing colour of controls, its like placing a coloured film so that everything that was white appears red, green appears yellow black remains black and so on..
I know I haven't added any attempts to this question because everything that I tried made no sense at all, only thing I can think of is to adjust RGB of every colour so that a particular value is added to it.
but here I just want to ask is there a simpler way of placing a red translucent pane on my application ?
Thanks
Like #HighCore suggested, you can simply put a control on top of everything else by putting it at the bottom of your XAML:
<Window>
<Grid>
<!-- Your Content Here -->
<Grid Background="Red" Opacity="0.3" />
</Grid>
</Window>
On that last grid you can also bind some things to it as your application needs, such as
<Window>
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVis" />
</Window.Resources>
<Grid>
<!-- Your Content Here -->
<Grid Background="{Binding MyColorCover}" Visbility="{Binding CoverIsVisible, Converter={StaticResource BoolToVis}}" Opacity="{Binding CoverOpacity}" />
</Grid>
</Window>
This would allow you to change the film cover's color/opacity/visibility at runtime. This assumes you got your bindings/MVVM stuff set up appropriately, which is another topic altogether.
I'm trying to include icon font in my wpf application, but my icon never appears and only displays a rectangle.
Projet structure :
MyProject/
-- Fonts/
-- myFont.ttf
-- MyWindow.xaml
MyWindow.xaml
<Label Text="" FontFamily="/Fonts/#myfont" />
"myfont" is the name of the font (the one I see after "Font name" when I double click on the file). The file's Build Action is set as "Resource".
I also tried the following :
Using style
<Window.Resources>
<Style x:key="MyFont">
<Setter property="TextElement.FontFamily"/Fonts/#myfont" />
</Style>
</Window.Resources>
<Label Text="" Style="{StaticResource MyFont}" />
Using pack URI
<Window.Resources>
<Style x:key="MyFont">
<Setter property="TextElement.FontFamily" value="v/Fonts/#myfont" />
</Style>
</Window.Resources>
<Label Text="" Style="{StaticResource MyFont}" />
None of them work. Moreover, the last one (with pack URI) generates an error. Something like an "index out of range error" (I have VS in French so I can't tell what the message is in English).
Already tried to find a solution on the internet, but all I could find was using pack URI, or checking that the name of the font is correct or that Build Action is set to "Resource".
Thank you.
EDIT
I precise that I don't want to use Blend. I would like to manage to do it directly in code. Thank you.
Wpf supports embedded fonts - the easiest way to set these up is to assign a font to a visual element, such as a label via the font family property. I normally use blend (part of visual studio) which displays a font manager button. You can then embed the font in your application from there.
Otherwise MSDN have a page which details other options. https://msdn.microsoft.com/en-us/library/ms753303%28v=vs.110%29.aspx
If you edit the .csproj file for your project and add the item group...
<ItemGroup>
<BlendEmbeddedFont Include="Fonts\ahronbd.ttf">
<IsSystemFont>True</IsSystemFont>
<All>True</All>
<AutoFill>True</AutoFill>
<Uppercase>True</Uppercase>
<Lowercase>True</Lowercase>
<Numbers>True</Numbers>
<Punctuation>True</Punctuation>
</BlendEmbeddedFont>
</ItemGroup>
You will need a Fonts folder with that .ttf file in there as part of your solution folder. This isn't pretty, but it will work without relying on additional tools.
I using a separate project for my styles a and i have some line of codes like this in it:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Style x:Key="NazaninFont" TargetType="Control">
<Setter Property="FontFamily" Value="pack://application:,,,/Extra/Fonts/#IRNazanin"/>
</Style>
......
</ResourceDictionary>
My another styles (like control effects and...) work well when i use my style key in the element like this:
Style="{ms:MyStyleRef ResourceKey=MyStyleKey}"
But When i use the following code in my Lable element
<Label Style="{ms:MyStyleRef ResourceKey=NazaninFont}" x:Name="LabelRemainingSec" Content="{Binding RemainingSec}"/>
I have:
In Design Time >> The FontFamily is set on IRNazanin on the properties panel but have not correct font view in the designer!
BUT
In Run Time >> The FontFamily is set on Tahoma (Window font)
Also i tested this way:
I added a style base on my font styles in the separate project, in top of my Window, like this:
<Style x:Key="NazaninFont" BasedOn="{ms:MyStyleRef ResourceKey=NazaninFont}" TargetType="Label"/>
Then i use it in my Lable normaly:
<Label Style="{StaticResource NazaninFont}" x:Name="LabelRemainingSec" Content="{Binding RemainingSec}"/>
Result is same as previous way:
In the Design Time in the FontFamily is on IRNazanin and have not correct font view in XAML designer! And in Run Time it is on Tahoma
What do you think about my problem? I think think my styles can not give the font path to the Label control correctly.
This may or may not be what you are after, but I'll take a guess that it is :p
You can define a FontFamily Resource in your resource dictionary, or where ever you want really. Take note of how I have defined it in Window.Resources.
I have included the physical font file in my project inside a folder called 'fonts' (no need for packing), and referenced it using the Font name: value. This is found by double clicking the font file (where it displays a whole heap of demo text and the option to install). You do not use the name of the file itself, you use the Font name: value when referencing. Also, don't forget the # at the start of any font reference! :)
Then, call it like you would any other resource for font family
Both the labels will be a different font. Please note that the font change won't be visible during design time unfortunately, only during runtime.
Working code as a demonstration:
<Window x:Class="Tinker.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="400" Width="500">
<Window.Resources>
<FontFamily x:Key="MyFont">fonts/#Roboto Thin</FontFamily>
</Window.Resources>
<StackPanel VerticalAlignment="Top">
<Label FontSize="36" Content="Helloooooo World!"/>
<Label FontSize="36" FontFamily="{StaticResource MyFont}" Content="Helloooooo World!"/>
</StackPanel>
</Window>