2

I have a nuget package for Xamarin Forms (ios and android) with which I want to distribute a custom font. I can't find from the nuget documentation how I can include the font in the nuget package so that it gets added to the iOS or Android project that consumes the package. Is this even possible? If so, how?

JohnB
  • 117
  • 1
  • 5
  • I don't know anything about Xamarin Forms. How does a font normally get included in a project, how does it integrate with msbuild? If it's just a `` item in the csproj, then you should look into NuGet's content and contentFiles. Otherwise, I need more info. – zivkan Jan 08 '19 at 14:43

1 Answers1

1

How do I distribute a font in a nuget package using the new csproj approach to create nuget packages?

To include the font in the nuget package and use it in the new csproj iOS or Android project, you should use contentFiles to include the font file in the .nuspec file, like:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
  <metadata>
    <id>MyFontPackage</id>
    <version>1.0.0</version>
    <authors>Tester</authors>
    <owners>Tester</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Package Description</description>
    <contentFiles>
      <files include="any/any/Fonts/xx.ttf" buildAction="content" flatten="true" copyToOutput="false"/>
    </contentFiles>
  </metadata>

  <files>
    <file src="contentFiles/any/any/Fonts/xx.ttf" target="contentFiles/any/any/Fonts" />
  </files>
</package>

Check the document NuGet ContentFiles Demystified and another thread for some more details.

Update:

If you are creating the nuget package directly from the new sdk style csproj, you can add following in your .csproj, then re-create the package, the font file will be added to the nuget package:

  <ItemGroup>
    <None Include="font\test.ttf" Pack="true"/>
  </ItemGroup>

This file added as content file:

enter image description here

Check the document NuGet pack and restore as MSBuild targets for some more details.

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • Thanks Leo, but I'm not using a nuspec, I'm creating the nuget package directly from the new sdk style csproj. I'll look at the links you've suggested. – JohnB Jan 09 '19 at 07:00
  • @JohnB, I have updated my answer for your comment, check if it helps you. – Leo Liu Jan 09 '19 at 07:33