How to set font name and font family to UILabel?

igorkr_10igorkr_10 Member ✭✭✭

Hello.
I'm using custom renderer for Label to show FontAwesome icons. I want to set font name to label.
FontAwesomeLabel element = this.Element as FontAwesomeLabel;
var font = UIFont.FromName(element.FontFamily, (nfloat)element.FontSize);
Debug.WriteLine(font.FamilyName); //shows: Font Awesome 5 Free
Debug.WriteLine(font.Name); //shows: Font Awesome 5 Free Regular
this.Control.Font = font;
Properties font.FamilyName and font.Name are readonly. Hot can I set family and name to UILabel? Or maybe I need to use something like Attributes?
For example:
UIFontAttributes uIFontAttributes = new UIFontAttributes() { Family = element.FontFamily, Name = element.FontName };
But how can I set this attributes to label?
Thanks for any help.

Best Answer

  • igorkr_10igorkr_10 ✭✭✭
    Accepted Answer

    Thanks for helping. I had solved the problem.
    For first we don't need any Renderers. This article has helped me.
    For second if you need Solid for iOS just use FontAttributes=Bold for Label.
    Example of using FontAwesomeSolid:

    <Label TextColor="#00ceea" FontSize="12" Text="&#xf073;" FontAttributes="Bold">
                        <Label.FontFamily>
                            <OnPlatform x:TypeArguments="x:String" Android="Font Awesome 5 Free-Solid-900.otf#Font Awesome 5 Free Solid" iOS="Font Awesome 5 Free"/>
                        </Label.FontFamily>
    </Label>
    

    For Regular on iOS just don't use FontAttributes

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    Since font name is a kind of specific font which belongs to font family name , you just need
    need to set Font name in following scenario .

    this.Control.Font = UIFont.FromName(font.Name, fontsize);

    You can find a series of Font name and which family name they belong to here :http://iosfonts.com/

  • igorkr_10igorkr_10 Member ✭✭✭

    Thanks for response.
    I did it by this way, but have 1 problem. I add FontAwesomeRegular and FontAwesomeSolid to project. When I set "Font Awesome 5 Free" to font it automatically sets FontAwesome5FreeRegular. As a result in variable font property Family will be "Font Awesome 5 Free" and Name="FontAwesome5FreeRegular". But I don't need always Regular, I want to set Solid too.

    How to be?

  • ColeXColeX Member, Xamarin Team Xamurai

    I think you should specify those font value with the keys Fonts provided by application in info.plist.

    Refer https://forums.xamarin.com/discussion/comment/114159/#Comment_114159

  • igorkr_10igorkr_10 Member ✭✭✭
    edited September 2018

    Is it same? I have already did it. I'm using Visual Studio on Windows

  • ColeXColeX Member, Xamarin Team Xamurai

    What about using this.Control.Font = UIFont.FromName("Font Awesome 5 Free Solid", fontsize); ? you should set the specific name.

  • igorkr_10igorkr_10 Member ✭✭✭
    edited September 2018

    @ColeX said:
    What about using this.Control.Font = UIFont.FromName("Font Awesome 5 Free Solid", fontsize); ? you should set the specific name.

    It was first thing I made. In this case impossible to find this font. UIFont.FromName("Font Awesome 5 Free Solid", fontsize) will be null.
    I was able to see property UIFont.FamilyNames and it returns "Font Awesome 5 Free" in array.

  • ColeXColeX Member, Xamarin Team Xamurai

    Double click .otf file and check if the font name is set correctly.

  • igorkr_10igorkr_10 Member ✭✭✭
    edited September 2018

    I was checked it right now with "Font Awesome 5 Free Solid" and it returns null. I think ios gets first font for this family. Maybe you have working example with multiple fonts for one family?
    I was also edited name in Info.plist and filename but the result is same.

  • igorkr_10igorkr_10 Member ✭✭✭
    Accepted Answer

    Thanks for helping. I had solved the problem.
    For first we don't need any Renderers. This article has helped me.
    For second if you need Solid for iOS just use FontAttributes=Bold for Label.
    Example of using FontAwesomeSolid:

    <Label TextColor="#00ceea" FontSize="12" Text="&#xf073;" FontAttributes="Bold">
                        <Label.FontFamily>
                            <OnPlatform x:TypeArguments="x:String" Android="Font Awesome 5 Free-Solid-900.otf#Font Awesome 5 Free Solid" iOS="Font Awesome 5 Free"/>
                        </Label.FontFamily>
    </Label>
    

    For Regular on iOS just don't use FontAttributes

Sign In or Register to comment.