title: How to restrict double/single space for TextField in Swift 3 iOS
body: <p>I have login page in my iOS app which is developing by Swift language.
  In that first letter should not be with single/double space and after user enters text it should allow single space, not allow double space and need to set the limit for text length. </p>
  
  <p>I want to restrict the user while tapping single/double space while starting time entering text and need to allow single space after enters first letter in textfield</p>
  
  <p>I am able to do either one of single/double, but not working as per my requirement.</p>
  
  <pre><code>func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -&gt; Bool {
      //for double space restrict
      if (range.location &gt; 0 &amp;&amp; (textField.text?.characters.count)! &gt; 0) {
          //Manually replace the space with your own space, programmatically
          //Make sure you update the text caret to reflect the programmatic change to the text view
          //Tell Cocoa not to insert its space, because you've just inserted your own
          return false
      }else {
          if textField.tag == 1 || textField.tag == 2 { //restrict text length
              guard let text = textField.text else { return true }
              let newLength = text.characters.count + string.characters.count - range.length
              return newLength &lt;= limitLengthForTextField
          }
      }
  
      return true 
  }
  </code></pre>
  
  <p>Can anyone give suggestions. Thanks</p>