public static void UpdateSelectionLength(this UITextView textView, IEditor editor) { var selectedTextRange = textView.SelectedTextRange; if (selectedTextRange == null) { return; } if (textView.GetOffsetFromPosition(selectedTextRange.Start, selectedTextRange.End) != editor.SelectionLength) { UpdateCursorSelection(textView, editor); } }
public static void UpdateCursorPosition(this UITextView textView, IEditor editor) { var selectedTextRange = textView.SelectedTextRange; if (selectedTextRange == null) { return; } if (textView.GetOffsetFromPosition(textView.BeginningOfDocument, selectedTextRange.Start) != editor.CursorPosition) { UpdateCursorSelection(textView, editor); } }
static UITextPosition GetSelectionStart(UITextView textView, IEditor editor, out int startOffset) { int cursorPosition = editor.CursorPosition; UITextPosition start = textView.GetPosition(textView.BeginningOfDocument, cursorPosition) ?? textView.EndOfDocument; startOffset = Math.Max(0, (int)textView.GetOffsetFromPosition(textView.BeginningOfDocument, start)); if (startOffset != cursorPosition) { editor.CursorPosition = startOffset; } return(start); }
static UITextPosition GetSelectionEnd(UITextView textView, IEditor editor, UITextPosition start, int startOffset) { int selectionLength = editor.SelectionLength; int textFieldLength = textView.Text == null ? 0 : textView.Text.Length; // Get the desired range in respect to the actual length of the text we are working with UITextPosition end = textView.GetPosition(start, Math.Min(textFieldLength - editor.CursorPosition, selectionLength)) ?? start; int endOffset = Math.Max(startOffset, (int)textView.GetOffsetFromPosition(textView.BeginningOfDocument, end)); int newSelectionLength = Math.Max(0, endOffset - startOffset); if (newSelectionLength != selectionLength) { editor.SelectionLength = newSelectionLength; } return(end); }
private static void HandleTap(UITapGestureRecognizer gestureRecognizer, UITextView textView, Dictionary <Source, FinalSourcePosition> sourcePositions, IInteractiveSourceAction action) { var point = gestureRecognizer.LocationInView(textView); point.Y += textView.ContentOffset.Y; var tapPos = textView.GetClosestPositionToPoint(point); // if a source link has been tapped: show the reference nint clickedPos = textView.GetOffsetFromPosition(textView.BeginningOfDocument, tapPos); int pos = Convert.ToInt32(clickedPos); var clickedSource = sourcePositions.SingleOrDefault(x => x.Value.Start <= pos && pos <= x.Value.End).Key; if (clickedSource != null) { action.Display(clickedSource); } }
private void Tap(UITapGestureRecognizer tap) { var location = tap.LocationInView(Control); // Fale UseTextView var fakeTextViwe = new UITextView(Control.Frame); fakeTextViwe.TextContainer.LineFragmentPadding = 0; fakeTextViwe.TextContainerInset = UIEdgeInsets.Zero; fakeTextViwe.AttributedText = Control.AttributedText; var selectedPositon = fakeTextViwe.GetClosestPositionToPoint(location); var position = (int)fakeTextViwe.GetOffsetFromPosition(fakeTextViwe.BeginningOfDocument, selectedPositon); var label = Element as Shared.LinkerLabel; if (label.MatchWords.Any(x => x.StartPosition <= position && x.EndPositon >= position)) { var word = label.MatchWords.First(x => x.StartPosition <= position && x.EndPositon >= position); if (label.Command?.CanExecute(word.Word) == true) { label.Command.Execute(word.Word); } } }
protected void OnTapUrl(UIGestureRecognizer tap) { var location = tap.LocationInView(Control); var textView = new UITextView(Control.Frame); textView.TextContainer.LineFragmentPadding = 0; textView.TextContainerInset = UIEdgeInsets.Zero; textView.AttributedText = Control.AttributedText; var position = textView.GetOffsetFromPosition( textView.BeginningOfDocument, textView.GetClosestPositionToPoint(location) ); var url = this.Matches.FirstOrDefault(m => m.Index <= position && position <= (m.Index + m.Length) ); if (url != null) { UIApplication.SharedApplication.OpenUrl(new Uri(url.Value)); } }
async public override void Changed (UITextView textView) { string text = StringUtils.TrimWhiteSpaceAndNewLine (textView.Text); UpdateCharacterCount(text); Validate (); if (SuggestionsCollectionView != null) { if (text.Length <= 2 || !text.Contains ("@")) { SuggestionsCollectionView.Hidden = true; return; } int cursorPositionEarly = (int)textView.GetOffsetFromPosition (textView.BeginningOfDocument, textView.SelectedTextRange.Start); if (cursorPositionEarly <= 0) { return; } string charToTest = textView.Text.Substring (cursorPositionEarly - 1, 1); if (charToTest != "@" && charToTest != " ") { string beforeTextEarly = textView.Text.Substring (0, cursorPositionEarly); string strippedToLastSpaceOnward = beforeTextEarly; if (strippedToLastSpaceOnward.IndexOf ("") > -1 && strippedToLastSpaceOnward.Length > 2) { strippedToLastSpaceOnward = strippedToLastSpaceOnward.Substring (strippedToLastSpaceOnward.LastIndexOf (" ") + 1); } if (strippedToLastSpaceOnward.Length > 1 && strippedToLastSpaceOnward.Substring (0, 1) == "@") { try { List<User> users = await TenServices.SearchFollowingUsers(strippedToLastSpaceOnward.Substring(1)); SuggestionsCollectionViewDelegate del = new SuggestionsCollectionViewDelegate(); del.ItemSelectedAction += delegate (int index) { int cursorPosition = (int)textView.GetOffsetFromPosition(textView.BeginningOfDocument, textView.SelectedTextRange.Start); string currentText = textView.Text; string beforeText = currentText.Substring(0, cursorPosition); beforeText = beforeText.Substring(0, beforeText.LastIndexOf('@')); string afterText = currentText.Substring(cursorPosition); currentText = currentText.Substring(0, cursorPosition); textView.Text = beforeText + "@" + users[index].username + " " + afterText; SuggestionsCollectionView.Hidden = true; UpdateCharacterCount(textView.Text); }; SuggestionsCollectionView.Delegate = del; SuggestionsCollectionView.DataSource = new SuggestionsCollectionViewDataSource(users); SuggestionsCollectionView.Hidden = false; } catch (RESTError e) { Console.WriteLine(e.Message); } finally { UpdateCharacterCount(text); } } else { SuggestionsCollectionView.Hidden = true; } } UpdateCharacterCount(text); } }