Defining a custom FontFamily with several font files in XAML/C# - c#

I'm trying to define a custom font in a C# WinRT app. I can define a single-file font in XAML like so:
<Page.Resources>
<FontFamily x:Key="Cousine">/Fonts/Cousine-Regular.ttf#Cousine</FontFamily>
</Page.Resources>
But I have several other font files that I would like to be used where appropriate:
Cousine-Bold.ttf
Cousine-BoldItalic.ttf
Cousine-Italic.ttf
How do I define a font family that uses the different fonts for the different font weights/variants?

They should all be in the same ttf file and you reference them like:
/Fonts/Cousine-Regular.ttf#Cousine
/Fonts/Cousine-Regular.ttf#Cousine Bold
/Fonts/Cousine-Regular.ttf#Cousine Italic
/Fonts/Cousine-Regular.ttf#Cousine Bold Italic
I don't know if the last one is right. I came here looking for the answer to that question. It might be BoldItalic. Whatever it is, I believe you create that name when you create the ttf file so you would be able to figure it out.

Related

How to apply custom font icon shapes in UWP

I want to show some custom shapes through font icons in my UWP project. I have created my own .ttf file for custom shapes. But I don't know how to apply it programmatically.
TextBlock text = new TextBlock();
string font = "Assets/mycustomfont.webfont.ttf#MyCustomFont";
text.FontFamily= new Windows.UI.Xaml.Media.FontFamily(font);
this.grid1.Children.Add(text);
Anyone please help me on this.
The approach you are using is practically correct, but make sure to check the following:
The .ttf file must be included in the project and have Build Action set to Content in the Properties window
Start the path to the font with / to make sure it begins in root.
Ensure the # suffix actually matches the font metadata. A font viewer app like dp4 Font Viewer can help you with that. Use the Font Family name as the suffix.
I have written an article on my blog about using custom fonts in UWP so check it out to see if you haven't missed some of the steps there.

How to get Internal Font Name programmatically in UWP mobile and desktop

I have downloaded custom font through code and want to apply it on my text view for that I want to know the exact name of font.
Note:
I want to know the font internal name not the font file name.let a font is abc.tff and when open the font the Name at top is "Arial", so i need the name Arial.
I have found method for pure windows 8/7 desktop applications not for UWP.
There is no way that I've found to extract the metadata from a font in order to find the name.
What you could do is create an observable collection that lists the data yourself... not ideal, but realistic.
Add(new Font("ABeeZee", "/Fonts/ABeeZee-regular.ttf#ABeeZee", "Sans Serif")
Then when you call the font, you can grab the data from your collection.

Get FontFamily from Font instance

As stated in the title i have a Font instance and i would get the FontFamily used to build that Font instance.
I need that specific FontFamily istance (or the ability to istantiate an equivalent instace).
Edited :
As asked i used Winform (on windows mobile, so i use NetCF 3.5)
You can use this to get the FontFamily of a Font:
FontFamily ff = oldFont.FontFamily;
Update - In .NET CF, all classes are optimized for minimal resource usage. The FontFamily property, thus does not exist in the Font class.

WPF app custom ttf font not being used?

I downloaded a custom font which displays EAN 128 barcode.
I also downloaded some source code and sample program which converts a string to an encoded string to use with the font (includes check sum etc).
I installed the font, opened my WPF app and set a textblock font to this new font and added some text. The text comes out as if it was Arial or something.
I tried adding the font as a resource and referencing it that way but still not joy.
So I then ran the program that came with the font. The interesting part I found was that when you entered text, the program encoded it and set a label's text and the barcode showed. However, that app allowed you to print preview and print the bar code but when you did the font of the barcode in the print preview and printout changed to like Arial or what ever it was, same issue as I am seeing in my app.
This is how the xaml looks at the moment but as I say, I have tried just setting the font in expression blend.
#Code 128 is the font name and not the file name.
<TextBlock x:Name="tbkBarCode" FontFamily="/Fonts/#Code 128" FontSize="24" HorizontalAlignment="Center"/>
Any ideas?
Try this:
<TextBlock x:Name="tbkBarCode" FontFamily="pack://application:,,,/Fonts/#Code 128" FontSize="24" HorizontalAlignment="Center"/>
EDIT: I tried a different font first and this way of doing it worked.
I downloaded and tried it with a Code 128 font and it didn't work right away. To get it to work I had to change the Build Action to "Content" and the Copy to Output Directory to "Copy if newer".
Here is the method I used to do that...
I add the Font on my project
I Set property "Build action" of the Font on "Resource"
Finally, I used the Font like that :
TextBlock Grid.Column="2" HorizontalAlignment="Center" FontSize="32" VerticalAlignment="Stretch" FontFamily="./#Code 128" Grid.Row="5" Margin="1" Text="{Binding Checksum}"/
If you create a test Windows Forms app, add a TextBox and select the "Code 128" font as the font to use, then it correctly shows the Barcode font.
With WPF it looks the rendering system inside that that deals with fonts doesn't like that particular font for some reason...maybe some information it expects in the file isn't there.
Even "Glyphs" has trouble getting anything sensible out of that font file:
<Glyphs UnicodeString="ABCD1234567890" FontUri="c:\windows\fonts\code128.ttf" Height="50" Fill="#FF000000" FontRenderingEmSize="25"/>
it just returns "blocks".....which means no-character.
When you use a TextBox/TextBlock etc....the fallback font is used to display the content (because it is unable to display it with the Code123 font)......which is why you see the text reverting to the Arial font (or whatever your fallback font is defined to be).
From where did you download the custom font and sample program...was it this place?
http://www.barcoderesource.com/wpfbarcode.shtml
Or from here?:
http://grandzebu.net/informatique/codbar-en/code128.htm
Are you sure the font name is #Code 128 ?
If you copy the .ttf font into your \Windows\Fonts directory and then use CharacterMap, what font names can you see?
After installation with the barcoderesource font, I have these fonts listed:
That would mean using:
FontFamily="CCode128_S3_Trial"
to refer to the font IF it was installed in Windows
or
FontFamily="/Fonts/#CCode128_S3_Trial"
to refer to a .ttf font file embedded into your application.
Did you add the .TTF font file to your project in a folder called "Fonts" and set the Build Type="Resource"?
http://msdn.microsoft.com/en-us/library/ms753303.aspx

How do I use Roboto Thin embedded in XAML?

I'm working on a C# WPF project that has the Roboto and Roboto Thin fonts embedded. They font files are in /Resources/Fonts and their build actions are set to 'Resource'. In my App.xaml, I declare the resources:
<FontFamily x:Key="Roboto">/Resources/Fonts/#Roboto</FontFamily>
<FontFamily x:Key="RobotoThin">/Resources/Fonts/#Roboto Thin</FontFamily>
The Roboto font works properly (as do some other embedded fonts), but Roboto Thin doesn't work. I've tried '.../Roboto-Thin.ttf', 'Roboto-Thin', 'Roboto_Thin' and the FontWeight property on the label/textblock but nothing works. When I try to use Roboto Thin on a UIElement, it doesn't seem to recognize it and it falls back to the default font (probably Segoe UI).
What am I doing wrong?
-- edit --
I just tried Roboto Light, but that doesn't work either. It would seem that only the regular fonts work but variations don't.
Not sure if its what you're looking for, but what works for me is the following:
<FontFamily x:Key="robotoRegularFont">/resources/Roboto-Regular.ttf#Roboto</FontFamily>
<FontFamily x:Key="robotoThinFont">/resources/Roboto-Thin.ttf#Roboto Thin</FontFamily>

Categories