Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone: Disable the "double-tap spacebar for ." shortcut?

By default, if you tap the spacebar twice on the iPhone or iPad, instead of getting "  " (two spaces), you get ". " (a period followed by a space). Is there any way to disable this shortcut in code?

Update: Disabling autocorrection via UITextInputTraits doesn't work.

Update 2: Got it! See my post below.

like image 675
igul222 Avatar asked Apr 05 '10 01:04

igul222


2 Answers

I have an answer based on the the one given by Chaise above.

Chaise's method does not allow you to type two spaces in sequence - this is not desirable in some situations. Here's a way to completely turn off the auto-period insert:

Swift

In the delegate method:

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
}

Now you can tap the spacebar as much and as fast as you like, inserting only spaces.

Objective-C

In the delegate method:

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

Add the following code:

//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;
}
like image 76
simeon Avatar answered Oct 16 '22 16:10

simeon


Swift 5

I found this question and answer tree while trying to allow " " and "-" in a carefully managed textField input context, but not allowing "." nor "–" caused by double space or double -.

So, I simplified and modified the delegate func as follows:

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if textField.text?.last == " " && string == " " {
            textField.text = (textField.text as NSString?)?.replacingCharacters(in: range, with: " ")
            if let pos = textField.position(from: textField.beginningOfDocument, offset: range.location + 1) {
            // updates the text caret to reflect the programmatic change to the textField
                textField.selectedTextRange = textField.textRange(from: pos, to: pos)
                return false
            }
        }
        if textField.text?.last == "-" && string == "-" {
            textField.text = (textField.text as NSString?)?.replacingCharacters(in: range, with: "-")
            if let pos = textField.position(from: textField.beginningOfDocument, offset: range.location + 1) {
            // updates the text caret to reflect the programmatic change to the textField
                textField.selectedTextRange = textField.textRange(from: pos, to: pos)
                return false
            }
        }
        return true
    }
like image 43
Peter Brockmann Avatar answered Oct 16 '22 15:10

Peter Brockmann