Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (71.8m points)

xamarin - I need To Set Font from .ttf File

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

enter image description here

Then Added This Font to Entitlements.plist

enter image description here

For Android, I Added to Assets Folder

enter image description here

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

enter image description here

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

enter image description here

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...