|
| 1 | +namespace Maui.Controls.Sample.Issues; |
| 2 | + |
| 3 | +[Issue(IssueTracker.Github, 24977, "Keyboard Scrolling in editors with Center or End VerticalTextAlignment is off", PlatformAffected.iOS)] |
| 4 | +public class Issue24977 : TestNavigationPage |
| 5 | +{ |
| 6 | + protected override void Init() |
| 7 | + { |
| 8 | + PushAsync(new ContentPage |
| 9 | + { |
| 10 | + Content = new VerticalStackLayout |
| 11 | + { |
| 12 | + Spacing = 12, |
| 13 | + Padding = new Thickness(12, 24), |
| 14 | + Children = |
| 15 | + { |
| 16 | + CreateButton(TextAlignment.Start), |
| 17 | + CreateButton(TextAlignment.Center), |
| 18 | + CreateButton(TextAlignment.End), |
| 19 | + } |
| 20 | + } |
| 21 | + }); |
| 22 | + } |
| 23 | + |
| 24 | + Button CreateButton(TextAlignment textAlignment) |
| 25 | + { |
| 26 | + var btn = new Button |
| 27 | + { |
| 28 | + Text = textAlignment.ToString(), |
| 29 | + AutomationId = $"{textAlignment}Button" |
| 30 | + }; |
| 31 | + btn.Clicked += (s, e) => Navigation.PushAsync(new Issue24977_1(textAlignment)); |
| 32 | + return btn; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +public class Issue24977_1 : TestContentPage |
| 37 | +{ |
| 38 | + Editor editor; |
| 39 | + Label cursorHeightTracker; |
| 40 | + |
| 41 | + public Issue24977_1(TextAlignment textAlignment) |
| 42 | + { |
| 43 | + editor.VerticalTextAlignment = textAlignment; |
| 44 | + } |
| 45 | + |
| 46 | + protected override void Init() |
| 47 | + { |
| 48 | + var rootGrid = new Grid |
| 49 | + { |
| 50 | + RowDefinitions = |
| 51 | + { |
| 52 | + new RowDefinition { Height = 50 }, |
| 53 | + new RowDefinition { Height = 50 }, |
| 54 | + new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }, |
| 55 | + new RowDefinition { Height = 50 } |
| 56 | + }, |
| 57 | + Margin = new Thickness(30) |
| 58 | + }; |
| 59 | + |
| 60 | + var entry = new Entry |
| 61 | + { |
| 62 | + Text = "Content before", |
| 63 | + AutomationId = "EntryBefore", |
| 64 | + ReturnType = ReturnType.Next, |
| 65 | + BackgroundColor = Colors.Aquamarine, |
| 66 | + }; |
| 67 | + |
| 68 | + cursorHeightTracker = new Label |
| 69 | + { |
| 70 | + Text = "0", |
| 71 | + AutomationId = "CursorHeightTracker", |
| 72 | + BackgroundColor = Colors.Aquamarine, |
| 73 | + }; |
| 74 | + |
| 75 | + editor = new Editor |
| 76 | + { |
| 77 | + Text = "Hello World!", |
| 78 | + AutomationId = "IssueEditor", |
| 79 | + BackgroundColor = Colors.Orange, |
| 80 | + }; |
| 81 | + editor.TextChanged += Editor_TextChanged; |
| 82 | + |
| 83 | + var button = new Button { Text = "Erase" }; |
| 84 | + button.Clicked += (s, e) => editor.Text = string.Empty; |
| 85 | + |
| 86 | + rootGrid.Add(entry, 0, 0); |
| 87 | + rootGrid.Add(cursorHeightTracker, 0, 1); |
| 88 | + rootGrid.Add(editor, 0, 2); |
| 89 | + |
| 90 | + rootGrid.Add(button, 0, 3); |
| 91 | + |
| 92 | + Content = rootGrid; |
| 93 | + } |
| 94 | + |
| 95 | + private void Editor_TextChanged(object sender, TextChangedEventArgs e) |
| 96 | + { |
| 97 | + if (sender is Editor editor) |
| 98 | + { |
| 99 | + AddCursorHeightToLabel(editor); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + void AddCursorHeightToLabel(Editor editor) |
| 104 | + { |
| 105 | +#if IOS |
| 106 | + var textInput = editor.Handler.PlatformView as UIKit.UITextView; |
| 107 | + var selectedTextRange = textInput?.SelectedTextRange; |
| 108 | + var localCursor = selectedTextRange is not null ? textInput?.GetCaretRectForPosition(selectedTextRange.Start) : null; |
| 109 | + |
| 110 | + if (localCursor is CoreGraphics.CGRect local && textInput is not null) |
| 111 | + { |
| 112 | + var container = GetContainerView(textInput); |
| 113 | + var cursorInContainer = container.ConvertRectFromView(local, textInput); |
| 114 | + var cursorInWindow = container.ConvertRectToView(cursorInContainer, null); |
| 115 | + |
| 116 | + cursorHeightTracker.Text = cursorInWindow.Y.ToString(); |
| 117 | + } |
| 118 | + |
| 119 | + UIKit.UIView GetContainerView(UIKit.UIView startingPoint) |
| 120 | + { |
| 121 | + var rootView = FindResponder<Microsoft.Maui.Platform.ContainerViewController>(startingPoint)?.View; |
| 122 | + |
| 123 | + if (rootView is not null) |
| 124 | + { |
| 125 | + return rootView; |
| 126 | + } |
| 127 | + |
| 128 | + return null; |
| 129 | + } |
| 130 | + |
| 131 | + T FindResponder<T>(UIKit.UIView view) |
| 132 | + where T : UIKit.UIResponder |
| 133 | + { |
| 134 | + var nextResponder = view as UIKit.UIResponder; |
| 135 | + while (nextResponder is not null) |
| 136 | + { |
| 137 | + nextResponder = nextResponder.NextResponder; |
| 138 | + |
| 139 | + if (nextResponder is T responder) |
| 140 | + return responder; |
| 141 | + } |
| 142 | + return null; |
| 143 | + } |
| 144 | +#endif |
| 145 | + } |
| 146 | +} |
0 commit comments