iPhone:禁用“双击空格键for。”快捷方式?

问题描述 投票:8回答:8

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

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

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

iphone cocoa-touch
8个回答
12
投票

我有一个基于Chaise给出的答案。

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

Swift

在委托方法中:

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
}

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

Objective-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;
}

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]]);

}

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;
}

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;
}

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


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值后重置它。否则,在字符串中间进行编辑时,光标将移动到文本的末尾。

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


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
}

希望这可以帮助!

编辑:在底部添加了一个缺少的返回语句。


-2
投票

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


-4
投票

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

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if([text isEqualToString:@". "])
        return NO;
}
© www.soinside.com 2019 - 2024. All rights reserved.