Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make UITextView text all selectable and show copy option to the user iPhone app?

Am working in iPhone application like iMessage native app. I have designed a page look like iMessage with added a bubbles also. How i done is, just added a UITextView in UITableView Cell and also i have added UIImageView on UITextView with bubble images.

Load the text to UITextView from NSMutableArray. It is working fine. Now my doubt is:

  1. In iMessage app they set that when the user hold the bubble they making the text as selectable and showing Copy option. In my app when the user hold a bubble the Copy option showing but some particular text only copying. How can i select all text and show copy option the user?

  2. In iMessage app the selecting region (two lines in beginning of selection and end of selection) not showing. But in my app the selection regions are showing how can i manage that?

Could you please help me to solve this issues? Thanks in advance.

like image 950
Yuvaraj.M Avatar asked Feb 20 '23 04:02

Yuvaraj.M


2 Answers

Finally i have solved my problem used below code. Now am able to select all content from UITextView and showing Copy option to the user to copy the message. Please find the code for your reference.

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(copy:))
    {
        [self selectAll:self];

        return YES;
    }
    else if (action == @selector(cut:))
    {
        return NO;
    } 
        return NO;
}


- (void)copy:(id)sender 
{
    UIPasteboard *pastBoard = [UIPasteboard generalPasteboard];
    [pastBoard setString:self.text];
    self.selectedTextRange = nil;
}

Happy Coding.

like image 78
Yuvaraj.M Avatar answered Feb 21 '23 17:02

Yuvaraj.M


Here, How you can select text in UITextView

[textView setSelectedRange:NSMakeRange(row, length)];

where, row indicates from which row you want to start your selection. length is total length of text.

e.g. [textView setSelectedRange:NSMakeRange(1, 45)]; // where 1 is first row, and 45 is length of text of selection.
like image 25
Hemang Avatar answered Feb 21 '23 17:02

Hemang