Reputation: 251
Custom fonts don't load in Xamarin iOS. As described in the Xamarin documentation from 2013 (https://blog.xamarin.com/custom-fonts-in-ios/), I have done the following:
1) Import the font into Resources
2) Set Build Action to "BundleResource" (mistake in the Xamarin documentation)
3) Copy to output directory: "Always Copy"
4) Added the font name to the array "Fonts provided by application" in the Info.plist (making the font available in the storyboard, but not at runtime)
In FinishedLaunching() in the App Delegate, I iterate through all installed fonts:
foreach (var family in UIFont.FamilyNames)
{
System.Diagnostics.Debug.WriteLine($"{family}");
foreach (var names in UIFont.FontNamesForFamilyName(family))
{
System.Diagnostics.Debug.WriteLine($"{names}");
}
}
var fontName = UIFont.FromName("Karbon", 20.0f); // fontName is null
No custom fonts show up, what is the problem here?
Visual Studio Community 2017 for Mac (Preview) Version 7.1 Preview (7.1 build 1178) Runtime: Mono 5.2.0.179 (2017-04/4498dc4) (64-bit)
Xamarin.iOS Version: 10.12.0.5 (Visual Studio Community)
Upvotes: 1
Views: 3061
Reputation: 1276
For anyone coming here for Windows 10 and Visual Studio
xxx.ttf
in Resources folder xxx.ttf
> Properties > Build Action: BundleResourceinfo.plist
> Open With > Xml (Text) Editor<dict>
... ... ...
<key>UIAppFonts</key>
<array>
<string>xxx.ttf</string>
</array>
... ... ...
</dict>
xxx.ttf
file name (double click xxx.ttf
to see Typeface name)<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<On Platform="iOS" Value="Typeface_name" />
<On Platform="Android" Value="filename_name.ttf#Typeface_name" />
<On Platform="UWP" Value="Assets/Fonts/filename_name.ttf#Typeface_name" />
</OnPlatform>
</Label.FontFamily>
Upvotes: 3
Reputation: 251
Update: the Info.plist has to be updated (including the font type, e.g. ttf) and the Font’s properties should be set to not copy to output directory (Do not copy). It works after these changes.
Upvotes: 0