我想在点击时选择特定句子。我有段落由许多句子组成,每个句子都有一个索引,当我点击它时,我必须显示并突出显示整个句子(诗句)。我已经提取了所需的NSString
以突出显示如下:
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)];
}