IndexedPosition GetPosition(UITextRange range, UITextLayoutDirection direction) { // Note that this sample assumes LTR text direction IndexedRange r = (IndexedRange)range; int pos = r.Range.Location; // For this sample, we just return the extent of the given range if the // given direction is "forward" in a LTR context (UITextLayoutDirectionRight // or UITextLayoutDirectionDown), otherwise we return just the range position switch (direction) { case UITextLayoutDirection.Up: case UITextLayoutDirection.Left: pos = r.Range.Location; break; case UITextLayoutDirection.Right: case UITextLayoutDirection.Down: pos = r.Range.Location + r.Range.Length; break; } // Return text position using our UITextPosition implementation. // Note that position is not currently checked against document range. return(IndexedPosition.GetPosition(pos)); }
void ReplaceRange(UITextRange range, string text) { IndexedRange r = (IndexedRange)range; NSRange selectedNSRange = textView.SelectedTextRange; // Determine if replaced range intersects current selection range // and update selection range if so. if (r.Range.Location + r.Range.Length <= selectedNSRange.Location) { // This is the easy case. selectedNSRange.Location -= (r.Range.Length - text.Length); } else { // Need to also deal with overlapping ranges. Not addressed // in this simplified sample. } // Now replace characters in text storage this.text.Remove(r.Range.Location, r.Range.Length); this.text.Insert(r.Range.Location, text); // Update underlying SimpleCoreTextView textView.Text = this.text.ToString(); textView.SelectedTextRange = selectedNSRange; }
IndexedRange GetTextRange(UITextPosition fromPosition, UITextPosition toPosition) { // Generate IndexedPosition instances that wrap the to and from ranges IndexedPosition @from = (IndexedPosition)fromPosition; IndexedPosition @to = (IndexedPosition)toPosition; NSRange range = new NSRange(Math.Min(@from.Index, @to.Index), Math.Abs(to.Index - @from.Index)); return(IndexedRange.GetRange(range)); }
RectangleF FirstRect(UITextRange range) { // FIXME: the Objective-C code doesn't get a null range // This is the reason why we don't get the autocorrection suggestions // (it'll just autocorrect without showing any suggestions). // Possibly due to http://bugzilla.xamarin.com/show_bug.cgi?id=265 IndexedRange r = (IndexedRange)(range ?? IndexedRange.GetRange(new NSRange(0, 1))); // Use underlying SimpleCoreTextView to get rect for range RectangleF rect = textView.FirstRect(r.Range); // Convert rect to our view coordinates return(ConvertRectFromView(rect, textView)); }
public static IndexedRange GetRange(NSRange theRange) { IndexedRange result; if (theRange.Location == NSRange.NotFound) { return(null); } if (!ranges.TryGetValue(theRange, out result)) { result = new IndexedRange(); result.Range = new NSRange(theRange.Location, theRange.Length); } return(result); }
IndexedRange GetCharacterRange(UITextPosition position, UITextLayoutDirection direction) { // Note that this sample assumes LTR text direction IndexedPosition pos = (IndexedPosition)position; NSRange result = new NSRange(pos.Index, 1); switch (direction) { case UITextLayoutDirection.Up: case UITextLayoutDirection.Left: result = new NSRange(pos.Index - 1, 1); break; case UITextLayoutDirection.Right: case UITextLayoutDirection.Down: result = new NSRange(pos.Index, 1); break; } // Return range using our UITextRange implementation // Note that range is not currently checked against document range. return(IndexedRange.GetRange(result)); }
string TextInRange(UITextRange range) { IndexedRange r = (IndexedRange)range; return(text.ToString().Substring(r.Range.Location, r.Range.Length)); }