title: Set the maximum character length of a UITextField in Swift
body: <p>I know there are other topics on this, but I can't seem to find out how to implement it.</p>
  <p>I'm trying to limit a UITextField to only five characters.</p>
  <p>Preferably alphanumeric, <code>-</code>, <code>.</code>, and <code>_</code>.</p>
  <p>I've seen this code:</p>
  <pre><code>func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange,
                         replacementString string: String) -&gt; Bool
  {
      let maxLength = 4
      let currentString: NSString = textField.text
      let newString: NSString =
               currentString.stringByReplacingCharactersInRange(range, withString: string)
      return newString.length &lt;= maxLength
  }
  </code></pre>
  <p>and</p>
  <pre><code>func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -&gt; Bool {
  
      let length = count(textField.text.utf16) + count(string.utf16) - range.length
      return length &lt;= 10
  }
  </code></pre>
  <p>How can I actually implement it? Which &quot;textfield&quot; should I swap out for my custom named UITextField?</p>