6

I am facing problem in my app that, When I press space bar twice, "."(dot) gets added in the TextField. I do not want to allow dot "." on double tap spacebar.

Note: I know that it is default behaviour of textField in iOS.

I refered the earlier answer iPhone: Disable the "double-tap spacebar for ." shortcut? but it is for textView, Not For textField.

I not getting replacement to this line

textView.selectedRange = NSMakeRange(range.location + 1, 0)
Community
  • 1
  • 1
Rohit Pradhan
  • 3,867
  • 1
  • 21
  • 29
  • 2
    Possible duplicate of [iPhone: Disable the "double-tap spacebar for ." shortcut?](http://stackoverflow.com/questions/2576561/iphone-disable-the-double-tap-spacebar-for-shortcut) – Vishnu gondlekar Jun 23 '16 at 05:44
  • @Vishnugondlekar if you can provide me for textfield version, it will be helpful. – Rohit Pradhan Jun 23 '16 at 05:49

4 Answers4

0

I have the same problem, then use the below delegate method, its was solved, use this code the dot not displayed in textfield,

swift Code:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        let newString = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string) as? NSString

        let arrayOfString = newString?.componentsSeparatedByString(".");
        if arrayOfString?.count > 1
        {
            return false
        }

        return true
    }

objective C code:

 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *newString1 = [textField.text stringByReplacingCharactersInRange:range withString:string];
    NSArray  *arrayOfString1 = [newString1 componentsSeparatedByString:@"."];

    if ([arrayOfString1 count] > 1 )
        return NO;

    return YES;
}

its working for me, hope its helpful

Iyyappan Ravi
  • 3,205
  • 2
  • 16
  • 30
  • 2
    please answer swift question with swift answers only. – Shubhank Jun 23 '16 at 05:51
  • func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { var newString1: String = textField.text!.stringByReplacingCharactersInRange(range, withString: string) var arrayOfString1: [AnyObject] = newString1.componentsSeparatedByString(".") if arrayOfString1.count > 1 { return false } return true } – HariKrishnan.P Jun 23 '16 at 06:02
  • just use converter to check it (objective c -swift) https://objectivec2swift.com/#/home/converter/ – HariKrishnan.P Jun 23 '16 at 06:03
  • ok But All the time its not correct code, check your above code in xcode, its show range error. – Iyyappan Ravi Jun 23 '16 at 06:09
0

Swift 4

final class RestrictedDoubleSpaceDotTextField: UITextField {

    override func awakeFromNib() {
        super.awakeFromNib()

        addTarget(self, action: #selector(didChangeText), for: .editingChanged)
    }

    @objc private func didChangeText() {
        let textWithoutDot = text?.replacingOccurrences(of: ".", with: " ", options: .literal, range: nil)
        text = textWithoutDot
    }
}
IvanPavliuk
  • 1,460
  • 1
  • 21
  • 16
-1

Change Keyboard settings from simulator/iPhone.

In iPhone Settings -> General -> Keyboard -> (Turn Off) "." shortcut

Pranit
  • 892
  • 12
  • 20
-2
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
  if (string == ". ") {
    return false
  }
  else {
    return true
  }
}
Amit Jagesha シ
  • 1,092
  • 12
  • 21