Xamarin iOS Using a Custom Font

Christopher77Christopher77 AUMember ✭✭
edited April 2015 in Xamarin.iOS

I have followed this tutorial step by step:
http://blog.xamarin.com/custom-fonts-in-ios/

I have placed the Font file in my resources folder and changed Copy to Output directory as always copy.
And I have added the file to Info.plist

And I am using the following code:

cell.TextLabel.Font = UIFont.FromName("Gotham", 16f);

I have uploaded the project here with the line above commented out, would someone be able to take a look and help me out.
It would really be appreciated, I don't see what I am doing wrong and I have read through other threads with people that have had issues.

http://turboverse.com/app.source.zip

I have checked the name of the font and it is "Gotham" or am I meant to use the PostScript name? I also tried that which is "Gotham-Medium" and UIFont.FromName still returned null.

Please help!

Posts

  • JimBennettJimBennett GBXamarin Team, Insider, University, Developer Group Leader ✭✭✭✭

    I've just been following the same post and had the same problem. By changing the Build Action to BundleResource and doing a rebuild I was able to get it to work.

    The name to use is the name of the file - so if it's Gotham.ttf then use Gotham.

  • Christopher77Christopher77 AUMember ✭✭

    I have tried that and it still wont work and I have also tried using both an otf and ttf file.

  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    P.S. The documentation neglects this important step, so maybe the Xamarin Team can update it to give more detail on the proper way to define a custom font in the Info.plist file.

  • Christopher77Christopher77 AUMember ✭✭
    edited April 2015

    Thank you so much @rmacias! This was indeed the problem, I needed to supply an array of strings in Info.plist.

  • JayFrenchJayFrench USMember

    These instructions look like they'd work for Xamarin Studio, but when I edit my Info.plist file in Visual Studio, I don't have the option of adding fonts. The blog post says to "select ‘Source’ at the bottom of the view", but in Visual Studio I don't see any tabs like the screenshots show for Xamarin Studio. How can I get this to work with Visual Studio?

  • RussellCollinghamRussellCollingham GBUniversity ✭✭

    @JayFrench add a section to the info.plist file using a text editor eg

    <key>UIAppFonts</key>
        <array>
            <string>Fonts/impact.ttf</string>
            <string>Fonts/Raleway.ttf</string>
            <string>Fonts/OpenSans.ttf</string>
        </array>
    
  • @RussellCollingham said:
    @JayFrench add a section to the info.plist file using a text editor eg

    <key>UIAppFonts</key>
      <array>
          <string>Fonts/impact.ttf</string>
          <string>Fonts/Raleway.ttf</string>
          <string>Fonts/OpenSans.ttf</string>
      </array>
    

    Hi Russell, I'm also using VS and copy and pasted the text like you said into info.plist. When I look at the fonts dropdown in the designer, my new three fonts are there in a section above all the other fonts, separated by a divider. But when I select one, there is no subtype - ie regular, semibold, italic - to select in the second dropdown below it, its blank. All the system fonts have options in the second dropdown. And even if I just try select the first dropdown as my font and tab off, it doesn't update the font and when I go back, its gone back to the previous font.

    Do you know why this might be?

  • RussellCollinghamRussellCollingham GBUniversity ✭✭

    Sorry @jameskerslake I don't use the designer so I'm not sure.

  • JVFLAJVFLA USMember ✭✭

    First off they have removed that tutorial page. Second, the only examples I can find of using a custom font requires you to programmatically set the font ON EVERY UI ITEM! That's a bit ridiculous. In a great many cases what the developer wants is a default font setting.

    Is there no simple way to specify a default font for the entire app to use whenever no font is specified?

  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    Is there no simple way to specify a default font for the entire app to use whenever no font is specified?

    No, til this day (that I'm aware of), Apple still hasn't provided an API to do this. The closest thing you can do is use the Appearance API to set a custom font. So, to set a custom font for all UILabels, you can do something this in your AppDelegate:

    UILabel.Appearance.Font = UIFont.FromName("MyCustomFont", 12.0f);

    That will make your default font to "MyCustomFont" for all UILabels. The downside is not every UIView adheres to the Appearance API, so you're probably stuck setting the font for those controls.

  • JVFLAJVFLA USMember ✭✭

    Another indication that mobile APIs really aren't very mature yet, but good information to know. Thanks for the reply!

  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    To make it less painful, what I typically do is create an extension method for the UIViews I want the custom font or setting common custom colors, etc.

    So I'll have something like:

    public static class UIButtonExtensions
    {
        public static void Decorate(this UIButton button)
        {
            button.SetBackgroundImage(MyImages.ButtonSelectedBG, UIControlState.Normal);
            button.SetBackgroundImage(MyImages.ButtonUnSelectedBG, UIControlState.Highlighted);
    
            button.Font = MyFonts.MyCustomFont;
            button.SetTitleColor(MyColors.ButtonSelectedFontColor, UIControlState.Normal);
            button.SetTitleColor (MyColors.ButtonDisabledFontColor, UIControlState.Disabled);
            button.SetTitleColor(MyColors.ButtonUnSelectedFontColor, UIControlState.Highlighted);
        }
    }
    

    Then in your ViewDidLoad() method, you can do something

    myButton.Decorate();

    Still not ideal, but makes it less painful to work with and flexible enough so you only have to change it in one place rather than in a 1000 places.

  • igorkr_10igorkr_10 Member ✭✭✭

    Please help me in this post. I have one font family (FontAwesome) with two fonts (Solid and Regular). I can't get Solid font, only Regular.

  • @rmacias is it possible to create a an extension class in that can be applied to several different UIKit elements?

    This is what I had in mind:

    public static class UIGeneralExtensions 
    {
        public static void setFont(this UIGeneralItem element)
        {
            element.Font = UIFont.FromName("font-name",15);
        }
    }
    
    
    UILabel label = new UILabel();
    UIButton button = new UIButton();
    UITextView text = new UITextView();
    
    label.setFont();
    button.setFont();
    text.setFont();
    
    
    

    Is this not possible? Is there a common parent class that i would need to add this extension to in order to make this work?

    Would it be better to create a template class/function?

    public static class CustomStyling
    {
        public static void setFont<T>(T uiElement)
        {
            uiElement.Font = UIFont.FromName("font-name",15);
        }
    }
    
    UILabel label = new UILabel();
    UIButton button = new UIButton();
    UITextView text = new UITextView();
    
    CustomStyling.setFont(label);
    CustomStyling.setFont(button);
    CustomStyling.setFont(textView);
    
Sign In or Register to comment.