Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port Editor Placeholder text and color properties #573

Merged
merged 16 commits into from
Mar 24, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected override FormsEditText CreateNativeControl()

protected override EditText EditText => Control;

[PortHandler]
protected override void UpdatePlaceholderColor()
{
_hintColorSwitcher = _hintColorSwitcher ?? new TextColorSwitcher(EditText.HintTextColors, Element.UseLegacyColorManagement());
Expand Down Expand Up @@ -269,6 +270,7 @@ void UpdateText()

abstract protected void UpdateTextColor();

[PortHandler]
protected virtual void UpdatePlaceholderText()
{
if (EditText.Hint == Element.Placeholder)
Expand All @@ -277,6 +279,7 @@ protected virtual void UpdatePlaceholderText()
EditText.Hint = Element.Placeholder;
}

[PortHandler]
abstract protected void UpdatePlaceholderColor();

void OnKeyboardBackPressed(object sender, EventArgs eventArgs)
Expand Down
6 changes: 6 additions & 0 deletions src/Compatibility/Core/src/iOS/Renderers/EditorRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ protected internal override void UpdateFont()
_placeholderLabel.Font = Element.ToUIFont();
}

[PortHandler]
protected internal override void UpdatePlaceholderText()
{
_placeholderLabel.Text = Element.Placeholder;
Expand All @@ -84,6 +85,7 @@ protected internal override void UpdateCharacterSpacing()
_placeholderLabel.AttributedText = placeHolder;
}

[PortHandler]
protected internal override void UpdatePlaceholderColor()
{
Color placeholderColor = Element.PlaceholderColor;
Expand All @@ -93,6 +95,7 @@ protected internal override void UpdatePlaceholderColor()
_placeholderLabel.TextColor = placeholderColor.ToUIColor();
}

[PortHandler]
void CreatePlaceholderLabel()
{
if (Control == null)
Expand Down Expand Up @@ -342,7 +345,10 @@ protected internal virtual void UpdateText()
}
}

[PortHandler]
protected internal abstract void UpdatePlaceholderText();

[PortHandler]
protected internal abstract void UpdatePlaceholderColor();
protected internal abstract void UpdateCharacterSpacing();

Expand Down
1 change: 1 addition & 0 deletions src/Controls/samples/Controls.Sample/Pages/MainPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void SetupMauiLayout()
verticalStack.Add(new Label { Text = loremIpsum, MaxLines = 2, LineBreakMode = LineBreakMode.TailTruncation });
verticalStack.Add(new Label { Text = "This should have five times the line height!", LineHeight = 5 });

verticalStack.Add(new Editor { Placeholder = "This is an editor placeholder." } );
var paddingButton = new Button
{
Padding = new Thickness(40),
Expand Down
8 changes: 4 additions & 4 deletions src/Core/src/Core/IEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
/// <summary>
/// Represents a View used to accept multi-line input.
/// </summary>
public interface IEditor : IView, IText
public interface IEditor : IView, ITextInput
{
/// <summary>
/// Gets the maximum allowed length of input.
/// Gets or sets the placeholder text color.
/// </summary>
int MaxLength { get; }

Color PlaceholderColor { get; set; }
/// <summary>
/// Gets a value that controls whether text prediction and automatic text correction is on or off.
/// </summary>
Expand Down
21 changes: 20 additions & 1 deletion src/Core/src/Handlers/Editor/EditorHandler.Android.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System;
using Android.Content.Res;
using Android.Views;
using Android.Views.InputMethods;
using AndroidX.AppCompat.Widget;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace Microsoft.Maui.Handlers
{
public partial class EditorHandler : AbstractViewHandler<IEditor, AppCompatEditText>
{
static ColorStateList? DefaultPlaceholderTextColors { get; set; }

protected override AppCompatEditText CreateNativeView()
{
var editText = new AppCompatEditText(Context)
Expand All @@ -23,11 +26,27 @@ protected override AppCompatEditText CreateNativeView()
return editText;
}

protected override void SetupDefaults(AppCompatEditText nativeView)
{
base.SetupDefaults(nativeView);
DefaultPlaceholderTextColors = nativeView.HintTextColors;
}

public static void MapText(EditorHandler handler, IEditor editor)
{
handler.TypedNativeView?.UpdateText(editor);
}

public static void MapPlaceholder(EditorHandler handler, IEditor editor)
{
handler.TypedNativeView?.UpdatePlaceholder(editor);
}

public static void MapPlaceholderColor(EditorHandler handler, IEditor editor)
{
handler.TypedNativeView?.UpdatePlaceholderColor(editor, DefaultPlaceholderTextColors);
}

public static void MapCharacterSpacing(EditorHandler handler, IEditor editor)
{
handler.TypedNativeView?.UpdateCharacterSpacing(editor);
Expand Down
2 changes: 2 additions & 0 deletions src/Core/src/Handlers/Editor/EditorHandler.Standard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public partial class EditorHandler : AbstractViewHandler<IEditor, object>
protected override object CreateNativeView() => throw new NotImplementedException();

public static void MapText(IViewHandler handler, IEditor editor) { }
public static void MapPlaceholder(IViewHandler handler, IEditor editor) { }
public static void MapPlaceholderColor(IViewHandler handler, IEditor editor) { }
public static void MapCharacterSpacing(IViewHandler handler, IEditor editor) { }
public static void MapMaxLength(IViewHandler handler, IEditor editor) { }
public static void MapIsTextPredictionEnabled(EditorHandler handler, IEditor editor) { }
Expand Down
3 changes: 2 additions & 1 deletion src/Core/src/Handlers/Editor/EditorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ public partial class EditorHandler
public static PropertyMapper<IEditor, EditorHandler> EditorMapper = new PropertyMapper<IEditor, EditorHandler>(ViewHandler.ViewMapper)
{
[nameof(IEditor.Text)] = MapText,
[nameof(IEditor.Placeholder)] = MapPlaceholder,
[nameof(IEditor.PlaceholderColor)] = MapPlaceholderColor,
[nameof(IEditor.CharacterSpacing)] = MapCharacterSpacing,
[nameof(IEditor.MaxLength)] = MapMaxLength,
[nameof(IEditor.IsTextPredictionEnabled)] = MapIsTextPredictionEnabled,
Expand All @@ -13,7 +15,6 @@ public partial class EditorHandler

public EditorHandler() : base(EditorMapper)
{

}

public EditorHandler(PropertyMapper? mapper = null) : base(mapper ?? EditorMapper)
Expand Down
37 changes: 31 additions & 6 deletions src/Core/src/Handlers/Editor/EditorHandler.iOS.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
using CoreGraphics;
using CoreGraphics;
using Foundation;
using Microsoft.Maui.Platform.iOS;
using System;
using Microsoft.Extensions.DependencyInjection;
using UIKit;

namespace Microsoft.Maui.Handlers
{
public partial class EditorHandler : AbstractViewHandler<IEditor, UITextView>
public partial class EditorHandler : AbstractViewHandler<IEditor, MauiTextView>
{
static readonly int BaseHeight = 30;

protected override UITextView CreateNativeView()
static readonly UIColor DefaultPlaceholderColor = ColorExtensions.PlaceholderColor;

protected override MauiTextView CreateNativeView()
{
return new UITextView(CGRect.Empty);
return new MauiTextView(CGRect.Empty);
}

protected override void ConnectHandler(UITextView nativeView)
protected override void ConnectHandler(MauiTextView nativeView)
{
nativeView.Changed += OnChanged;
nativeView.ShouldChangeText += OnShouldChangeText;
}

protected override void DisconnectHandler(UITextView nativeView)
protected override void DisconnectHandler(MauiTextView nativeView)
{
nativeView.Changed -= OnChanged;
nativeView.ShouldChangeText -= OnShouldChangeText;
}

Expand All @@ -36,6 +41,16 @@ public static void MapText(EditorHandler handler, IEditor editor)
MapFormatting(handler, editor);
}

public static void MapPlaceholder(EditorHandler handler, IEditor editor)
{
handler.TypedNativeView?.UpdatePlaceholder(editor);
}

public static void MapPlaceholderColor(EditorHandler handler, IEditor editor)
{
handler.TypedNativeView?.UpdatePlaceholderColor(editor, DefaultPlaceholderColor);
}

public static void MapCharacterSpacing(EditorHandler handler, IEditor editor)
{
handler.TypedNativeView?.UpdateCharacterSpacing(editor);
Expand All @@ -59,6 +74,16 @@ public static void MapFormatting(EditorHandler handler, IEditor editor)
handler.TypedNativeView?.UpdateCharacterSpacing(editor);
}

void OnChanged(object? sender, System.EventArgs e) => OnTextChanged();

void OnTextChanged()
{
if (TypedNativeView == null)
return;

TypedNativeView.HidePlaceholder(!string.IsNullOrEmpty(TypedNativeView.Text));
}

bool OnShouldChangeText(UITextView textView, NSRange range, string replacementString)
{
var currLength = textView?.Text?.Length ?? 0;
Expand Down
25 changes: 22 additions & 3 deletions src/Core/src/Platform/Android/EditTextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,31 @@ public static void UpdateMaxLength(this AppCompatEditText editText, int maxLengt
editText.Text = TrimToMaxLength(editText.Text, maxLength);
}

public static void UpdatePlaceholder(this AppCompatEditText editText, IEntry entry)
public static void UpdatePlaceholder(this AppCompatEditText editText, IPlaceholder textInput)
{
if (editText.Hint == entry.Placeholder)
if (editText.Hint == textInput.Placeholder)
return;

editText.Hint = entry.Placeholder;
editText.Hint = textInput.Placeholder;
}

public static void UpdatePlaceholderColor(this AppCompatEditText editText, IEditor editor, ColorStateList? defaultColor)
{
var placeholderTextColor = editor.PlaceholderColor;
if (placeholderTextColor.IsDefault)
{
editText.SetHintTextColor(defaultColor);
}
else
{
var androidColor = placeholderTextColor.ToNative();

if (!editText.HintTextColors.IsOneColor(ColorExtensions.States, androidColor))
{
var acolor = androidColor.ToArgb();
editText.SetHintTextColor(new ColorStateList(ColorExtensions.States, new[] { acolor, acolor }));
}
}
}

public static void UpdateIsReadOnly(this AppCompatEditText editText, IEntry entry)
Expand Down
22 changes: 22 additions & 0 deletions src/Core/src/Platform/iOS/EditorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using UIKit;
using Microsoft.Maui.Platform.iOS;

namespace Microsoft.Maui
{
public static class EditorExtensions
{
public static void UpdatePlaceholder(this MauiTextView textView, IEditor editor)
{
textView.PlaceholderText = editor.Placeholder;
}

public static void UpdatePlaceholderColor(this MauiTextView textView, IEditor editor, UIColor? defaultPlaceholderColor)
{
Color placeholderColor = editor.PlaceholderColor;
if (placeholderColor.IsDefault)
textView.PlaceholderTextColor = defaultPlaceholderColor;
else
textView.PlaceholderTextColor = placeholderColor.ToNative();
}
}
}
67 changes: 67 additions & 0 deletions src/Core/src/Platform/iOS/MauiTextView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using UIKit;
using CoreGraphics;
using Foundation;

namespace Microsoft.Maui.Platform.iOS
{
public class MauiTextView : UITextView
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

{
UILabel PlaceholderLabel { get; } = new UILabel
{
BackgroundColor = UIColor.Clear,
Lines = 0
};

public MauiTextView(CGRect frame) : base(frame)
{
InitPlaceholderLabel();
}

public string? PlaceholderText
{
get => PlaceholderLabel.Text;
set
{
PlaceholderLabel.Text = value;
PlaceholderLabel.SizeToFit();
}
}

public UIColor? PlaceholderTextColor
{
get => PlaceholderLabel.TextColor;
set => PlaceholderLabel.TextColor = value;
}

public void HidePlaceholder(bool hide)
{
PlaceholderLabel.Hidden = hide;
}

void InitPlaceholderLabel()
{
AddSubview(PlaceholderLabel);

var edgeInsets = TextContainerInset;
var lineFragmentPadding = TextContainer.LineFragmentPadding;

var vConstraints = NSLayoutConstraint.FromVisualFormat(
"V:|-" + edgeInsets.Top + "-[PlaceholderLabel]-" + edgeInsets.Bottom + "-|", 0, new NSDictionary(),
NSDictionary.FromObjectsAndKeys(
new NSObject[] { PlaceholderLabel }, new NSObject[] { new NSString("PlaceholderLabel") })
);

var hConstraints = NSLayoutConstraint.FromVisualFormat(
"H:|-" + lineFragmentPadding + "-[PlaceholderLabel]-" + lineFragmentPadding + "-|",
0, new NSDictionary(),
NSDictionary.FromObjectsAndKeys(
new NSObject[] { PlaceholderLabel }, new NSObject[] { new NSString("PlaceholderLabel") })
);

PlaceholderLabel.TranslatesAutoresizingMaskIntoConstraints = false;

AddConstraints(hConstraints);
AddConstraints(vConstraints);
}
}
}
1 change: 0 additions & 1 deletion src/Core/src/Primitives/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using Microsoft.Maui;

namespace Microsoft.Maui
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public async Task FontFamilyInitializesCorrectly(string family)
string GetNativeText(EditorHandler editorHandler) =>
GetNativeEditor(editorHandler).Text;

string GetNativePlaceholderText(EditorHandler editorHandler) =>
GetNativeEditor(editorHandler).Hint;

Color GetNativePlaceholderColor(EditorHandler editorHandler) =>
((uint)GetNativeEditor(editorHandler).CurrentHintTextColor).ToColor();

bool GetNativeIsTextPredictionEnabled(EditorHandler editorHandler) =>
!GetNativeEditor(editorHandler).InputType.HasFlag(InputTypes.TextFlagNoSuggestions);

Expand Down