我开始使用 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/