Skip to content

Commit

Permalink
Implement CharacterSpacing property in EntryHandlers (#566)
Browse files Browse the repository at this point in the history
* CharacterSpacing  added in Entry

* PortHandled attribute added.
  • Loading branch information
almirvuk committed Mar 23, 2021
1 parent 837d97d commit 1768c24
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ void UpdateMaxLength()
EditText.Text = currentControlText.Substring(0, Element.MaxLength);
}

[PortHandler]
void UpdateCharacterSpacing()
{
if (Forms.IsLollipopOrNewer)
Expand Down
1 change: 1 addition & 0 deletions src/Compatibility/Core/src/iOS/Renderers/EntryRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ void UpdateText()
Control.Text = text;
}

[PortHandler ("Partially ported ...")]
void UpdateCharacterSpacing()
{
var textAttr = Control.AttributedText.AddCharacterSpacing(Element.Text, Element.CharacterSpacing);
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 @@ -101,6 +101,7 @@ void SetupMauiLayout()
verticalStack.Add(new Entry { Placeholder = "This should be placeholder text" });
verticalStack.Add(new Entry { Text = "This should be read only property", IsReadOnly = true });
verticalStack.Add(new Entry { MaxLength = 5, Placeholder = "MaxLength text" });
verticalStack.Add(new Entry { Text = "This should be text with character spacing", CharacterSpacing = 10 });

verticalStack.Add(new ProgressBar { Progress = 0.5 });
verticalStack.Add(new ProgressBar { Progress = 0.5, BackgroundColor = Color.LightCoral });
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Handlers/Entry/EntryHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public static void MapReturnType(EntryHandler handler, IEntry entry)
handler.TypedNativeView?.UpdateReturnType(entry);
}

public static void MapCharacterSpacing(EntryHandler handler, IEntry entry)
{
handler.TypedNativeView?.UpdateCharacterSpacing(entry);
}

void OnTextChanged(string? text)
{
if (VirtualView == null || TypedNativeView == null)
Expand Down
1 change: 1 addition & 0 deletions src/Core/src/Handlers/Entry/EntryHandler.Standard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ public partial class EntryHandler : AbstractViewHandler<IEntry, object>
public static void MapIsReadOnly(IViewHandler handler, IEntry entry) { }
public static void MapFont(IViewHandler handler, IEntry entry) { }
public static void MapReturnType(IViewHandler handler, IEntry entry) { }
public static void MapCharacterSpacing(IViewHandler handler, IEntry entry) { }
}
}
3 changes: 2 additions & 1 deletion src/Core/src/Handlers/Entry/EntryHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public partial class EntryHandler
[nameof(IEntry.Placeholder)] = MapPlaceholder,
[nameof(IEntry.IsReadOnly)] = MapIsReadOnly,
[nameof(IEntry.Font)] = MapFont,
[nameof(IEntry.ReturnType)] = MapReturnType
[nameof(IEntry.ReturnType)] = MapReturnType,
[nameof(IEntry.CharacterSpacing)] = MapCharacterSpacing
};

public EntryHandler() : base(EntryMapper)
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Handlers/Entry/EntryHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ public static void MapFormatting(EntryHandler handler, IEntry entry)
handler.TypedNativeView?.UpdateHorizontalTextAlignment(entry);
}

public static void MapCharacterSpacing(EntryHandler handler, IEntry entry)
{
handler.TypedNativeView?.UpdateCharacterSpacing(entry);
}

void OnEditingChanged(object? sender, EventArgs e) => OnTextChanged();

void OnEditingEnded(object? sender, EventArgs e) => OnTextChanged();
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Platform/Android/EditTextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ public static void UpdateCharacterSpacing(this AppCompatEditText editText, IEdit
editText.LetterSpacing = editor.CharacterSpacing.ToEm();
}

public static void UpdateCharacterSpacing(this AppCompatEditText editText, IEntry editor)
{
editText.LetterSpacing = editor.CharacterSpacing.ToEm();
}

internal static void SetInputType(this AppCompatEditText editText, IEntry entry)
{
editText.InputType = InputTypes.ClassText;
Expand Down
8 changes: 8 additions & 0 deletions src/Core/src/Platform/iOS/TextFieldExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ public static void UpdateCharacterSpacing(this UITextField textField, IText text
textField.AttributedText = textAttr;
}

public static void UpdateCharacterSpacing(this UITextField textField, IEntry textView)
{
var textAttr = textField.AttributedText?.WithCharacterSpacing(textView.CharacterSpacing);

if (textAttr != null)
textField.AttributedText = textAttr;
}

public static void UpdateFont(this UITextField textField, IText textView, IFontManager fontManager)
{
var uiFont = fontManager.GetFont(textView.Font);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,43 @@ bool GetNativeIsReadOnly(EntryHandler entryHandler)

ImeAction GetNativeReturnType(EntryHandler entryHandler) =>
GetNativeEntry(entryHandler).ImeOptions;

[Fact(DisplayName = "CharacterSpacing Initializes Correctly")]
public async Task CharacterSpacingInitializesCorrectly()
{
var xplatCharacterSpacing = 4;

var entry = new EntryStub()
{
CharacterSpacing = xplatCharacterSpacing,
Text = "Some Test Text"
};

float expectedValue = entry.CharacterSpacing.ToEm();

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

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

double GetNativeCharacterSpacing(EntryHandler entryHandler)
{
var editText = GetNativeEntry(entryHandler);

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

return -1;
}
}
}
31 changes: 31 additions & 0 deletions src/Core/tests/DeviceTests/Handlers/Entry/EntryHandlerTests.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,37 @@ public async Task ReturnTypeInitializesCorrectly()
Assert.Equal(expectedValue, values.NativeViewValue);
}

[Fact(DisplayName = "CharacterSpacing Initializes Correctly")]
public async Task CharacterSpacingInitializesCorrectly()
{
string originalText = "Some Test Text";
var xplatCharacterSpacing = 4;

var entry = new EntryStub()
{
CharacterSpacing = xplatCharacterSpacing,
Text = originalText
};

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

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

double GetNativeCharacterSpacing(EntryHandler entryHandler)
{
var entry = GetNativeEntry(entryHandler);
return entry.AttributedText.GetCharacterSpacing();
}

UITextField GetNativeEntry(EntryHandler entryHandler) =>
(UITextField)entryHandler.View;

Expand Down

0 comments on commit 1768c24

Please sign in to comment.