【问题标题】:Select only whole words uiTextView仅选择整个单词 uiTextView
【发布时间】:2014-02-03 00:03:58
【问题描述】:

我目前正在实施翻译应用程序。当用户从 UITextView 中选择一个单词时,应用程序会翻译成另一种语言。

我想将选择选项限制为仅整个单词,例如:

“Hello World”而不是“Hello Wo”

在字典中定义单词时类似于 iOS Kindle 应用程序。

我的 .h 文件中有 UITextViewDelegate。

我目前在我的 .m 中:

- (void)textViewDidChangeSelection:(UITextView *)textView{
    [self translate];
}

- (void)translate {

    if(![[self.descriptionTextView selectedTextRange] isEmpty]) {

        NSString * selectedWord = [self.descriptionTextView textInRange:[self.descriptionTextView selectedTextRange]];

 ...

然后我继续将其转换为编码字符串并启动 Google 翻译 API。有没有办法只从 UITextView 中选择整个单词?以便选择以整个单词块递增。

【问题讨论】:

  • 我不知道是否可以在全字上强制选择,但是从那里去的一种方法是使用正则表达式来查找以''开头的世界块,结束于一个 ' ', '。', '?'或其他标记,并包含选定的文本...然后您将确保只有完整的单词。我将其保留为评论,以防出现更好的答案
  • 您始终可以获取选择的范围,并检查 range-1 = ' ' 和 range+1 = ' ' 或标点符号('.',',','!' 等) .

标签: ios uitextview word selection


【解决方案1】:

您可以将自己添加为文本字段的inputDelegate,并实现selectionDidChange:。 When the selection changes you can use positionWithinRange:atCharacterOffset: to investigate around the selected range for white space.找到所需范围后,您可以更新selectedTextRange

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多