CCLabelTTF 显示不全

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;
}


修改后结果:现在已是凌晨两点收工。。。。。。。






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值