1 ) I am going to assume the exception stack you are seeing is not showing the actual underlaying error as it is coming from the Xamarin.iOS
generated UILabel.g.cs
file (that is a auto-generated wrapper within https://github.com/xamarin/xamarin-macios) and I personally have had a few issues tracing certain low-level OS exceptions/throws and thus the shown exception is a little (a lot?) misleading.
2) Again I am going to assume since you said you are "loading fonts from files" that these fonts are not registered. Assuming you are loading these fonts via a CGFont.CreateFromProvider
from files in your app bundle (or dynimically downloaded) and since they do not exist in the info.plist under the UIAppFonts
key, they are not auto-registered to iOS. You need to do that step yourself:
NSError error;
if (!CTFontManager.RegisterGraphicsFont(customFont, out error))
{
// error occurred, checkout the contents of the NSError var
}
After registering the CGFont with the iOS font manager (CTFontManager
) you can actually use them within UIKit
, otherwise internally there is an exception within iOS code... Would need to write some ObjC/Swift to check it out in Xcode to see what the actual error being thrown is...
Are your fonts registered or not:
Somewhere before the exception but after your CreateFromProvider
, you can do the following:
foreach (var familyNames in UIFont.FamilyNames.OrderBy(c => c).ToList())
{
D.WriteLine(" * " + familyNames);
foreach (var familyName in UIFont.FontNamesForFamilyName(familyNames).OrderBy(c => c).ToList())
{
D.WriteLine(" *-- " + familyName);
}
}
Note: D
is just using D = System.Diagnostics.Debug;
I would bet that your font is not in the list, but after a call to CTFontManager.RegisterGraphicsFont
it will be in that list and valid to be consumed by UIKit
.
Update:
Interesting note in that using a Swift-based app and not registering the font does not cause a app crash. The font is not shown of course, but referencing it via UIFont(name: "XXXXX", size: 35)
is just a silent failure.
So I'd just recommend some defensive try{} catch{}
around any use of your custom font to be safe.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…