문제

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)];
}
도움이 되었습니까?

해결책

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

Hope, It will may helpful to you,

:)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top