Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Unable to subclass UILabel without getting an error

$
0
0

Hi all,

I'm trying to subclass UILabel in my iOS app, as I have been informed this is the most reliable method of applying a custom font. I have added the font to my Info.plist file, however I am getting consistent errors when running the application with the subclass specified in IB. Here's the code:

OpenSansRegularLabel.cs

[Register("OpenSansRegularLabel")]
public partial class OpenSansRegularLabel : CustomFontLabel
{
    public OpenSansRegularLabel() : base("OpenSans-Regular", 10f) {}
}

CustomFontLabel.cs

public class CustomFontLabel : UILabel
{
    private readonly string _fontName;
    private readonly float _pointSize;

    public CustomFontLabel(string fontName, float pointSize)
    {
        _fontName = fontName;
        _pointSize = pointSize;
    }

    public override void AwakeFromNib() 
    {
        base.AwakeFromNib();
        Font = UIFont.FromName(_fontName, _pointSize);
    }
}

In my XIB file in XCode I have specified the custom class as OpenSansRegularLabel on my UILabel. As stated though this gives me an error in AppDelegate.cs every time:

// class-level declarations
UIWindow window;
UIViewController viewController;

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    window = new UIWindow(UIScreen.MainScreen.Bounds);

    viewController = new UserGuideMainScreen();
    window.RootViewController = viewController;
    window.MakeKeyAndVisible(); // Error thrown here: System.NullReferenceException

    return true;
}

When I use the standard UILabel class for the control in IB the app launches as normal.

This seems like such a simple problem to solve, I feel like I'm missing something obvious. Thanks for any help.


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>