iPhone:禁用“双击空格键”。捷径?

时间:2010-04-05 01:45:59

标签: iphone cocoa-touch

默认情况下,如果您在iPhone或iPad上点按两次空格键,而不是“”(两个空格),则会得到“。”(句点后面跟一个空格)。有没有办法在代码中禁用此快捷方式?

更新:通过UITextInputTraits禁用自动更正不起作用。

更新2:明白了!请参阅下面的帖子。

9 个答案:

答案 0 :(得分:12)

我的回答基于上面的Chaise给出的答案。

Chaise的方法不允许您按顺序键入两个空格 - 在某些情况下这是不可取的。这是一种完全关闭自动周期插入的方法:

夫特

在委托方法中:

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    //Ensure we're not at the start of the text field and we are inserting text
    if range.location > 0 && text.count > 0
    {
        let whitespace = CharacterSet.whitespaces

        let start = text.unicodeScalars.startIndex
        let location = textView.text.unicodeScalars.index(textView.text.unicodeScalars.startIndex, offsetBy: range.location - 1)            

        //Check if a space follows a space
        if whitespace.contains(text.unicodeScalars[start]) && whitespace.contains(textView.text.unicodeScalars[location])
        {
            //Manually replace the space with your own space, programmatically
            textView.text = (textView.text as NSString).replacingCharacters(in: range, with: " ")

            //Make sure you update the text caret to reflect the programmatic change to the text view
            textView.selectedRange = NSMakeRange(range.location + 1, 0)

            //Tell UIKit not to insert its space, because you've just inserted your own
            return false
        }
    }

    return true
}

现在,您可以根据需要尽可能快地点击空格键,只插入空格。

目标C

在委托方法中:

- (BOOL) textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text

添加以下代码:

//Check if a space follows a space
if ( (range.location > 0 && [text length] > 0 &&
      [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[text characterAtIndex:0]] &&
      [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[[textView text] characterAtIndex:range.location - 1]]) )
{
    //Manually replace the space with your own space, programmatically
    textView.text = [textView.text stringByReplacingCharactersInRange:range withString:@" "];

    //Make sure you update the text caret to reflect the programmatic change to the text view
    textView.selectedRange = NSMakeRange(range.location+1, 0);  

    //Tell Cocoa not to insert its space, because you've just inserted your own
    return NO;
}

答案 1 :(得分:4)

将它放在你的委托类中:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

 //Check for double space
 return !(range.location > 0 && 
          [string length] > 0 &&
          [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[string characterAtIndex:0]] &&
          [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[[textField text] characterAtIndex:range.location - 1]]);

}

答案 2 :(得分:1)

发布的另一个版本的simeon(这是Chaise的版本)。这个适用于文本字段(UITextField)。您必须设置UITextFieldDelegate才能执行此操作。我注释了更新插入符号的行,但它似乎仍然有用。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)text
{
    //Check if a space follows a space
    if ( (range.location > 0 && [text length] > 0 &&
          [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[text characterAtIndex:0]] &&
          [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[[textField text] characterAtIndex:range.location - 1]]) )
    {
        //Manually replace the space with your own space, programmatically
        textField.text = [textField.text stringByReplacingCharactersInRange:range withString:@" "];

        //Make sure you update the text caret to reflect the programmatic change to the text view
//      textField.selectedTextRange = NSMakeRange(range.location+1, 0);  

        //Tell Cocoa not to insert its space, because you've just inserted your own
        return NO;
    }
    return YES;
}

答案 3 :(得分:1)

我发现适用于UITextView的更简单的解决方案如下:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@". "] && range.length == 1) {
        NSMutableAttributedString *attributedString = [textView.attributedText mutableCopy];
        [attributedString replaceCharactersInRange:range withString:@" "];
        textView.attributedText = attributedString;
        textView.selectedRange = NSMakeRange(range.location + 1, 0);

        return NO;
    }
    return YES;
}

这允许重复输入多个空格并处理属性文本。我发现它失败的唯一情况是粘贴一个已选择单个字符的句点和空格。

答案 4 :(得分:1)

这是Swift 4.0中针对文本字段的一种实现,复制了Simeon的答案:

func textField(_ textField: UITextField, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    //Ensure we're not at the start of the text field and we are inserting text
    if range.location > 0 && text.count > 0{
        let whitespace = CharacterSet.whitespaces
        //get list of whitespace characters

        let start = text.unicodeScalars.startIndex
        if let textFieldText = textField.text{
            let location = textFieldText.unicodeScalars.index(textFieldText.unicodeScalars.startIndex, offsetBy: range.location - 1)

            //Check if a space follows a space
            if whitespace.contains(text.unicodeScalars[start]) && whitespace.contains(textFieldText.unicodeScalars[location]){

                //Manually replace the space with your own space, programmatically
                textField.text = (textFieldText as NSString?)?.replacingCharacters(in: range, with: " ")

                if let pos = textField.position(from: textField.beginningOfDocument, offset: range.location + 1)
                {
                    //Make sure you update the text caret to reflect the programmatic change to the text view
                    textField.selectedTextRange = textField.textRange(from: pos, to: pos)


                    //Tell UIKit not to insert its space, because you've just inserted your own
                    return false
                }
            }
        }
    }
    return true
}

希望这会有所帮助!

编辑:在底部添加了一个丢失的return语句。

答案 5 :(得分:1)

这是我可以在Swift 4中解决此问题的最简单的解决方案。 它比其他一些答案更完整,因为它允许连续输入多个空格。

func disableAutoPeriodOnDoubleTapSpace() {
    textField.addTarget(self, action: #selector(replaceAutoPeriod), for: .editingChanged)
}

@objc private func replaceAutoPeriod() {
    textField.text = textField.text.replacingOccurrences(of: ". ", with: "  ")
}

如果您的文本字段使用.attributedText格式设置,则需要先存储旧的.selectedTextRange,然后在设置.text值后将其重置。否则,在字符串中间进行编辑时,光标将移动到文本的结尾。

希望这对那些没有运气尝试过所有其他答案的人有所帮助!

答案 6 :(得分:1)

Swift 5

我在仔细管理的 textField 输入上下文中尝试允许“”和“-”但不允许“。”时发现了这个问答树。也不是由双空格或双-引起的“-”。

因此,我对委托函数进行了如下简化和修改:

criteria.and(new Criteria("FieldName").expression("*" + SEARCH_TEXT_WITH_SPACE + "*"));

答案 7 :(得分:-2)

我认为没有办法完全关闭此功能,但请查看UITextInputTraits。这允许您声明字段的属性,例如,您可以说它是否用于输入URL。这会影响键盘的行为。如果您不希望双倍空间产生句点和空间的原因是文本应该是用户出于某种原因输入的字面意思,也许您想要关闭自动更正。我不知道关闭自动校正是否会关闭双倍空间。

答案 8 :(得分:-4)

好的,我明白了。在您的UITextView委托中,添加以下内容:

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if([text isEqualToString:@". "])
        return NO;
}