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

Implement CharacterSpacing property in EditorHandlers #516

Merged
merged 1 commit into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ void UpdateInputType()
}
}

[PortHandler]
void UpdateCharacterSpacing()
{
if (Forms.IsLollipopOrNewer)
Expand Down
1 change: 1 addition & 0 deletions src/Compatibility/Core/src/iOS/Renderers/EditorRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ protected internal override void UpdatePlaceholderText()
_placeholderLabel.SizeToFit();
}

[PortHandler("Partially ported")]
protected internal override void UpdateCharacterSpacing()
{
var textAttr = TextView.AttributedText.AddCharacterSpacing(Element.Text, Element.CharacterSpacing);
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Handlers/Editor/EditorHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,10 @@ public static void MapText(EditorHandler handler, IEditor editor)
{
handler.TypedNativeView?.UpdateText(editor);
}

public static void MapCharacterSpacing(EditorHandler handler, IEditor editor)
{
handler.TypedNativeView?.UpdateCharacterSpacing(editor);
}
}
}
1 change: 1 addition & 0 deletions src/Core/src/Handlers/Editor/EditorHandler.Standard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ 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 MapCharacterSpacing(IViewHandler handler, IEditor editor) { }
}
}
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 @@ -4,7 +4,8 @@ public partial class EditorHandler
{
public static PropertyMapper<IEditor, EditorHandler> EditorMapper = new PropertyMapper<IEditor, EditorHandler>(ViewHandler.ViewMapper)
{
[nameof(IEditor.Text)] = MapText
[nameof(IEditor.Text)] = MapText,
[nameof(IEditor.CharacterSpacing)] = MapCharacterSpacing
};

public EditorHandler() : base(EditorMapper)
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Handlers/Editor/EditorHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@ public static void MapText(EditorHandler handler, IEditor editor)
{
handler.TypedNativeView?.UpdateText(editor);
}

public static void MapCharacterSpacing(EditorHandler handler, IEditor editor)
{
handler.TypedNativeView?.UpdateCharacterSpacing(editor);
}
}
}
5 changes: 5 additions & 0 deletions src/Core/src/Platform/Android/EditorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@ public static void UpdateText(this AppCompatEditText editText, IEditor editor)

editText.SetSelection(text.Length);
}

public static void UpdateCharacterSpacing(this AppCompatEditText editText, IEditor editor)
{
editText.LetterSpacing = editor.CharacterSpacing.ToEm();
}
}
}
62 changes: 62 additions & 0 deletions src/Core/src/Platform/iOS/CharacterSpacingExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Foundation;
using UIKit;

namespace Microsoft.Maui
{
public static class CharacterSpacingExtensions
{
public static NSMutableAttributedString? AddCharacterSpacing(this NSAttributedString attributedString, string text, double characterSpacing)
{
if (attributedString == null && characterSpacing == 0)
return null;

NSMutableAttributedString? mutableAttributedString = attributedString as NSMutableAttributedString;
if (attributedString == null || attributedString.Length == 0)
{
mutableAttributedString = text == null ? new NSMutableAttributedString() : new NSMutableAttributedString(text);
}
else
{
mutableAttributedString = new NSMutableAttributedString(attributedString);

if (!mutableAttributedString.MutableString.ToString().Equals(text))
{
mutableAttributedString.MutableString.SetString(new NSString(text));
}
}

AddKerningAdjustment(mutableAttributedString, text, characterSpacing);

return mutableAttributedString;
}

internal static bool HasCharacterAdjustment(this NSMutableAttributedString mutableAttributedString)
{
if (mutableAttributedString == null)
return false;

var attributes = mutableAttributedString.GetAttributes(0, out NSRange removalRange);

for (uint i = 0; i < attributes.Count; i++)
if (attributes.Keys[i] is NSString nSString && nSString == UIStringAttributeKey.KerningAdjustment)
return true;

return false;
}

internal static void AddKerningAdjustment(NSMutableAttributedString mutableAttributedString, string? text, double characterSpacing)
{
if (!string.IsNullOrEmpty(text))
{
if (characterSpacing == 0 && !mutableAttributedString.HasCharacterAdjustment())
return;

mutableAttributedString.AddAttribute
(
UIStringAttributeKey.KerningAdjustment,
NSObject.FromObject(characterSpacing), new NSRange(0, text != null ? text.Length - 1 : 0)
);
}
}
}
}
10 changes: 10 additions & 0 deletions src/Core/src/Platform/iOS/EditorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,15 @@ public static void UpdateText(this UITextView textView, IEditor editor)
textView.Text = text;
}
}

public static void UpdateCharacterSpacing(this UITextView textView, IEditor editor)
{
var textAttr = textView.AttributedText.AddCharacterSpacing(editor.Text, editor.CharacterSpacing);

if (textAttr != null)
textView.AttributedText = textAttr;

// TODO: Include AttributedText to Label Placeholder
}
}
}
18 changes: 18 additions & 0 deletions src/Core/tests/DeviceTests/AssertionExtensions.iOS.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Threading.Tasks;
using CoreGraphics;
using Foundation;
using Microsoft.Maui.Essentials;
using UIKit;
using Xunit;
Expand Down Expand Up @@ -195,5 +196,22 @@ public static UILineBreakMode ToNative(this LineBreakMode mode) =>
LineBreakMode.MiddleTruncation => UILineBreakMode.MiddleTruncation,
_ => throw new ArgumentOutOfRangeException(nameof(mode))
};

public static double GetCharacterSpacing(this NSAttributedString text)
{
if (text == null)
return 0;

var value = text.GetAttribute(UIStringAttributeKey.KerningAdjustment, 0, out var range);
if (value == null)
return 0;

Assert.Equal(0, range.Location);
Assert.Equal(text.Length, range.Length);

var kerning = Assert.IsType<NSNumber>(value);

return kerning.DoubleValue;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,55 @@
using AndroidX.AppCompat.Widget;
using System.Threading.Tasks;
using AndroidX.AppCompat.Widget;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Handlers;
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
public partial class EditorHandlerTests
{
AppCompatEditText GetNativeEditor(EditorHandler editorHandler) =>
[Fact(DisplayName = "CharacterSpacing Initializes Correctly")]
public async Task CharacterSpacingInitializesCorrectly()
{
var xplatCharacterSpacing = 4;

var editor = new EditorStub()
{
CharacterSpacing = xplatCharacterSpacing,
Text = "Test"
};

float expectedValue = editor.CharacterSpacing.ToEm();

var values = await GetValueAsync(editor, (handler) =>
{
return new
{
ViewValue = editor.CharacterSpacing,
NativeViewValue = GetNativeCharacterSpacing(handler)
};
});

Assert.Equal(xplatCharacterSpacing, values.ViewValue);
Assert.Equal(expectedValue, values.NativeViewValue, EmCoefficientPrecision);
}

AppCompatEditText GetNativeEditor(EditorHandler editorHandler) =>
(AppCompatEditText)editorHandler.View;

string GetNativeText(EditorHandler editorHandler) =>
GetNativeEditor(editorHandler).Text;
}

double GetNativeCharacterSpacing(EditorHandler editorHandler)
{
var editText = GetNativeEditor(editorHandler);

if (editText != null)
{
return editText.LetterSpacing;
}

return -1;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,50 @@
using Microsoft.Maui.Handlers;
using System.Threading.Tasks;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Handlers;
using UIKit;
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
public partial class EditorHandlerTests
{
UITextView GetNativeEditor(EditorHandler editorHandler) =>
[Fact(DisplayName = "CharacterSpacing Initializes Correctly")]
public async Task CharacterSpacingInitializesCorrectly()
{
string originalText = "Test";
var xplatCharacterSpacing = 4;

var editor = new EditorStub()
{
CharacterSpacing = xplatCharacterSpacing,
Text = originalText
};

var values = await GetValueAsync(editor, (handler) =>
{
return new
{
ViewValue = editor.CharacterSpacing,
NativeViewValue = GetNativeCharacterSpacing(handler)
};
});

Assert.Equal(xplatCharacterSpacing, values.ViewValue);
Assert.Equal(xplatCharacterSpacing, values.NativeViewValue);
}

UITextView GetNativeEditor(EditorHandler editorHandler) =>
(UITextView)editorHandler.View;

string GetNativeText(EditorHandler editorHandler) =>
GetNativeEditor(editorHandler).Text;
}

double GetNativeCharacterSpacing(EditorHandler editorHandler)
{
var searchBar = GetNativeEditor(editorHandler);
var textField = searchBar.FindDescendantView<UITextField>();

return textField.AttributedText.GetCharacterSpacing();
}
}
}
8 changes: 3 additions & 5 deletions src/Core/tests/DeviceTests/TestBase.Android.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.Maui.DeviceTests
{
public partial class TestBase
{
public global::Android.Content.Context DefaultContext =>
public const int EmCoefficientPrecision = 4;

public Android.Content.Context DefaultContext =>
Platform.DefaultContext;
}
}