Skip to content

Commit dcacab0

Browse files
🐛 Fix system font bug
Fixes #70
1 parent 8340d31 commit dcacab0

File tree

6 files changed

+51
-9
lines changed

6 files changed

+51
-9
lines changed

src/GMImagePicker/FontParser.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Linq;
3+
using UIKit;
4+
5+
namespace GMImagePicker
6+
{
7+
internal static class FontParser
8+
{
9+
public static UIFont GetFont(string family, nfloat size)
10+
{
11+
UIFont result;
12+
13+
if (family.StartsWith(".SFUI", System.StringComparison.InvariantCultureIgnoreCase))
14+
{
15+
var fontWeight = family.Split('-').LastOrDefault();
16+
17+
if (!string.IsNullOrWhiteSpace(fontWeight) && System.Enum.TryParse<UIFontWeight>(fontWeight, true, out var uIFontWeight))
18+
{
19+
result = UIFont.SystemFontOfSize(size, uIFontWeight);
20+
return result;
21+
}
22+
23+
result = UIFont.SystemFontOfSize(size, UIFontWeight.Regular);
24+
return result;
25+
}
26+
else
27+
{
28+
result = GetFont(family, size);
29+
if (result != null)
30+
return result;
31+
}
32+
33+
// fallback
34+
return UIFont.SystemFontOfSize(size);
35+
}
36+
}
37+
}

src/GMImagePicker/GMAlbumsViewController.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Collections.Generic;
1717
using CoreFoundation;
1818
using System.Diagnostics;
19+
using CoreText;
1920

2021
namespace GMImagePicker
2122
{
@@ -121,7 +122,7 @@ public override void ViewDidLoad()
121122
// Buttons
122123
var barButtonItemAttributes = new UITextAttributes
123124
{
124-
Font = UIFont.FromName(_picker.PickerFontName, _picker.PickerFontHeaderSize)
125+
Font = FontParser.GetFont(_picker.PickerFontName, _picker.PickerFontHeaderSize)
125126
};
126127

127128
var cancelTitle = _picker.CustomCancelButtonTitle ?? "picker.navigation.cancel-button".Translate(defaultValue: "Cancel");
@@ -324,7 +325,7 @@ public override UITableViewCell GetCell(UITableView tableView, NSIndexPath index
324325
cell.Tag = currentTag;
325326

326327
// Set the label
327-
cell.TextLabel.Font = UIFont.FromName(_picker.PickerFontName, _picker.PickerFontHeaderSize);
328+
cell.TextLabel.Font = FontParser.GetFont(_picker.PickerFontName, _picker.PickerFontHeaderSize);
328329
cell.TextLabel.Text = _collectionsFetchResultsTitles[indexPath.Section][indexPath.Row];
329330
cell.TextLabel.TextColor = _picker.PickerTextColor;
330331

@@ -334,7 +335,7 @@ public override UITableViewCell GetCell(UITableView tableView, NSIndexPath index
334335
// Display the number of assets
335336
if (_picker.DisplayAlbumsNumberOfAssets)
336337
{
337-
cell.DetailTextLabel.Font = UIFont.FromName(_picker.PickerFontName, _picker.PickerFontNormalSize);
338+
cell.DetailTextLabel.Font = FontParser.GetFont(_picker.PickerFontName, _picker.PickerFontNormalSize);
338339
// Just use the number of assets. Album app does this:
339340
cell.DetailTextLabel.Text = string.Format("{0:0}", assetsFetchResult.Count);
340341
cell.DetailTextLabel.TextColor = _picker.PickerTextColor;
@@ -456,7 +457,7 @@ public override void WillDisplayHeaderView(UITableView tableView, UIView headerV
456457
header.BackgroundView.BackgroundColor = _picker.PickerBackgroundColor;
457458

458459
// Default is a bold font, but keep this styled as a normal font
459-
header.TextLabel.Font = UIFont.FromName(_picker.PickerFontName, _picker.PickerFontNormalSize);
460+
header.TextLabel.Font = FontParser.GetFont(_picker.PickerFontName, _picker.PickerFontNormalSize);
460461
header.TextLabel.TextColor = _picker.PickerTextColor;
461462
}
462463

src/GMImagePicker/GMGridViewController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ private void SetupButtons ()
163163
}
164164
if (_picker.UseCustomFontForNavigationBar) {
165165
var barButtonItemAttributes = new UITextAttributes {
166-
Font = UIFont.FromName(_picker.PickerFontName, _picker.PickerFontHeaderSize)
166+
Font = FontParser.GetFont(_picker.PickerFontName, _picker.PickerFontHeaderSize)
167167
};
168168
NavigationItem.RightBarButtonItem.SetTitleTextAttributes (barButtonItemAttributes, UIControlState.Normal);
169169
NavigationItem.RightBarButtonItem.SetTitleTextAttributes (barButtonItemAttributes, UIControlState.Highlighted);

src/GMImagePicker/GMImagePicker.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
<PackageLicenseUrl>https://raw.githubusercontent.com/roycornelissen/GMImagePicker.Xamarin/master/LICENSE</PackageLicenseUrl>
2222
<Owners>Roy Cornelissen</Owners>
2323
<PackageProjectUrl>https://github.com/roycornelissen/GMImagePicker.Xamarin</PackageProjectUrl>
24-
<PackageReleaseNotes>[2.3.5]
24+
<PackageReleaseNotes>[2.3.6]
25+
- #70 Fixes system fonts in iOS 13 reverting to TimesNewRoman.
26+
- #71 Fixes Picker auto scroll to bottom when selecting images. Thanks @dustintnguyen
27+
[2.3.5]
2528
- #65: Fixes the checkmark image not being visible on selected items. Thanks @sergbuk
2629
[2.3.4]
2730
- #58: Fixes for Norwegian translations. Thanks @jasonctoms
@@ -131,6 +134,7 @@
131134
<Compile Include="Events.cs" />
132135
<Compile Include="GMAlbumsViewController.cs" />
133136
<Compile Include="TranslationExtensions.cs" />
137+
<Compile Include="FontParser.cs" />
134138
</ItemGroup>
135139
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.iOS.CSharp.targets" />
136140
<ItemGroup>

src/GMImagePicker/GMImagePickerController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ public sealed override async void ViewWillAppear (bool animated)
704704
UIStringAttributes attributes;
705705
if (UseCustomFontForNavigationBar) {
706706
attributes = new UIStringAttributes { ForegroundColor = NavigationBarTextColor,
707-
Font = UIFont.FromName (PickerBoldFontName, PickerFontHeaderSize)
707+
Font = FontParser.GetFont (PickerBoldFontName, PickerFontHeaderSize)
708708
};
709709
} else {
710710
attributes = new UIStringAttributes { ForegroundColor = NavigationBarTextColor };
@@ -885,7 +885,7 @@ private UITextAttributes ToolbarTitleTextAttributes
885885
get {
886886
return new UITextAttributes {
887887
TextColor = ToolbarTextColor,
888-
Font = UIFont.FromName (PickerFontName, PickerFontHeaderSize)
888+
Font = FontParser.GetFont (PickerFontName, PickerFontHeaderSize)
889889
};
890890
}
891891
}

src/GMPhotoPicker.Xamarin/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<key>LSRequiresIPhoneOS</key>
1414
<true/>
1515
<key>MinimumOSVersion</key>
16-
<string>13.2</string>
16+
<string>13.4</string>
1717
<key>UIDeviceFamily</key>
1818
<array>
1919
<integer>1</integer>

0 commit comments

Comments
 (0)