How to get selected value without selecting copy or cut options from UIPasteboard in IOS

gdkgdk INMember ✭✭✭
edited March 2017 in Xamarin.iOS


Hi everyone, I have struggle for getting selected value from UITextView without selecting any copy or cut options in UIPasteboard. I have idea about getting selected value while copy option is selecting in UIPasteboard but I want selected text without select any thing. Please suggest any idea. Thanks in advance.

Tagged:

Best Answer

  • JGoldbergerJGoldberger US Xamurai
    Accepted Answer

    @gdk

    There is an event called SelectionChanged that you can subscribe to to let you know that the selection in the UITextView has changed. When this event triggers you can get the range that is selected with the SelectedTextRange property on the UITextView and then get the text that is in that range with the TextInRange method of the UITextView. E.g.

    public override void ViewDidLoad()
    {
          ...
          textView.SelectionChanged += TextView_SelectionChanged;
    }
    
     void TextView_SelectionChanged(object sender, EventArgs e)
     {
            // First you make sure there is actually some text selected by testing if the range is not emtpy
            if (!textView.SelectedTextRange.IsEmpty) {
                  // then get the selected text
                  UITextRange selectedRange = textView.SelectedTextRange;
                  string selectedText = textView.TextInRange(selectedRange);
                  Console.WriteLine(selectedText);
            }
     }
    

Answers

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai
    Accepted Answer

    @gdk

    There is an event called SelectionChanged that you can subscribe to to let you know that the selection in the UITextView has changed. When this event triggers you can get the range that is selected with the SelectedTextRange property on the UITextView and then get the text that is in that range with the TextInRange method of the UITextView. E.g.

    public override void ViewDidLoad()
    {
          ...
          textView.SelectionChanged += TextView_SelectionChanged;
    }
    
     void TextView_SelectionChanged(object sender, EventArgs e)
     {
            // First you make sure there is actually some text selected by testing if the range is not emtpy
            if (!textView.SelectedTextRange.IsEmpty) {
                  // then get the selected text
                  UITextRange selectedRange = textView.SelectedTextRange;
                  string selectedText = textView.TextInRange(selectedRange);
                  Console.WriteLine(selectedText);
            }
     }
    
Sign In or Register to comment.