|
//XForms
public class CustomEditor : Editor
{
public static readonly BindableProperty AllowMultilineInputProperty =
BindableProperty.Create(nameof(AllowMultilineInput), typeof(bool), typeof(CustomEditor), true, BindingMode.TwoWay, null, null);
public bool AllowMultilineInput
{
get { return (bool)GetValue(AllowMultilineInputProperty); }
set { this.SetValue(AllowMultilineInputProperty, value); }
}
public CustomEditor()
{
}
}
public class CustomEditorRenderer : EditorRenderer
{
public CustomEditorRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Editor> e)
{
base.OnElementChanged(e);
if (this.Control != null)
{
this.Control.SetPadding(0, 0, 0, (int)(this.Control.PaddingBottom + (2 * Resources.DisplayMetrics.Density)));
this.Control.TextSize = 14;
this.Control.SetBackgroundColor(NativeAndroid.Graphics.Color.Transparent);
if (!(this.Element as CustomEditor).AllowMultilineInput)
{
this.EditText.ImeOptions = ImeAction.Send;
this.EditText.SetSingleLine(true);
this.Control.EditorAction += Control_EditorAction;
}
else
{
this.EditText.ImeOptions = ImeAction.ImeNull;
this.EditText.SetSingleLine(false);
this.Control.SetMaxLines(6);
this.Control.EditorAction -= Control_EditorAction;
}
}
}
private void Control_EditorAction(object sender, TextView.EditorActionEventArgs e)
{
this.Element.Unfocus();
}
} |
|
public class CustomEditorRenderer : EditorRenderer
{
private bool doneButtonPressed = false;
private UIBarButtonItem doneButton;
protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
{
base.OnElementChanged(e);
this.Control.TranslatesAutoresizingMaskIntoConstraints = true;
if (this.Element != null)
{
if (!(this.Element as CustomEditor).AllowMultilineInput)
{
this.Control.ReturnKeyType = UIReturnKeyType.Send;
this.Control.ShouldChangeText = delegate (UITextView textView, Foundation.NSRange range, string text)
{
if (text == "\n")
{
this.Element.Unfocus();
return false;
}
else
{
return true;
}
};
}
else
{
this.Control.ReturnKeyType = UIReturnKeyType.Default;
}
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e?.PropertyName == "Text")
{
var lineCount = this.Control.ContentSize.Height / this.Control.Font.LineHeight;
if (this.Element != null)
{
if (!(this.Element as CustomEditor).AllowMultilineInput)
{
this.Control.ScrollEnabled = false;
this.TextView.TextContainer.MaximumNumberOfLines = 1;
this.Element.AutoSize = EditorAutoSizeOption.Disabled;
}
else if (lineCount > 6)
{
this.Control.ScrollEnabled = true;
this.Element.AutoSize = EditorAutoSizeOption.Disabled;
}
else
{
this.Control.ScrollEnabled = false;
this.Element.AutoSize = EditorAutoSizeOption.TextChanges;
}
}
}
}
}
|