I have a UITextField into which characters will be entered. It is as simple as I can return its actual length? When a string contains AZ 1-9 characters, it works as expected, but any emojis or special characters get a double count.
It has the simplest format, it just has 2 characters for some special characters, such as emoji:
NSLog(@"Field '%@' contains %i chars", myTextBox.text, [myTextBox.text length] );
I tried scrolling every character with characterAtIndex , substringFromIndex , etc. and didnโt get anywhere.
According to the answer below, the exact code is used to count the characters (I hope this is the correct approach, but it works ..):
NSString *sString = txtBox.text; __block int length = 0; [sString enumerateSubstringsInRange:NSMakeRange(0, [sString length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { length++; }]; NSLog(@"Total: %u", length );
objective-c uitextfield special-characters nsstring
Ralphonzo
source share