2

I want to select specific sentence on tap.I have paragraph composed of many sentences each sentence has an index and I have to show and highlight the whole sentence(verse) when I tap on that. I have fetched the required NSString to highlight like this:

CGPoint pos = [tap locationInView:self.textView];

//get location in text from textposition at point
UITextPosition *tapPos = [self.textView closestPositionToPoint:pos];

//fetch the word at this position (or nil, if not available)
UITextRange * wr = 
  [self.textView.tokenizer rangeEnclosingPosition:tapPos
                                  withGranularity:UITextGranularitySentence
                                      inDirection:UITextLayoutDirectionRight];

//fetch the word at this position (or nil, if not available)
UITextRange * wl = 
  [self.textView.tokenizer rangeEnclosingPosition:tapPos
                                  withGranularity:UITextGranularitySentence
                                      inDirection:UITextLayoutDirectionLeft];

if ([wr isEqual:wl]) {

    NSLog(@"WORD: %@", [self.textView textInRange:wr]);       
    NSString *infoString = [self.textView textInRange:wr];
    NSMutableAttributedString *attString =
      [[NSMutableAttributedString alloc] initWithString:infoString];
    NSInteger _stringLength = [infoString length];

    UIColor *_black = [UIColor blackColor];
    UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" 
                                   size:30.0f];
    [attString addAttribute:NSFontAttributeName 
                      value:font 
                      range:NSMakeRange(0, _stringLength)];
    [attString addAttribute:NSForegroundColorAttributeName 
                      value:_black 
                      range:NSMakeRange(0, _stringLength)];
}
Alex Cio
  • 6,014
  • 5
  • 44
  • 74

1 Answers1

0

Please check it out: https://github.com/00StevenG/UITextViewExtras

Hope, It will may helpful to you,

:)

Gaurav
  • 299
  • 4
  • 15
  • its good..but the problem is that it selects the whole row..i have arabic text and i have to highlight the verse (sentence) from its starting point to ending point.. – Umair Tariq Mar 04 '14 at 12:00
  • @UmairTariq : Hi, i m making this same thing. I also have to do the same thing. Highlight the whole verse. Is this done? Can you please help me out here? – try catch finally Jun 09 '14 at 12:50
  • @iOSDev i could not done that by highlighting the text.. i have done it by highlighting the part of image by getting coordinates.. – Umair Tariq Jun 11 '14 at 05:00