标签:range pen UI nss 中文 character har string 符号
截取字符串长度。通常情况下,中文以及中文标点符号占2字节,ASIIC编码占1字节,如果规定字符长度不超过8位,可通过以下方法
- (NSString *)nameLength:(NSString *)name{ NSUInteger asciiLength = 0; NSMutableString *nameStr = [NSMutableString string]; for (NSUInteger i = 0; i < name.length; i++) { unichar uc = [name characterAtIndex: i]; asciiLength += isascii(uc) ? 1 : 2; NSString *strS = [name substringWithRange:NSMakeRange(i, 1)]; if (asciiLength <= 16) { [nameStr appendString:strS]; }else{ [nameStr appendString:@"..."]; return nameStr; } } return nameStr; }
标签:range pen UI nss 中文 character har string 符号
原文地址:https://www.cnblogs.com/jztsdwn/p/9087162.html