I need To Set Font from .ttf File

2019-02-26 07:35发布

问题:

I'm Trying to set font to my system and use it in my application it works fine for android but it does not apply for IOS - First I Added .ttf File to Resources in IOS Project

Then Added This Font to Entitlements.plist

For Android, I Added to Assets Folder

Then App File in the Main Application I added this Code in dictionary Resources

 <Style x:Key="LableStyle" TargetType="Label" >
        <Setter Property="Label.FontFamily">
            <Setter.Value>
                <OnPlatform x:TypeArguments="x:String">
                    <OnPlatform.Android>
                        DroidSansArabic.ttf#DroidSansArabic
                            </OnPlatform.Android>
                    <OnPlatform.iOS>
                        DroidSansArabic
                    </OnPlatform.iOS>
                </OnPlatform>
            </Setter.Value>
        </Setter>
    </Style>

And for The Label I Added the Style like this

<Label HorizontalOptions="StartAndExpand" 
   Margin="20,5,0,0"
   x:Name="lblForgotPassword"
   Text="Forgot Password"
   Style="{StaticResource LableStyle}">
</Label>

But it works fine in Android but not working in IOS Why ?

回答1:

I would assume your font family name is incorrect on iOS. The macOS/iOS font names are not the filename sans extension, the names are defined within the ttf itself.

The DroidSansArabic.ttf on my macOS and the one that I have used on iOS uses the family name of B Tabassom

So the usage of it in Forms would be:

label.FontFamily = Device.OnPlatform(
    iOS: "B Tabassom",
    Android: "DroidSansArabic.ttf#DroidSansArabic",
    WinPhone: null
);

As there are a number of DroidSansArabic.ttf floating around, verify what your font family name is by opening the ttf in macOS's Font Book and looking at the title bar:

If your macOS has it already installed, look in the name column:

Update: You also the UIAppFonts key in the wrong .plist, it goes in the Info.plist:

<key>UIAppFonts</key>
        <array>
                <string>DroidSansArabic.ttf</string>
        </array>
</dict>