4

我正在使用一种名为 KomikaTitle 的自定义字体。在某些情况下,字体在第一个字符的左侧出现截断。当我使用 Arial 等原生字体时,不会发生这种情况。

以下是我正在使用的代码:

scoreDisplayLabel = [CCLabelTTF labelWithString:@"0" dimensions:CGSizeMake(200,30) hAlignment:UITextAlignmentLeft fontName:@"KomikaTitle" fontSize:18];
scoreDisplayLabel.color = (ccc3(r,b,g));
[self addChild:scoreDisplayLabel z:2];
[scoreDisplayLabel setPosition:ccp(115,wins.height-73)];

我该如何防止这种情况发生?我附上了这个问题的截图。

我尝试按照http://www.cocos2d-iphone.org/forums/topic/custom-font-being-cut-off/中的建议搞乱,但没有运气。

在此处输入图像描述

多谢你们!

4

3 回答 3

3

这可能不是一个真正的答案,但在我制作的一个旧 cocos2d 项目中,我遇到了同样的字体问题。只是添加了一个额外的空间和一行。

于 2013-06-27T08:56:37.577 回答
2

这可能相关也可能不相关,但根据此来源,您必须包含字体的文件扩展名。你在哪里

fontName:@"KomikaTitle"

它应该是

fontName:@"KomikaTitle.ttf"

例如。

于 2013-06-26T13:31:53.150 回答
0

如果有安卓用户使用 cocos2dx,这不一定是一个容易解决的问题,但是一旦你进入兔子洞,它是可行的。它确实需要编辑 Cocos2dxBitmap.java 文件,这意味着所做的任何更改都可能被更新覆盖。基本上,用于测量文本的方法虽然不正确,但并不充分。

首先,我们需要向TextProperty添加一个新变量

private final int mX;

接下来,将computeTextProperty代码替换为以下内容:

private static TextProperty computeTextProperty(final String pString, final int unusedWidth, final int unusedHeight, final Paint pPaint) { final FontMetricsInt fm = pPaint.getFontMetricsInt(); final int h = (int) Math.ceil(fm.bottom - fm.top); int maxContentWidth = 0; final String[] lines = Cocos2dxBitmap.splitString(pString, 0, 0, pPaint); /* Compute the max width. */ int temp = 0; float left = 0; for (final String line : lines) { //get a path from text Path path = new Path(); pPaint.getTextPath(line, 0, line.length(), 0, 0, path); RectF bounds = new RectF(); path.computeBounds(bounds, true); temp = (int) FloatMath.ceil(bounds.width()); //if the text extends to the left of 0 if (bounds.left < left) { left = bounds.left; } if (temp > maxContentWidth) { maxContentWidth = temp; //extend the width to account for text rendered to the left of 0 if (left < bounds.left) { maxContentWidth += (int) FloatMath.ceil(Math.abs(left)); } } } left = Math.abs(left); return new TextProperty(maxContentWidth, h, lines, (int) FloatMath.ceil(left)); }

基本上发生的情况是,我们使用了文本路径返回的信息来获取左边界是否小于 0,这意味着它将在位图之外呈现。当有多行文本时,我们也会扩展宽度,因为我们要移动所有内容以匹配左边界,我们也需要移动右边界。

最后,将computeX替换为

private static int computeX(final String pText, final int pMaxWidth, final int pHorizontalAlignment, final int pX) { int ret = 0; int expectedWidth = pX + pMaxWidth; switch (pHorizontalAlignment) { case HORIZONTALALIGN_CENTER: ret = expectedWidth / 2; break; case HORIZONTALALIGN_RIGHT: ret = expectedWidth; break; case HORIZONTALALIGN_LEFT: ret = pX; default: break; } return ret; }

您必须自己完成所有连接,但这将提供最准确的文本渲染。

于 2015-05-07T00:18:36.700 回答