突然间想做一个弹幕效果Demo, 其中一个想法就是Label的长是 随着文字的长度而变化的。
在 代码中 当你用肉眼观察一个字母和一个汉字的时候,你会发现他俩的长度不一致。
NSString *a = @"a";
NSString *b = @"你";
于是乎你就认为汉字和字母的长度在机器的存储长度是不一样的。
那么,恭喜你,你就被你的眼睛欺骗了。
测试内容:
比较 大写字母 小写字母 汉字 数字 半角符号 全角符号 在 NSString中的长度
NSLog(@"\nA.length = %lu \nAA.length = %lu \na.length = %lu \naa.length = %lu \n0.length = %lu \n00.length = %lu \n我.length = %lu \n我我.length = %lu \n..length = %lu \n...length = %lu \n。.length = %lu \n。。.length = %lu \n——.length = %lu \n《.length = %lu",(unsigned long)@"A".length,(unsigned long)@"AA".length,(unsigned long)@"a".length,(unsigned long)@"aa".length,(unsigned long)@"0".length,(unsigned long)@"00".length,(unsigned long)@"我".length,(unsigned long)@"我我".length,(unsigned long)@".".length,(unsigned long)@"..".length,(unsigned long)@"。".length,(unsigned long)@"。。".length,(unsigned long)@"——".length,(unsigned long)@"《".length);
测试结果:
结论:
1. 大、小写字母;汉字;数字;普通半角符号;普通全角符号 长度 在 NSString中的长度一致 , 都为 1
2. 全角符号中"——" 长度为 2