1
0
mirror of https://github.com/bitwarden/mobile.git synced 2025-01-04 18:28:15 +01:00
bitwarden-mobile/src/iOS/Controls/ExtendedEntryRenderer.cs

76 lines
2.4 KiB
C#

using System;
using System.ComponentModel;
using Bit.App.Controls;
using Bit.iOS.Controls;
using CoreAnimation;
using CoreGraphics;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(ExtendedEntry), typeof(ExtendedEntryRenderer))]
namespace Bit.iOS.Controls
{
public class ExtendedEntryRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
var view = e.NewElement as ExtendedEntry;
if(view != null)
{
SetBorder(view);
SetMaxLength(view);
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
var view = (ExtendedEntry)Element;
if(e.PropertyName == ExtendedEntry.HasBorderProperty.PropertyName
|| e.PropertyName == ExtendedEntry.HasOnlyBottomBorderProperty.PropertyName
|| e.PropertyName == ExtendedEntry.BottomBorderColorProperty.PropertyName)
{
SetBorder(view);
}
}
private void SetBorder(ExtendedEntry view)
{
if(view.HasOnlyBottomBorder)
{
var borderLayer = new CALayer();
borderLayer.MasksToBounds = true;
borderLayer.Frame = new CGRect(0f, Frame.Height / 2, Frame.Width * 2, 1f);
borderLayer.BorderColor = view.BottomBorderColor.ToCGColor();
borderLayer.BorderWidth = 1f;
Control.Layer.AddSublayer(borderLayer);
Control.BorderStyle = UITextBorderStyle.None;
}
else if(view.HasBorder)
{
Control.BorderStyle = UITextBorderStyle.RoundedRect;
}
else
{
Control.BorderStyle = UITextBorderStyle.None;
}
}
private void SetMaxLength(ExtendedEntry view)
{
Control.ShouldChangeCharacters = (textField, range, replacementString) =>
{
var newLength = textField.Text.Length + replacementString.Length - range.Length;
return newLength <= view.MaxLength;
};
}
}
}