IPhone: turn off the "space" for double-tap. label?

By default, if you double-tap a space on your iPhone or iPad, instead of getting a “” (two spaces), you get a “.” (Period followed by a space). Is there a way to disable this shortcut in code?

Update: Disabling auto-correction using UITextInputTraits does not work.

Update 2: it worked! See my post below.

+8
iphone cocoa-touch
source share
8 answers

OK, I get it. In your UITextView, add the following:

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

I have an answer based on what is given above in Chaise.

The Chaise method does not allow you to enter two spaces in series - this is undesirable in some situations. Here you can completely disable the insertion of an automatic period:

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 press the spacebar as fast as you like by 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; } 
+12
source share

Put this in your delegate class:

 - (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]]); } 
+4
source share

Another version of one Simeon (it was Chaise's version). This works with text fields ( UITextField ). You will need to configure UITextFieldDelegate for this to do something. I commented out the line to update the carriage, but it still works.

 - (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
source share

A simpler solution that I found for a UITextView to work is the following:

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

This allows you to enter multiple spaces, as well as handle the text attribute. The only case that I encountered an error was to insert a period and a space with a character already selected.

+1
source share

This is an implementation in Swift 4.0 for text fields, copying Simeon's answer:

 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 } 

Hope this helps!

EDIT: added missing return statement at the bottom.

+1
source share

This is the simplest solution that I could solve for this problem in Swift 4. It is more complete than some other answers, because it allows you to enter several spaces in a line.

 func disableAutoPeriodOnDoubleTapSpace() { textField.addTarget(self, action: #selector(replaceAutoPeriod), for: .editingChanged) } @objc private func replaceAutoPeriod() { textField.text = textField.text.replacingOccurrences(of: ". ", with: " ") } 

If your text field is formatted using .attributedText, you will need to save the old .selectedTextRange before and reset it after setting the .text value. Otherwise, your cursor will move to the end of the text when editing in the middle of the line.

Hope this helps someone who has tried all the other answers without luck!

+1
source share

I don't think there is a way to disable this, but check out UITextInputTraits . This allows you to declare the properties of your field, for example, you can tell whether it needs to enter a URL. This affects keyboard behavior. If the reason you do not want double space to give a period and space is because the text should be literally entered by the user for some reason, you might want to turn off auto-correction. Perhaps disabling auto-correction disables double space over a period, I don’t know.

-2
source share

All Articles