Include font from resource folder - c#

I am trying to have a label in my WPF-Window that has a special font that is probably not installed on all computers.
This is currently my xaml-code for the label:
<Label x:Name="ping" Content="00" FontFamily="pack://application:,,,/resources/#visitor" HorizontalAlignment="Left" Margin="165,10,0,0" VerticalAlignment="Top"/>
And here is my project hierarchy:
But why doesn't the font appear in the application/on the label?

Sorry to drag up an old thread, but I found the solution to this. While the font filename is visitor1, FontFamily doesn't look for this and instead looks for the font name. Open the font file and you'll see the name is actually "Visitor TT1 BRK". Use this in your code and it will work:
FontFamily="CustomFonts\#Visitor TT1 BRK"

Related

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.

Why is the custom font not being used? Xamarin.Forms.iOS

I am working with custom fonts right now and currently a font named: "Yellowtail-Regular.tff". I created a map called Fonts in my iOS folder and I added the tff file in there. After that I went to my Info.plist and created a Fonts provided by application and in there I created a new string called Fonts/Yellowtail-Regular.ttf.
When I now try to use it like this:
<Label FontFamily = "Yellowtail" Text = "Testing my label" />
The standard font (Helvetica Neue) still remains. I also tried with:
<Label FontFamily = "Yellowtail-Regular" Text = "Testing my label" />
But with the same outcome.
Am I missing a step or am I doing something wrong?
If you are using Yellowtail-Regular.ttf from http://www.1001freefonts.com/yellowtail.font, then that font name under iOS would be just Yellowtail.
Each platform has different naming conventions, so using Device.OnPlatform might look something like:
public Label()
{
FontFamily = Device.OnPlatform("Yellowtail", "Yellowtail-Regular", "/Assets/Yellowtail-Regular.ttf#Yellowtail-Regular");
}
To confirm your iOS font family name, on macOS open your .ttf using Font Book and the name that macOS/iOS will use is the one in the title bar of the app.
Some times font book will display the name with spaces.
In my case:
Font name is : SFProDisplay-Ultralight. But the font book shown the
name as "SF Pro Display Ultralight". Due to this custom font is not working in iOS.
So you can use the below code to find the correct font name and apply it in the iOS.
Add this code in the AppDelegate launch method. Then you can find the list of viable font names in the Application output.
foreach (var family in UIFont.FamilyNames)
{
System.Diagnostics.Debug.WriteLine($"{family}");
foreach (var names in UIFont.FontNamesForFamilyName(family))
{
System.Diagnostics.Debug.WriteLine($"{names}");
}
}
After that you can use xaml to refer the font.
<OnPlatform x:Key="SFProDisplayUltraLight" x:TypeArguments="x:String">
<On Platform="iOS" Value="SFProDisplay-Ultralight" />
<On Platform="Android" Value="SF-Pro-Display-Ultralight.otf#SF Pro Display Ultralight" />
</OnPlatform>
Then Apply it for the control.
<Lable Text="Welcome" FontFamily="{StaticResource SFProDisplayUltraLight}"/>

How do I include an icon font?

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.

Changing Textbox FontFamily does not take effect in windows phone 8.1

I need to use a font family in my application. I added the following snippet in App.Resources
<FontFamily x:Key="SourceSanProFontFamily">Assets/Fonts/SourceSansPro-Regular.ttf#Source Sans Pro</FontFamily>
And i applied fontfamily to the textblock.
<TextBlock Text="Welcomeg!" FontFamily="{StaticResource SourceSanProFontFamily}"></TextBlock>
When application is not running,it is fine and i can see the change. But when I running the application it doest not take effect.
Why this happens? How to resolve this?
Our combined forces managed to solve the problem - after checking the working sample project it turend out that when the FontFamily is declared in other ResourceDictionaries additional / is needed:
<FontFamily x:Key="SourceSanProFontFamily">/Assets/Fonts/SourceSansPro-Regular.ttf#Source Sans Pro</FontFamily>

Load and use barcode font in TextBlock

Hey, I am trying to create a BarCode input box. On it a textblock is showing a preview of what the text input will look like in the specified Barcode type.
I have a bunch of .TTF files that are the barcode fonts, they have been used in a WinForms app so I am sure they are correct. I try to load them to memory (don't want to install them) using:
sBarCodeFonts = new PrivateFontCollection();
unsafe
{
fixed (byte* p = Resources.Code39)
{
IntPtr MyIntPtr = (IntPtr)p;
sBarCodeFonts.AddMemoryFont(MyIntPtr, Resources.Code39.Length);
}
fixed (byte* p = Resources.Code128b)
{
IntPtr MyIntPtr = (IntPtr)p;
sBarCodeFonts.AddMemoryFont(MyIntPtr, Resources.Code128b.Length);
}
fixed (byte* p = Resources.i2of5)
{
IntPtr MyIntPtr = (IntPtr)p;
sBarCodeFonts.AddMemoryFont(MyIntPtr, Resources.i2of5.Length);
}
fixed (byte* p = Resources.ean13)
{
IntPtr MyIntPtr = (IntPtr)p;
sBarCodeFonts.AddMemoryFont(MyIntPtr, Resources.ean13.Length);
}
}
This seems to load the PrivateFontCollection correctly, quickwatch indicates so.
In the Barcode class, I have a MyFontFamily property, which contains the System.Media.FontFamily that corresponds to the loaded file.
This property is loaded like this:
MyFontFamily = new System.Windows.Media.FontFamily(sBarCodeFonts.Families[0].Name);
And it seems to be loaded correctly as well.
Then, I have a Barcode object, and I'm using a TextBlock to display it's text, using it's FontFamily:
<TextBlock Text="{Binding Path=MyBarcode.TextContent, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}, Mode=OneWay}"
FontFamily="{Binding Path=MyBarcode.MyFontFamily, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}, Mode=OneWay}"
Name="txt"
Grid.Row="2" />
The TextBlock displays the text using a default font every time. I've debugged and the FontFamily is correctly set to one of the loaded ones in the previous C# code.
Any clues?
Thanks.
EDIT:
Trying to simplify the problem, I've created a very quick and dirty test app to load the TTF and show it, this is the only code (besides the XAML with only a grid):
System.Windows.Media.FontFamily lFamily = new System.Windows.Media.FontFamily(new Uri(#"E:\Prototypes\TestApp\Resources\Code128b.ttf", UriKind.Absolute), "Code128bWin");
TextBlock lTextBlock = new TextBlock();
lTextBlock.Text = "jsldkasjLKJOSDIFUEWR32849792837.,´` ";
lTextBlock.FontFamily = lFamily;
lTextBlock.FontSize = 50.0;
grid.Children.Add(lTextBlock);
And it still shows the default font.
<FontFamily x:Key="MyFont">/WpfTest;component/Resources/#Airplanes in the Night Sky</FontFamily>
<TextBlock FontFamily="{StaticResource MyFont}">Hello world</TextBlock>
Given that when you open the .ttf the "Font name: .." is "Airplanes in the Night Sky" and your project namespace is "WpfTest" and you dragged the font into a folder called "Resources"
I don't know about the method you're using, but I've successfully embedded fonts in a WPF application before by adding the files to the project as resources, and then creating a WPF/XAML resource in markup to reference the font file (using a pack Uri). This doesn't require the fonts to be installed on the machine - they're embedded in your .exe or a .dll file.
I did not find any answer for exactly this.
But I found a solution that I did not see on Internet.
I followed a recommendation of doing a folder and marking all the files inside as Resources.
But I needed to enumerate them, and that was my main problem, because I need to load all to my screen without recording the name somewhere. I just want to simple drop another font to that folder and list it.
I found this as a solution to list all the files inside my resources/fonts folder
Fonts.GetFontFamilies(new Uri("pack://application:,,,/resources/fonts/#"))
I expect it to help you organize your fonts.

Categories

Resources