How do I use Roboto Thin embedded in XAML? - c#

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>

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.

UWP setting themeresource acrylic in code behind

I have a UWP app and I want to upgrade it to fluent design system. I have created a new project using Windows Template Studio and my navigation is with Pivot.
Now I want to put acrylic background on the header of the pivot. as mentioned in the design guidelines of uwp it is recommended to use 70 percent acrylic in this scenario.
So I tried to use 70 percent acrylic with following code.
private void MainPivot_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.UI.Xaml.Media.AcrylicBrush"))
{
MainPivot.Background = Application.Current.Resources["SystemControlAltHighAcrylicWindowBrush"] as AcrylicBrush;
}
}
Where MainPivot is the pivot I am using and this loaded method is loaded event for that pivot.
The problem is that it works only for either Light or Dark Theme (depends which theme was set during last run of the app), but when app is running and I change theme and switch between light or dark themes, it doesn't work well for both themes, for example if I make the theme dark the acrylic color remains white and pivot header text is also white hence creating disturbed UI.
Also the FallBack Color doesn't make sense either, for light theme fallback color is black (which blends in with black text) and same problem occurs in dark theme.
The reason I am doing it from code behind because the min project target of my app is creators update which doesn't have acrylic brush.
The Conditional XAML provides a way to use the ApiInformation.IsTypePresent method in XAML markup. This lets you set properties and instantiate objects in markup based on the presence of an API without needing to use code behind.
To use a conditional method in XAML, you must first declare a conditional XAML namespace at the top of your page.
xmlns:IsAcrylicBrushPresent="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsTypePresent(Windows.UI.Xaml.Media.AcrylicBrush)"
xmlns:IsAcrylicBrushNotPresent="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsTypeNotPresent(Windows.UI.Xaml.Media.AcrylicBrush)"
After the namespace is defined, we can use the namespace prefix to the Background property of your Grid to qualify it as a property that should be set conditionally at runtime.
<Grid Name="MainPivot" IsAcrylicBrushPresent:Background="{ThemeResource SystemControlAltHighAcrylicWindowBrush}" IsAcrylicBrushNotPresent:Background="Red">
If the device supports AcrylicBrush, it will use the SystemControlAltHighAcrylicWindowBrush. If not, it will use the Red color.

Defining a custom FontFamily with several font files in XAML/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.

Set font style without System.Drawing

I'm facing a awkward problem. I can't set the FontStyle of a text. This is annoying me cause until now I'm not able to understand why the System. Drawing is missing, why I can't import her (At least I think I shouldn't).
The context is I'm in a silverlight application, setting the font color and style of a dynamic tree.
I'm using System.Windows.Media.SolidColorBrush to the Foreground, but I trying to set the FontStyle without success. And all google that I try answer the simple: 'use System.Drawing'
What I'm missing here?
You should not try to create a new FontStyle struct, but use the static properties of FontStyles: FontStyles.Italic or FontStyles.Normal.

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

Categories