Please check it out: https://github.com/00StevenG/UITextViewExtras
Hope, It will may helpful to you,
:)
Question
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)];
}
Solution
Please check it out: https://github.com/00StevenG/UITextViewExtras
Hope, It will may helpful to you,
:)