objective-c - 在 iOS 8 的自定义键盘中使用 deleteBackward 删除整个单词

标签 objective-c keyboard ios8

我开始使用 iOS8 和 Objective-C 开发自定义键盘。我想创建一个删除按钮,在插入点,单击时删除单个字符,并在按住时删除整个单词。 deleteBackward 似乎删除了单个字符,但在我发现的 Apple 开发人员文档中:

To let you determine how much text is appropriate to delete when you call the deleteBackward method, obtain the textual context near the insertion point from the documentContextBeforeInput property of the textDocumentProxy property, as follows:

NSString *precedingContext = self.textDocumentProxy.documentContextBeforeInput;

You can then delete what you determine to be appropriate—for example, a single character, or everything back to a whitespace character.

但我不明白如何实现...有没有办法删除一个完整的单词?

编辑:我找到了解决方案...这是对 documentContextBeforeInput 的简单标记化:

-(void)pressDeleteKey{
NSArray *tokens = [self.textDocumentProxy.documentContextBeforeInput componentsSeparatedByString:@" "];
for (int i = 0; i < [[tokens lastObject] length];i++) {
    [self.textDocumentProxy deleteBackward];
}

最佳答案

Swift 1.2 解决方案:

extension String {

    var length : Int {
        get{
            return count(self)
        }
    }  
}


func deleteLastWord() {

        if let textArray:[String]? = proxy.documentContextBeforeInput?.componentsSeparatedByString(" "){
        if let validArray = textArray{
            for var i=0; i<validArray.last!.length; i++ {
                            proxy.deleteBackward()
                        }
                }
        }
}

关于objective-c - 在 iOS 8 的自定义键盘中使用 deleteBackward 删除整个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26362968/

相关文章:

ios7 - 在 Xcode6 中针对 iOS7.1 或 7.0 时,应用程序中出现黑条

objective-c - iOS:将图像转换为 PNG 以允许保存

iphone - 非常非常持久的无效权利错误

angular - 带有 ion 项目的 ionic 3 应用程序中键盘无法第二次打开

ios - 在 iOS 8 键盘中选择时检测笑脸和地球仪

ios - 动态调整 UITableViewCell 大小

iphone - 在导航栏上方添加 View

ios - 崩溃 : com. apple.root.default-qos SIGABRT ABORT

c++ - 如何在 OSX 上使用 C++ 中的 CGEventPost 保持按键按下?

ios - 为什么我没有在 tableView 中获得不可见的节标题 View ?