Last active
December 19, 2017 12:26
-
-
Save prat14k/8f8b2b4304211662cc3512ae8325ef86 to your computer and use it in GitHub Desktop.
Objective-c Code to get word from a UITextView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(NSString *)getWordAtPosition:(CGPoint)pos inTextView:(UITextView*)_tv | |
{ | |
UITextView *tempTextview = [[UITextView alloc]init]; | |
tempTextview.text = temmp; | |
//eliminate scroll offset | |
pos.y += _tv.contentOffset.y; | |
//get location in text from textposition at point | |
UITextPosition *tapPos = [_tv closestPositionToPoint:pos]; | |
//fetch the word at this position (or nil, if not available) | |
UITextRange * wr = [_tv.tokenizer rangeEnclosingPosition:tapPos withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight]; | |
UITextPosition* beginningForTV = _tv.beginningOfDocument; | |
UITextPosition* selectionStart = wr.start; | |
UITextPosition* selectionEnd = wr.end; | |
UITextPosition *beginning; | |
UITextPosition *start; | |
UITextPosition *end; | |
UITextRange *textRange; | |
NSInteger location = [_tv offsetFromPosition:beginningForTV toPosition:selectionStart]; | |
NSInteger length = [_tv offsetFromPosition:selectionStart toPosition:selectionEnd]; | |
NSRange tter = NSMakeRange(location, length); | |
if (tter.length != 0) { | |
for (int i=0; i<hotwordsRanges.count; i++) { | |
NSValue *val = [hotwordsRanges objectAtIndex:i]; | |
NSRange range = [val rangeValue]; | |
int total = range.location+range.length; | |
if (total >= tter.location) { | |
beginning = tempTextview.beginningOfDocument; | |
start = [tempTextview positionFromPosition:beginning offset:range.location]; | |
end = [tempTextview positionFromPosition:start offset:range.length]; | |
textRange = [tempTextview textRangeFromPosition:start toPosition:end]; | |
// NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:self.textViewTag.text]; | |
// [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range]; | |
// self.textViewTag.attributedText = string; | |
// NSString *param = nil; | |
// if (range.location != NSNotFound) | |
// { | |
// param = [temmp substringFromIndex:range.location + range.length]; | |
// | |
// NSRange end = [param rangeOfString:@"%"]; | |
// // NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:self.textViewTag.text]; | |
// // [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range]; | |
// // self.textViewTag.attributedText = string; | |
// if (end.location != NSNotFound) | |
// { | |
// param = [param substringToIndex:end.location]; | |
// } | |
// } | |
return [tempTextview textInRange:textRange]; | |
break; | |
} | |
} | |
return[tempTextview textInRange:textRange]; | |
} | |
return [_tv textInRange:wr]; | |
} | |
// the textview will be the view triggering the gesture action | |
// the location will be th point of gesture action . Can get from : | |
// gestureSender.location(inView:) method | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment