Using a custom font.

xanadontxanadont USMember

I found this blog post but was unable to get it to work:

http://blog.xamarin.com/custom-fonts-in-ios/

I'm attempting to use the font at http://netdna.bootstrapcdn.com/font-awesome/3.2.1/font/fontawesome-webfont.ttf?v=3.2.1 - see http://fortawesome.github.io/Font-Awesome/

One thing that seemed fishy to me in the blog post is the screen cap is showing a build action of "Compile". Shouldn't it be an action that's more resource-friendly. Regardless, I couldn't get it to work with other build actions. I'm getting the "value is null" exception that the blog post mentions happens when iOS can't find the font.

Thanks for any help!

Posts

  • MikeCodesDotNetMikeCodesDotNet GBXamarin Team Xamurai

    Hi,

    I'm the guy who wrote the blog post and it seems I've put the wrong image up!

    To get the custom font working, you will need to set the fonts build action to be 'Bundle Resource'.

    Let me know if this doesn't work as it might be useful if you could send me a copy of your project.

    All the best,

    Mike

  • xanadontxanadont USMember

    Yep, didn't help. The project is attached.

    Thanks!

  • xanadontxanadont USMember

    Hello. Any updates on this?

    Thanks.

  • MikeCodesDotNetMikeCodesDotNet GBXamarin Team Xamurai

    Hi,

    Sorry for the delay in getting back to you.

    I've had a look at the project and you are bundling the font correctly but the issue is with the following line:

    myLabel.Font = UIFont.FromName ("fontawesome-webfont", 20);

    If you change it to the following:

    myLabel.Font = UIFont.FromName ("fontawesome", 20);

    Then the problem should be resolved. I copied and pasted the sample text from Apple's Fontbook which seemed to work very well.

    I hope this helps.

    All the best,

    Mike

  • jbarker4682jbarker4682 USMember ✭✭

    Hello... I am also trying to use a custom font... ("HandelGothic.ttf"), however I am not using a label, but instead I am attempting to draw the text on a custom control, per the "Graphics and Animation" tutorial 2.4.1. Adding Text to the Image.

    When I run the app in the iPad Simulator (6.1) the text displays in the correct font and everything appears to work correctly. However, when I deploy the app to my iPad, the text does not display at all.

    I installed the font on my system via FontBook, and then also added it to my app's resources folder and set its properties per the above referenced blog-post Using custom Fonts in Xamarin.iOS :

    "Build action" = "BundleResource"
    "Copy to output directory" = "Always".

    Here is my relevant code:

    `

    using(CGContext g = UIGraphics.GetCurrentContext ()) {
    
        //get x,y position for where I want text to display
        float xOrigin = this.Center.X - 25.0f;
        float yOrigin = this.Center.Y + (FONT_SIZE * 4);
    
        //invert y-axis so text appears right-side-up
        g.ScaleCTM(1, -1);
    
        //move origin to desired position
        g.TranslateCTM(xOrigin, -yOrigin);
    
        //set text properties
        g.SetLineWidth(1.0f);
        g.SetStrokeColor(UIColor.LightGray.CGColor);
        g.SetFillColor(UIColor.Gray.CGColor);
        g.SetTextDrawingMode(CGTextDrawingMode.FillStroke);
    
        //select custom font
        g.SelectFont("HandelGothic", FONT_SIZE, CGTextEncoding.FontSpecific);
    
        //display text
        g.ShowText(String.Format("{0:0.00}", CurrentDegrees));
    
        //reset origin and y-axis back to original settings
        g.TranslateCTM(xOrigin * -1, yOrigin);
        g.ScaleCTM(1, -1);
    
        //additional drawing logic for rest of control...
    }
    

    `

    Any help would be greatly appreciated...

    Thanks!

  • DivinerDiviner HKMember

    Try to change the font name to "Helvetica-Bold", it is ok for me.

  • PooranPrasadRajannaPooranPrasadRajanna INMember ✭✭

    Is there a way I can define custom fonts globally in the application so that user sees same font in iOS and Android? I am trying to do it in Xamarin 3 forms.

  • IsaacZais.1920IsaacZais.1920 USMember

    Is there a way to get the custom font to show up in the Xamarin Studio iOS Designer font selector? Or, even better, is there a way to set my font as a "global" font so that all text based views use my specified font?

  • D-BotD-Bot CAUniversity ✭✭

    I just finished going through the walkthrough and did everything correctly but still to no avail. I keep getting the ArgumentNullException. Any ideas? See my related question here

  • andreacurtiandreacurti ITUniversity ✭✭

    I'had similar issue.
    I'd 2 solution, i use same font, i did same things.
    in the trial sln it works fine, in my real sln it doesn't work.
    I tried to change the font name, but still doesn't work.
    Any help please?

  • TobyKTobyK GBMember ✭✭✭

    I had this problem also - using font on a sim no problem, but when running on a physical device. It appears that the naming convention is case sensitive on the physicals.

Sign In or Register to comment.