[Mac] Fix NRE on setting non-existing FontFamily (#7636) fixes #7582

* Update FontExtensions.cs

* Added issue to gallery
This commit is contained in:
Gerald Versluis 2019-09-24 01:04:24 +02:00 коммит произвёл Rui Marinho
Родитель e44bfb8d01
Коммит 463d188fef
3 изменённых файлов: 33 добавлений и 5 удалений

Просмотреть файл

@ -0,0 +1,24 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 7582, "Null Reference Exception thrown when set FontFamily for label in Xamarin Forms macOS", PlatformAffected.macOS)]
public class Issue7582 : TestContentPage
{
protected override void Init()
{
var label = new Label
{
Text = "This is a label with a non-existent FontFamily, If you're seeing this label the test succeeded.",
FontFamily = "THISISNOTAFONT",
FontAttributes = FontAttributes.Bold,
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center
};
Content = label;
}
}
}

Просмотреть файл

@ -1023,6 +1023,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue6894.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue6929.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\ApiLabel.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue7582.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Bugzilla22229.xaml">

Просмотреть файл

@ -38,7 +38,10 @@ namespace Xamarin.Forms.Platform.MacOS
//if we didn't found a Font or Descriptor for the FontFamily use the default one
if (font == null)
{
font = defaultFont;
descriptor = defaultFont.FontDescriptor;
}
if (descriptor == null)
descriptor = defaultFont.FontDescriptor;
@ -47,16 +50,16 @@ namespace Xamarin.Forms.Platform.MacOS
{
var traits = (NSFontSymbolicTraits)0;
if (bold)
traits = traits | NSFontSymbolicTraits.BoldTrait;
traits |= NSFontSymbolicTraits.BoldTrait;
if (italic)
traits = traits | NSFontSymbolicTraits.ItalicTrait;
traits |= NSFontSymbolicTraits.ItalicTrait;
var fontDescriptoWithTraits = descriptor.FontDescriptorWithSymbolicTraits(traits);
var fontDescriptorWithTraits = descriptor.FontDescriptorWithSymbolicTraits(traits);
font = NSFont.FromDescription(fontDescriptoWithTraits, size);
font = NSFont.FromDescription(fontDescriptorWithTraits, size);
}
return font.ScreenFontWithRenderingMode(NSFontRenderingMode.AntialiasedIntegerAdvancements);
}
}
}
}