accessible font sizes

This commit is contained in:
Kyle Spearrin 2019-06-20 16:32:22 -04:00
parent dc91624597
commit a2960c45bc
7 changed files with 210 additions and 0 deletions

View file

@ -0,0 +1,40 @@
using System.ComponentModel;
using Bit.iOS.Renderers;
using Bit.iOS.Utilities;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(Button), typeof(CustomButtonRenderer))]
namespace Bit.iOS.Renderers
{
public class CustomButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if(Control != null && e.NewElement is Button)
{
UpdateFont();
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if(e.PropertyName == Button.FontProperty.PropertyName)
{
UpdateFont();
}
}
private void UpdateFont()
{
var pointSize = iOSHelpers.GetAccessibleFont<Button>(Element.FontSize);
if(pointSize != null)
{
Control.Font = UIFont.FromDescriptor(Element.Font.ToUIFont().FontDescriptor, pointSize.Value);
}
}
}
}

View file

@ -0,0 +1,21 @@
using Bit.iOS.Renderers;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(Editor), typeof(CustomEditorRenderer))]
namespace Bit.iOS.Renderers
{
public class CustomEditorRenderer : EditorRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
{
base.OnElementChanged(e);
if(e.NewElement is Editor)
{
var descriptor = UIFontDescriptor.PreferredBody;
Control.Font = UIFont.FromDescriptor(descriptor, descriptor.PointSize);
}
}
}
}

View file

@ -0,0 +1,49 @@
using System.ComponentModel;
using Bit.iOS.Renderers;
using Bit.iOS.Utilities;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(Entry), typeof(CustomEntryRenderer))]
namespace Bit.iOS.Renderers
{
public class CustomEntryRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if(Control != null && e.NewElement is Entry)
{
UpdateFontSize();
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if(e.PropertyName == Entry.FontAttributesProperty.PropertyName ||
e.PropertyName == Entry.FontFamilyProperty.PropertyName ||
e.PropertyName == Entry.FontSizeProperty.PropertyName)
{
UpdateFontSize();
}
}
private void UpdateFontSize()
{
var pointSize = iOSHelpers.GetAccessibleFont<Entry>(Element.FontSize);
if(pointSize != null)
{
if(!string.IsNullOrWhiteSpace(Element.FontFamily))
{
Control.Font = UIFont.FromName(Element.FontFamily, pointSize.Value);
}
else
{
Control.Font = UIFont.FromDescriptor(UIFontDescriptor.PreferredBody, pointSize.Value);
}
}
}
}
}

View file

@ -0,0 +1,42 @@
using System.ComponentModel;
using Bit.iOS.Renderers;
using Bit.iOS.Utilities;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(Label), typeof(CustomLabelRenderer))]
namespace Bit.iOS.Renderers
{
public class CustomLabelRenderer : LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if(Control != null && e.NewElement is Label)
{
UpdateFont();
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if(e.PropertyName == Label.FontProperty.PropertyName ||
e.PropertyName == Label.TextProperty.PropertyName ||
e.PropertyName == Label.FormattedTextProperty.PropertyName)
{
UpdateFont();
}
}
private void UpdateFont()
{
var pointSize = iOSHelpers.GetAccessibleFont<Label>(Element.FontSize);
if(pointSize != null)
{
Control.Font = UIFont.FromDescriptor(Element.Font.ToUIFont().FontDescriptor, pointSize.Value);
}
}
}
}

View file

@ -0,0 +1,21 @@
using Bit.iOS.Renderers;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(Picker), typeof(CustomPickerRenderer))]
namespace Bit.iOS.Renderers
{
public class CustomPickerRenderer : PickerRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
if(e.NewElement is Picker)
{
var descriptor = UIFontDescriptor.PreferredBody;
Control.Font = UIFont.FromDescriptor(descriptor, descriptor.PointSize);
}
}
}
}

View file

@ -0,0 +1,31 @@
using UIKit;
using Xamarin.Forms;
namespace Bit.iOS.Utilities
{
public static class iOSHelpers
{
public static System.nfloat? GetAccessibleFont<T>(double size)
{
var pointSize = UIFontDescriptor.PreferredBody.PointSize;
if(size == Device.GetNamedSize(NamedSize.Large, typeof(T)))
{
pointSize *= 1.3f;
}
else if(size == Device.GetNamedSize(NamedSize.Small, typeof(T)))
{
pointSize *= .8f;
}
else if(size == Device.GetNamedSize(NamedSize.Micro, typeof(T)))
{
pointSize *= .6f;
}
else if(size != Device.GetNamedSize(NamedSize.Default, typeof(T)))
{
// not using dynamic font sizes, return
return null;
}
return pointSize;
}
}
}

View file

@ -113,11 +113,17 @@
<Compile Include="AppDelegate.cs" /> <Compile Include="AppDelegate.cs" />
<Compile Include="Migration\KeyChainStorageService.cs" /> <Compile Include="Migration\KeyChainStorageService.cs" />
<Compile Include="NFCReaderDelegate.cs" /> <Compile Include="NFCReaderDelegate.cs" />
<Compile Include="Renderers\CustomPickerRenderer.cs" />
<Compile Include="Renderers\CustomEntryRenderer.cs" />
<Compile Include="Renderers\CustomEditorRenderer.cs" />
<Compile Include="Renderers\CustomButtonRenderer.cs" />
<Compile Include="Renderers\CustomLabelRenderer.cs" />
<Compile Include="Renderers\CustomContentPageRenderer.cs" /> <Compile Include="Renderers\CustomContentPageRenderer.cs" />
<Compile Include="Renderers\HybridWebViewRenderer.cs" /> <Compile Include="Renderers\HybridWebViewRenderer.cs" />
<Compile Include="Services\DeviceActionService.cs" /> <Compile Include="Services\DeviceActionService.cs" />
<Compile Include="Services\iOSPushNotificationHandler.cs" /> <Compile Include="Services\iOSPushNotificationHandler.cs" />
<Compile Include="Services\iOSPushNotificationService.cs" /> <Compile Include="Services\iOSPushNotificationService.cs" />
<Compile Include="Utilities\iOSHelpers.cs" />
<None Include="Entitlements.plist" /> <None Include="Entitlements.plist" />
<None Include="Info.plist" /> <None Include="Info.plist" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />