I have followed the guidelines in the official documentation to add a custom font to my native script application. https://docs.nativescript.org/ui/styling#custom-fonts
The font name is Ubuntu as shown in the image below.
I added the font Ubuntu.ttf under the folder /fonts of my project, and add the following to my app.css file :
.Ubuntu
{
font-family: Ubuntu;
}
However, once strating the application I cannot see the font applied to my label.
<Label class="Ubuntu" text="This is a test message" ></Label>
Any help will be much more appreciated.
Ah, the wonderful fonts issue on iOS. Fortunately this is something that is easy to fix.
I actually have an entire blog article on how to fonts in NativeScript.
But to simple answer your specific question the font is not actually named "Ubuntu"; that is the file name. If you look at the top-middle of your font picture you will see the name is actually "Ubuntu Gras".
So your CSS should actually look like this.
.Ubuntu
{
font-family:Ubuntu,Ubuntu Gras;
}
What this does is cause iOS and Android to auto-load the "Ubuntu.ttf" file when they can't find a font already loaded called "Ubuntu" in the system loaded fonts. (Note: The physical file name should always be the first definition.)
So after it loads it, it then tries to use "Ubuntu" as the font lookup; but this can fail on iOS on some fonts (but always works on Android). So then because it can't find the "Ubuntu" name, it uses the next available font name which we so conveniently provided for it -- "Ubuntu Gras". Since the font was already loaded (i.e. from the "Ubuntu" name) then the iOS operating system can now find the "Ubuntu Gras" name, and so it uses it properly.
Related
I'm new to Xamarin development, but try to be exact.
Using Visual Studio 2022, with Xamarin version 17.0.0.182 (as displayed in VS About).
Found this great article on how to use Bootstrap icons, but it seems to be out of date. When I followed the steps and provided hex codes for the characters, I didn't get the expected glyiphs.
I found that
new Xamarin needs new methods,
new Bootstrap fonts need new ways.
Here they are.
1.) Get the font from the Bootstrap github repo at https://github.com/twbs/icons
Go to releases (https://github.com/twbs/icons/releases), choose latest, scroll down to Assets, download the bootstrap-icons-x.x.x.xip file.
Unzip it, and find fonts/bootstrap-icons.woff.
Not something you can use in Xamarin right away :-(
2.) Convert the woff file to ttf
I googled for a converter and used https://cloudconvert.com/woff-to-ttf
Now you have the ttf you need :-)
3.) Now follow the current Xamarin method of adding a font to your app.
The process is described here: https://devblogs.microsoft.com/xamarin/embedded-fonts-xamarin-forms/
In short:
add the ttf file to the shared Xamarin project, Embedded Resources / Fonts
change file properties / Build Action to "Embedded resource"
register the font by adding a line to the end of Assemblyinfo.cs:
[assembly: ExportFont("bootstrap-icons.ttf", Alias = "Bootstrap")]
4.) Use it in xaml like
<Label FontFamily="Bootstrap" Text=""/>
to display a magnifying glass.
You may also define a Label style, but I'll skip that for now.
4/b.) To use it in xaml via binding
Oh, if things were easy...
When using a binding to display a glyph, there's one more hoop to jump, thanks to these guys for the solution: Using data binding, how do I bind text which contains emojis to a label and have it display correctly?
So, in xaml:
<!-- set BindingContext to MyViewModel -->
<Label FontFamily="Bootstrap" Text="{Binding StateIconName}" />
in your MyViewModel:
public string StateIconName
{
get => WebUtility.HtmlDecode("");
}
5.) To browse available glyphs (icons)
Open the overview page: https://icons.getbootstrap.com/
6.) To find the unicode character code, also referred to as "Unicode HTML Entity"
Check the name of the icon above in step 5.
Open the Bootstrap-icons.css file from Github: https://github.com/twbs/icons/blob/main/font/bootstrap-icons.css
Search for the name of the glyph to find the character code, something like:
.bi-zoom-in::before { content: "\f62c"; }
Use this code in xaml like
I followed the Nativescript guide to using custom fonts and it's been working well. As it notes, I use the Mac default Font Book and use that exact name in the css, regardless of what the actual filename is. However, I am using some fonts now that are coming up with the same name in Font Book. I have files Avenir-Next-LT-Medium.ttf, Avenir-Next-LT-Demi.ttf and Avenir-Next-LT-Regular.ttf, all of which just show up as Avenir Next Pro in Font Book so I don't know how to use each individually.
I try to use custom fonts in my swift app, but they don't load.
I copy the fonts.ttf in my resources folder, and I added the names in Info.plist under "Fonts provided by application " key.
I've try with "Application fonts resource path" key from .plist , but no results.
Here is the code I used to apply my font. I've try with : "MyFont.ttf" , and "MyFont"
#IBOutlet weak var label:NSTextField!
override func awakeFromNib() {
label.font = NSFont(name: "MyFont.ttf", size: 15)
}
As "Application fonts resource path" is now a string type in XCode 7.3.1 and I couldn't seem to find a way to use an Array for multiple fonts, I used "." in the Info.plist:
Application fonts resource path String .
and this seemed to work to pick up all my custom fonts in a Resources folder dynamically e.g. using Swift
labelText.font = NSFont(name: "DS-Digital", size: 48)
However, to see it in XCode design mode (to choose a font from drop-down menu), I needed to first add the font to Font Book.
However, Font Book was not required for the dynamic method to work :)
ATSApplicationFontsPath is for macOS:
ATSApplicationFontsPath (String - macOS) identifies the location of a
font file or directory of fonts in the bundle’s Resources directory.
If present, macOS activates the fonts at the specified path for use by
the bundled app. The fonts are activated only for the bundled app and
not for the system as a whole. The path itself should be specified as
a relative directory of the bundle’s Resources directory. For example,
if a directory of fonts was at the path
/Applications/MyApp.app/Contents/Resources/Stuff/MyFonts/, you should
specify the string Stuff/MyFonts/ for the value of this key.
macOS app Instructions:
Select your Xcode project in the project navigator
Select your
app target
Click the + button and to add a New Copy Files Phase
Select Resources for the destination
Under subpath specify the directory (e.g. Fonts) where your embedded fonts will be copied to within your application bundle's Resources directory.
Drag and drop the font files into the file list of the Copy Files build phase.
UIAppFonts is for iOS:
UIAppFonts (Array - iOS) Specifies any app-provided fonts that should
be made available through the normal mechanisms. Each item in the
array is a string containing the name of a font file (including
filename extension) that is located in the app’s bundle. The system
loads the specified fonts and makes them available for use by the app
when that app is run.
This key is supported in iOS 3.2 and later.
By directly setting the Application fonts resource path as my font file's name, I solved this problem by sheer luck.
For those whose font family is called something like My-Custom-Font-Family:
be aware that in code you should instantiate your custom font like this: NSFont(name: "MyCustomFontFamily-Bold", size: 20)
Spaces and "-" are ignored and font type is written after "-". I did not see this in any docs and spend a few hours trying to figure out wtf was wrong.
Also if you want to get list of all available fonts you can use this code
for font in NSFontManager.shared.availableFonts {
print(font)
}
First add the desired font you want to embed to your OSX app to your project:
Then click project > Info, then click the plus sign and add a new key "Application fonts resource path" and type the name of your fonts there creating an array of strings:
Now you can select custom font and the name of the font will show there, you still need to use the Font Book to make it available inside Xcode.
Try doing this from the interface builder, in the attributes inspector .
I have been trying to use some custom fonts within my WP7 app.
I followed the example as per http://www.jeffblankenburg.com/2010/10/24/31-days-of-windows-phone-day-24-embedding-fonts/ but in my project it simply will not pick up the custom font.
I have downloaded the sample project in this example and this one does work okay.
So, I have now used the same font used in there, and included in my project. I have ensured that the properties on the file are identical (Build Action = 'Content' and Copy To Output Directory = "Always")
I have used in my XAML in exactly the same way i.e.
FontFamily="Fonts/DigitalDream.ttf#Digital Dream" FontSize="24"
But to no avail. The custom font is just ignored. Is there something that I am missing, or a setting .. anything that would allow this to work in one project, but not another?
I have discovered that it will work if my XAML page is defined at the root level, i.e. immediately under the project, but in my case I have it in a 'Views' folder.
My understanding was that
FontFamily="/Fonts/DigitalDream.ttf#Digital Dream" FontSize="24"
should start at the root folder of the project?
thanks
Custom fonts can be a little tricky. I spent several hours once trying to embed an .otf font only to find that it just didn't seem to be doable.
You're on the right track, though. A .ttf font should be embedable. It's hard to get the right combination of paths and filenames to make the font work, though.
The easiest way I've found to handle some of these more difficult "magic string" type of situations in XAML is to let Expression Blend do the heavy lifting.
Here's a blog post about using Blend to embed fonts. The nice thing about using Blend is that it gives you some visual indications that things are set up correctly--i.e. you can see the font you want in the font selection menu. This is what finally clued me in on the .otf issue. I couldn't see the font in Blend.
And here's an example of a custom font that I've used in one of my apps:
<TextBlock Name="MyTextblock"
FontFamily="/MyAppName;component/fonts/Fonts.zip#Segoe UI Mono">
That's FontFamily URI is a pain to get right. I'm pretty sure that's your problem.
I was actually having the same problem. I followed everything to the dot and still I wasn't able to embed one particular font. So I created a new project and tried embedding the font there. Still no luck. I then tried embedding some other font. And lo and behold, as soon as I typed the FontFamily attribute, I could see that it had embedded. This was particularly odd since I didn't have to change any properties of the font.
I opened the project in Blend and clicked on the font property box to the right. It took a few seconds and then it embedded the font I was having problems with in the first place.
It only happens on some web pages, and only for some text items. On splurb.com, it is happening all over:
http://scottelkin.com/images/splurb.jpg
Going to splurb.com it looks like the font used is in the family of Helvetica,Arial,sans-serif(lifted from the css). Those fonts should be included into windows. Perhaps they have been removed. Make sure those fonts are installed.