这个是将昵称控制在20个字符内
//计算字符
- (int)convertToInt:(NSString*)strtemp {
int strlength = 0;
char *p = (char *)[strtemp cStringUsingEncoding:NSUnicodeStringEncoding];
for (int i=0 ; i<[strtemp lengthOfBytesUsingEncoding:NSUnicodeStringEncoding] ;i++) {
if (*p) {
p++;
strlength++;
}else {
p++;
}
}
return strlength;
}
昵称截取
-(NSString *)nameSubstring:(NSString *)name{
do {
int number = [self convertToInt:name];
int len = (number - 20)/2;
if (len == 0) {
len = 1;
}
name = [name substringToIndex:name.length - len];
} while ([self convertToInt:name] > 20);
NSString *nameStr = [NSString stringWithFormat:@"%@...",name];
return nameStr;
}
网友评论