1

i am doing a reader application in IPAD ,the book data is stored in Sqlite DB,i render this content of DB in a view through core text...the formate of my text is like this e.g.:1 haii this is iPhone. 2 this is iPad. 3 this is mac. etc etc..i just want to get touch events while touching the text.for example if i tap the first sentence,,the fist sentence only must be selected with any color,and trigger a popup(subview or alert view or action sheet)like the image belowenter image description here

i am having this code to make the text to shown in a view

NSMutableString *combined = [NSMutableString string];

    for(NSUInteger idx = 0; idx < [delegatee.allSelectedVerseEnglish count]; idx++) {
        [combined appendFormat:@"  %d %@", 
         idx + 1, 
         [delegatee.allSelectedVerseEnglish objectAtIndex:idx]];

    }

    self.multiPageView.text =combined;

combined is the string contains verses oif the bible of the selected chapter,so when i click the first verse it need to be selected like the image below and pops up a subview or alert view like the image below,and by selection that verse must be stored in somewhere or copy to clipboard.multiPageView is the view that render the core text .i want just like in olive tree bible application or ibook.

i am not using the web view,due to some limitation in the web view..is there any idea how to do this..,please help me. Thanks in advance. EDIT:

- (void)textViewDidEndEditing:(UITextView *)textView
{

    mainpopupview.frame =CGRectMake(0, 0, 768, 1004)    ;
    [self.view addSubview:mainpopupview];

    NSRange selectedRange = [textView selectedRange];
    NSString *backString = [maintextview.text substringToIndex:selectedRange.location];
    NSRange backRange = [backString rangeOfString:@"." options:NSBackwardsSearch];
    NSRange backRangee = [backString rangeOfString:@"." options:NSBackwardsSearch];
    int  myRangeLenght = backRangee.location - backRange.location;
    NSRange myStringRange = NSMakeRange (backRange.location, myRangeLenght);
    NSString *forwardString  = [maintextview.text substringFromIndex:backRange.location];
    NSLog(@"%@",[[forwardString componentsSeparatedByString:@"."] objectAtIndex:1]);
    NSLog (@"%@",  [maintextview.text substringWithRange:myStringRange]);

    NSString * myStringTxt = [[forwardString componentsSeparatedByString:@"."] objectAtIndex:1];
    NSLog(@"1 %@", myStringTxt);

      //  maintextview.textColor = [UIColor yellowColor];

    NSRange myStringRangee = [maintextview.text rangeOfString:myStringTxt];
    [maintextview select:self];
    maintextview.selectedRange = myStringRangee;
    }

myStringTxt contains the correct verse(menz text between .to .but i want to elect this text.

stackiphone
  • 1,245
  • 3
  • 19
  • 41

1 Answers1

2

I think you can wire up UITextview's UIControlEventEditingChanged to some method. So when ever the user selects something that method would be called. Inside the method you can obtain the selected text using,

NSRange range = [txtView selectedRange];
NSString *str = [txtView.text substringWithRange:range];

From the selected string you can perform the actions you like.

If your text view is editable you can get,

NSLog(@"%d",range.location);

Once you know the location you can create a range and select string. This and this has relevant info.

//Here is the complete sample code.

  {
        sampleTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 60, 300)];
        sampleTextView.text = @"This is sample text to find text under touch. I am not going to use any private api.How am i supposed to do?.Let me see.";
        sampleTextView.editable = YES;
        sampleTextView.delegate = (id)self;
        [self.view addSubview:sampleTextView];
    }
- (void)textViewDidBeginEditing:(UITextView *)textView
{

    [NSTimer scheduledTimerWithTimeInterval:0.001 target:sampleTextView   selector:@selector(resignFirstResponder) userInfo:nil repeats:NO];
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    NSRange selectedRange = [textView selectedRange];
    NSString *backString = [sampleTextView.text substringToIndex:selectedRange.location];
    NSRange backRange = [backString rangeOfString:@" " options:NSBackwardsSearch];
    NSString *forwardString  = [sampleTextView.text substringFromIndex:backRange.location];
    NSLog(@"%@",[[forwardString componentsSeparatedByString:@" "] objectAtIndex:1]);
}

EDIT :

I have updated the code based on your requirement. Please find it below,

UITextView *textview;
BOOL toggle;

- (void)viewDidLoad
{
    [super viewDidLoad];

    textview = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
    textview.delegate = (id)self;
    textview.inputView = [[UIView alloc] init];
    textview.text = @"Let's hope the text selection works programmatically!";
    [self.view addSubview:textview];
}

- (void)textViewDidChangeSelection:(UITextView *)textView
{    
    NSRange selectedRange = [textView selectedRange];
    NSString *backString = [textView.text substringToIndex:selectedRange.location];
    NSRange backRange = [backString rangeOfString:@" " options:NSBackwardsSearch];
    NSString *forwardString  = [textView.text substringFromIndex:backRange.location];

    NSLog(@"%@",[[forwardString componentsSeparatedByString:@" "] objectAtIndex:1]);

    NSRange ran = [textView.text rangeOfString:[[forwardString componentsSeparatedByString:@" "] objectAtIndex:1]];

    if (!toggle)
    {
        [self performSelector:@selector(selectText:) withObject:[NSValue valueWithRange:ran] afterDelay:0.001];
    }
    else
    {
        toggle = !toggle;
    }
}

-(void)selectText:(NSValue *)sender
{
    toggle = !toggle;

    textview.selectedRange = [sender rangeValue];
}
Community
  • 1
  • 1
Vignesh
  • 10,205
  • 2
  • 35
  • 73
  • haii thanks for your response,,i didn't understand what u said...i ahem tons of sentences ,its a bible app,so if the user tap any sentence,,it want to highlight that sentence only and trigger a action,,like the above image.. – stackiphone Apr 24 '12 at 08:17
  • OK. Actually there are two parts in your question.1) Selecting the text 2)bring popover on selection. I have addressed only the second part. Let me try the first part. – Vignesh Apr 24 '12 at 08:20
  • ,i tried it,but it say my view controller oenott support selected range,becz am using core text which render text to a view controller.any idea?> – stackiphone Apr 29 '12 at 08:18
  • haii vigneshh,i changes my core text framework and instead of that i put UITextview ,your code works for me..but some doubts there. – stackiphone May 21 '12 at 09:28
  • @stackiphone. Glad it is of some help!. – Vignesh May 22 '12 at 12:10
  • how can i calculate the string range from the above code?i got the correct verse when i tapped,but i need the selection also for the tapped verse. – stackiphone May 22 '12 at 13:01
  • can u please please help me to solve this issue?i tried my maximum but no luck.didnt get the selection of the tapped verese.i will give you 100 bounty for it – stackiphone May 22 '12 at 13:03
  • @stackiphone. No prob about bounty.I don answer for that:) I am little busy. Thats'y not replying promptly. Let me know the issue. I will try !. – Vignesh May 22 '12 at 13:12
  • ohh thats great.i am getting the correct verse when i tapped ,just change you this line of code NSRange backRange = [backString rangeOfString:@" " options:NSBackwardsSearch]; to NSRange backRange = [backString rangeOfString:@"." options:NSBackwardsSearch]; but i want to select that verse like the default opt select of textview. – stackiphone May 22 '12 at 13:20
  • if you became free,plz look into my edited portion of the question,i tried that much with the help of your code. – stackiphone May 22 '12 at 13:25
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11618/discussion-between-stackiphone-and-vignesh) – stackiphone May 23 '12 at 05:59
  • ,i tried lott,but no luck textview dideditend didn't seems to be work,but there is in built nsselect range in EGOTextview.i think its possible in EGO ,but due to my lack of deep knlowdge i am not able to do it. – stackiphone May 23 '12 at 11:40
  • ,i am trying with EGOTextView,it has the selection highlighting property also,we can change any color to highlight the selection,but it only select the single word. – stackiphone May 23 '12 at 12:37
  • new solution highlight too.try that. – Vignesh May 23 '12 at 12:39
  • ohh you given anew one ,i dint noticed it.thats let me try – stackiphone May 23 '12 at 12:39
  • i give you 100 bounty,if the solution works or not,becz i am getting the verse correctly becz of your code.let me chk – stackiphone May 23 '12 at 12:40
  • @stackiphone. Ha, you gave it?.Thanks:) . – Vignesh May 23 '12 at 12:48
  • i have doubt,where id i put this code UITextView *textview; BOOL toggle;?my textview is maintextview.so is it necessary to add this code UITextView *textview;. – stackiphone May 23 '12 at 12:54
  • brillient,,this is brilliancy.i never forgot this help....so you deserves that bounty..thanks man – stackiphone May 23 '12 at 13:00
  • @stackiphone. Glad it is of some help!:) – Vignesh May 23 '12 at 17:18
  • not some help,it helps me lotto...now searching for dismissing of the keyboard...from thgis view,the selection is ok,but the keyboard is there in the view.hehehe.i just ask a question about this now. – stackiphone May 23 '12 at 17:21
  • @stackiphone. Just saw it!. Let me explain. – Vignesh May 23 '12 at 17:23