cocos2d版本:quick 2.2.6
测试字符串:abcd\n\n\nefg\n\n\nhijk
结果:如图hijk几个字母没有显示出来。
原因:是因为cocos2d计算文字高度的时候把文字以\n分割开来,然后计算每段文字在高度,如果\n\n连接起就行形成一个空字符,导致计算高度变小,所以在显示文字的时候hijk没有显示出来。
解决方法:修改这个方法计算文字高度,红色的是修改的。
CGSize tmp;
if (s.length == 0) {
tmp = [@"\n" sizeWithFont:font constrainedToSize:textRect];
}else{
tmp = [s sizeWithFont:font constrainedToSize:textRect];
}
static CGSize _calculateStringSize(NSString *str, id font, CGSize *constrainSize)
{
NSArray *listItems = [str componentsSeparatedByString: @"\n"];
CGSize dim = CGSizeZero;
CGSize textRect = CGSizeZero;
textRect.width = constrainSize->width > 0 ? constrainSize->width
: 0x7fffffff;
textRect.height = constrainSize->height > 0 ? constrainSize->height
: 0x7fffffff;
for (NSString *s in listItems)
{
CGSize tmp;
if (s.length == 0) {
tmp = [@"\n" sizeWithFont:font constrainedToSize:textRect];
}else{
tmp = [s sizeWithFont:font constrainedToSize:textRect];
}
if (tmp.width > dim.width)
{
dim.width = tmp.width;
}
dim.height += tmp.height;
}
dim.width = ceilf(dim.width);
dim.height = ceilf(dim.height);
return dim;
}
修改后结果:现在已是凌晨两点收工。。。。。。。