I'm using the below code to detect words tapped in a UITextView. This works fine, but I want to detect some special characters, like a ?. ? doesn't show up as part of a word when using UITextGranularityWord and I can't seem to get it to show up when using UITextGranularityCharacter either.
How can I detect taps on single special characters such as the ??
-(NSString*)getWordAtPosition:(CGPoint)pos inTextView:(UITextView*)_tv { //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]; if ([_tv textInRange:wr].length == 0) {//i.e. it's not a word NSLog(@"is 0 length, check for characters (e.g. ?)"); UITextRange *ch = [_tv.tokenizer rangeEnclosingPosition:tapPos withGranularity:UITextGranularityCharacter inDirection:UITextLayoutDirectionRight]; NSLog(@"ch range: %@ ch text: %@",ch, [_tv textInRange:ch] ); // logs: ch range: (null) ch text: if ([[_tv textInRange:ch] isEqualToString:@"?"]) { return [_tv textInRange:ch]; } } return [_tv textInRange:wr]; }
?, how can I useNSCharacterSet? I don't want to check if a?exists in the string, but only if it exists in the location that the user taps.foo?becomesfooor that are by themselves?. It just returns an empty NSRange and string. I'm using the GM, perhaps this is a bug?