how to use custom font in MAUI ios?

mc 5,426 Reputation points
2024-12-17T02:56:34.74+00:00

I am using .net MAUI and i want to use CGContext in platform ios

I want to showText but it is not showing right with unicode text.

but I can not load font using bytes but only can use context.SelectFont('Arial',15,CGTextEncoding.MacRoman)

but how to use OpenSansRegular here or other ttf fonts?

the context is getting by var context=UIGraphics.GetCurrentContext();

Developer technologies .NET .NET MAUI
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,436 Reputation points Microsoft External Staff
    2024-12-17T08:10:59.01+00:00

    Hello,

    but how to use OpenSansRegular here or other ttf fonts?

    Please follow the steps below:

    1. Drag and copy the ttf file into the Resources folder in your .NET iOS project, make sure the Build Action is BundleResource.
    2. Add the following key in Info.plist
         <key>UIAppFonts</key>
         <arra
          <string>OpenSans-Regular.ttf</string>
         </array>
      
    3. Consume the font like the following
      context.SelectFont('OpenSans-Regular',15,CGTextEncoding.MacRoman)
    

    For more details, you could refer to Using custom Fonts in Xamarin.iOS - Xamarin Blog

    (This doc is old and is about Xamarin.iOS, however, it applies to MAUI. I understand you are using .net-ios, you could have a look.)

    but CGContext.ShowTextAtPoint to show unicode will be not displayed right which means it can not show unicode right?

    Yes, you are right. I tested with the unicode character \uf30c, it can not be shown with the ionicons.ttf.

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.