-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement CharacterSpacing property in EditorHandlers (#516)
- Loading branch information
1 parent
71d7d56
commit c214b9d
Showing
13 changed files
with
196 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 44 additions & 3 deletions
47
src/Core/tests/DeviceTests/Handlers/Editor/EditorHandlerTests.Android.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
42 changes: 39 additions & 3 deletions
42
src/Core/tests/DeviceTests/Handlers/Editor/EditorHandlerTests.iOS.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |