logo
down
shadow

Grab SELECTED text on UITextView


Grab SELECTED text on UITextView

By : user3864984
Date : January 10 2021, 01:49 PM
hope this fix your issue How do I grab the SELECTED or HIGHLIGHTED text on a UITextView? I already know how to do this on UIWebView using JavaScript. Now I am trying to figure it out for the UITextView. , you can do it by
code :


Share : facebook icon twitter icon
How can I get the selected text frame from a UITextView

How can I get the selected text frame from a UITextView


By : Anthony Williams
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I think [UITextInput selectedTextRange] and [UITextInput caretRectForPosition:] is what you are looking for.
[UITextInput selectedTextRange] returns the selected range in character
code :
UITextRange * selectionRange = [textView selectedTextRange];
CGRect selectionStartRect = [textView caretRectForPosition:selectionRange.start];
CGRect selectionEndRect = [textView caretRectForPosition:selectionRange.end];
CGPoint selectionCenterPoint = (CGPoint){(selectionStartRect.origin.x + selectionEndRect.origin.x)/2,(selectionStartRect.origin.y + selectionStartRect.size.height / 2)};
How to print out selected text in UITextview?

How to print out selected text in UITextview?


By : Jama
Date : March 29 2020, 07:55 AM
this will help
code :
NSString *selectedText = [textView.text substringWithRange:[textView selectedRange]];
NSLog (@"This is selected text in UITextView %@", selectedText);
UIMenuController and UITextView selected text

UIMenuController and UITextView selected text


By : user2428064
Date : March 29 2020, 07:55 AM
Does that help hmm you need two corrections. The first is:
you have to implement the UIResponder method like this in your custom UITextView class.
code :
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {

    if (action == @selector(doSomethingHere:) || action == @selector(copy:)) {

        if (self.selectedRange.length > 0) {

            return YES;
        }
    }
    return NO;
}
if (action == @selector(doSomethingHere:) || action == @selector(copy:))
if (action == @selector(doSomethingHere:))
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {

    return YES;
}
UIMenuItem *menuItem;
menuItem = [[UIMenuItem alloc] initWithTitle:@"Add note" action:@selector(doSomethingHere:)];
[items addObject:menuItem];

[[UIMenuController sharedMenuController] setMenuItems:@[menuItem]];
UITextview selected text

UITextview selected text


By : user3088378
Date : March 29 2020, 07:55 AM
it helps some times Did you try setting textView.selectedTextRange = nil?
Edit: Depending on your requirements, could you set this in viewWillDisappear or viewWillAppear so it is deselected when you either leave or come back to your view controller?
Get selected text in a UITextView

Get selected text in a UITextView


By : Amanda Paiola
Date : March 29 2020, 07:55 AM
Does that help What is happening is that UITextView method textInRange have been renamed to text(in: Range) since Swift 3. Btw you forgot to add the let keyword in your sentence:
code :
if let range = textView.selectedTextRange {
    UIPasteboard.general.string = textView.text(in: range)
}
shadow
Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk