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 @@ -268,6 +269,7 @@ void UpdateText()

abstract protected void UpdateTextColor();

[PortHandler]
protected virtual void UpdatePlaceholderText()
{
if (EditText.Hint == Element.Placeholder)
Expand All @@ -276,6 +278,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 @@ -341,7 +344,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 @@ -47,6 +47,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
{
Expand Down
8 changes: 6 additions & 2 deletions src/Core/src/Core/IEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
/// <summary>
/// Represents a View used to accept multi-line input.
/// </summary>
public interface IEditor : IView, IText
public interface IEditor : IView, ITextInput
{
/// <summary>
/// Gets or sets the placeholder text color.
/// </summary>
Color PlaceholderColor { get; set; }

/// Gets a value that controls whether text prediction and automatic text correction is on or off.
/// </summary>
bool IsTextPredictionEnabled { get; }
}
}
}
23 changes: 21 additions & 2 deletions src/Core/src/Handlers/Editor/EditorHandler.Android.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using Android.Views;
using Android.Views;
using Android.Views.InputMethods;
using AndroidX.AppCompat.Widget;
using Android.Content.Res;

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 @@ -21,11 +24,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 All @@ -36,4 +55,4 @@ public static void MapIsTextPredictionEnabled(EditorHandler handler, IEditor edi
handler.TypedNativeView?.UpdateIsTextPredictionEnabled(editor);
}
}
}
}
6 changes: 5 additions & 1 deletion src/Core/src/Handlers/Editor/EditorHandler.Standard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ public partial class EditorHandler : AbstractViewHandler<IEditor, object>

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 MapIsTextPredictionEnabled(EditorHandler handler, IEditor editor) { }
}
}
}
5 changes: 3 additions & 2 deletions src/Core/src/Handlers/Editor/EditorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ 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.IsTextPredictionEnabled)] = MapIsTextPredictionEnabled
};

public EditorHandler() : base(EditorMapper)
{

}

public EditorHandler(PropertyMapper? mapper = null) : base(mapper ?? EditorMapper)
{

}
}
}
}
48 changes: 43 additions & 5 deletions src/Core/src/Handlers/Editor/EditorHandler.iOS.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,64 @@
using System;
using System;
using CoreGraphics;
using UIKit;
using Microsoft.Maui.Platform.iOS;

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

static readonly UIColor DefaultPlaceholderColor = ColorExtensions.PlaceholderColor;

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

protected override void SetupDefaults(MauiTextView nativeView)
{
nativeView.PlaceholderTextColor = DefaultPlaceholderColor;
brunck marked this conversation as resolved.
Show resolved Hide resolved
}

public override Size GetDesiredSize(double widthConstraint, double heightConstraint) =>
new SizeRequest(new Size(widthConstraint, BaseHeight));

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

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

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

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

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

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, DefaultPlaceholderColor);
}

public static void MapCharacterSpacing(EditorHandler handler, IEditor editor)
{
handler.TypedNativeView?.UpdateCharacterSpacing(editor);
Expand All @@ -31,4 +69,4 @@ public static void MapIsTextPredictionEnabled(EditorHandler handler, IEditor edi
handler.TypedNativeView?.UpdatePredictiveText(editor);
}
}
}
}
35 changes: 35 additions & 0 deletions src/Core/src/Platform/Android/EditorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Android.Content.Res;
using AndroidX.AppCompat.Widget;

namespace Microsoft.Maui
{
public static class EditorExtensions
{
public static void UpdatePlaceholder(this AppCompatEditText editText, IEditor editor)
brunck marked this conversation as resolved.
Show resolved Hide resolved
{
if (editText.Hint == editor.Placeholder)
return;

editText.Hint = editor.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 }));
}
}
}
}
}
10 changes: 10 additions & 0 deletions src/Core/src/Platform/Standard/EditorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Microsoft.Maui
{
public static class EditorExtensions
brunck marked this conversation as resolved.
Show resolved Hide resolved
{
public static void UpdatePlaceholder(this object nothing, IEditor editor)
{

}
}
}
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