How to set a limit for a textbox ?

How to set a limit for a textbook ?
I created a textbox and want the user to have a maximum number of characters that can be written.

You can do it this way:


func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool

{

var newLength = textView.text.length + text.length - range.length;

  let currentCharacterCount = textField.text?.characters.count ?? 0

     if (newLength > MAX_LIMIT)

     { return false }


//do your code


     return true;

}

You have to be very careful when dealing with text; many things that seem obvious just aren’t correct. Consider this code snippet:

let s1 = "e"
let s2 = "\u{0301}"        // U+0301 COMBINING ACUTE ACCENT
let s3 = s1 + s2
print(s1.characters.count)  // prints "1"
print(s2.characters.count)  // prints "1"
print(s3.characters.count)  // prints "1"

One plus one actually equals one; it’s almost Orwellian! This Swift blog post explains some of the background here.

You also have to worry about the differences between NSRange and Swift string indexes. I posted about this last week.

@Giovanni Martins., there’s no way to answer your question without you defining what you mean by character. Presumably you have to limit the number of characters in the field because some ‘back end’ code you’re talking to has an equivalent limit. How does that back end code enforce that limit?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

WWDC runs Mon, 13 Jun through to Fri, 17 Jun. During that time all of DTS will be at the conference, helping folks out face-to-face. http://developer.apple.com/wwdc/

Define o delegate do

UITextField
pra um objeto teu e implementa o método
textField shouldChangeCharactersInRange
.

Exemplo:

class ViewController: UIViewController, UITextFieldDelegate {

     let maxCharCount = 3

     @IBOutlet weak var textField: UITextField! 
     
     override func viewDidLoad() { 
          super.viewDidLoad()
          self.textField.delegate = self 
          } 


     func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { 
          return textField.text!.characters.count + string.characters.count <= self.maxCharCount 
     } 
}


Esse código não trata o caso do usuário colar um texto no textfield que passa do número máximo (do jeito que está, ele não vai deixar), mas já dá pra ter uma ideia de como fazer.

That's not going to work because you're ignoring all of the important string modification cases. For example, deletions are handled by specifying a range in the string that should be replaced by "".


To handle that, what you have to instead is:

String startingText = textField.text;

String newText = startingText.stringByReplacingCharactersInRange(range, withString: string);

and then check the length of newText.

To handle that, what you have to instead is … and then check the length of newText.

Right. Except that you still need to define what you mean by ‘length’ (-:

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

WWDC runs Mon, 13 Jun through to Fri, 17 Jun. During that time all of DTS will be at the conference, helping folks out face-to-face. http://developer.apple.com/wwdc/

How to set a limit for a textbox ?
 
 
Q