OC计算字节长度

对于文本框的输入字数限制

1
2
3
4
5
6
7
8
9
10
11
- (NSUInteger) unicodeLengthOfString: (NSString *) text {
NSUInteger asciiLength = 0;

for (NSUInteger i = 0; i < text.length; i++) {

unichar uc = [text characterAtIndex: i];

asciiLength += isascii(uc) ? 1 : 2;
}
return asciiLength;
}