Reputation: 1276
I'm currently implementing a translation application. One that when a user selects a word from a UITextView the application translates to a different language.
I would like to restrict the options for selection to only whole words for example:
"Hello World" and not "Hello Wo"
Similar to the iOS Kindle application when defining words in the dictionary.
I have the UITextViewDelegate in my .h file.
I currently have in my .m:
- (void)textViewDidChangeSelection:(UITextView *)textView{
[self translate];
}
and
- (void)translate {
if(![[self.descriptionTextView selectedTextRange] isEmpty]) {
NSString * selectedWord = [self.descriptionTextView textInRange:[self.descriptionTextView selectedTextRange]];
...
I then go on to convert this to an encodedString and fires off the Google Translate API. Is there a way to only select whole words from the UITextView? So that the selection increments in whole word chunks.
Upvotes: 0
Views: 990
Reputation: 119031
You can add yourself as the inputDelegate
of the text field and implement selectionDidChange:
. When the selection changes you can use positionWithinRange:atCharacterOffset:
to investigate around the selected range for white space. Once you've found your desired range you can update the selectedTextRange
.
Upvotes: 2