Implement CharacterSpacing property in EntryHandlers (#566)

* CharacterSpacing  added in Entry

* PortHandled attribute added.
This commit is contained in:
Almir Vuk 2021-03-23 05:48:58 +01:00 коммит произвёл GitHub
Родитель 837d97d515
Коммит 1768c24f50
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
11 изменённых файлов: 98 добавлений и 1 удалений

Просмотреть файл

@ -390,6 +390,7 @@ namespace Microsoft.Maui.Controls.Compatibility.Platform.Android
EditText.Text = currentControlText.Substring(0, Element.MaxLength);
}
[PortHandler]
void UpdateCharacterSpacing()
{
if (Forms.IsLollipopOrNewer)

Просмотреть файл

@ -375,6 +375,7 @@ namespace Microsoft.Maui.Controls.Compatibility.Platform.iOS
Control.Text = text;
}
[PortHandler ("Partially ported ...")]
void UpdateCharacterSpacing()
{
var textAttr = Control.AttributedText.AddCharacterSpacing(Element.Text, Element.CharacterSpacing);

Просмотреть файл

@ -101,6 +101,7 @@ namespace Maui.Controls.Sample.Pages
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 });

Просмотреть файл

@ -89,6 +89,11 @@ namespace Microsoft.Maui.Handlers
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)

Просмотреть файл

@ -16,5 +16,6 @@ namespace Microsoft.Maui.Handlers
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) { }
}
}

Просмотреть файл

@ -13,7 +13,8 @@
[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)

Просмотреть файл

@ -114,6 +114,11 @@ namespace Microsoft.Maui.Handlers
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();

Просмотреть файл

@ -139,6 +139,11 @@ namespace Microsoft.Maui
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;

Просмотреть файл

@ -91,6 +91,14 @@ namespace Microsoft.Maui
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);

Просмотреть файл

@ -142,5 +142,43 @@ namespace Microsoft.Maui.DeviceTests
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;
}
}
}

Просмотреть файл

@ -85,6 +85,37 @@ namespace Microsoft.Maui.DeviceTests
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;