2013-07-16 23 views
0

建立標籤,這種風格:CCLabelBMFont失去拖尾字符每個換行符

CCLabelBMFont *label1_= 
[CCLabelBMFont labelWithString:@"description: -" fntFile:@"comicsans.fnt" width:270 alignment:kCCTextAlignmentLeft]; 

和:

[label1_ setString: 
@"someText\n and some newline \nand another new line too but this is last"]; 

這個字符串有新行seen.and 2個轉義字符,當我設置林失去了最後的2個字 其顯示這樣的事情

someText 
and some newline 
and another new line too but this is la 

最後兩封信以某種方式丟失了。 什麼可能是這個問題的原因? a cocos2d v2.1(stable)bug或恐怖片中的im?如果是這樣,我該怎麼辦?

\ r與\ n的效果相同\ n 不知道爲什麼。可能是你知道的。

如果我不使用\ r \ n轉義字符; CCLabelFont字符串顯示正確的字符串,不會丟失任何數量的字符拖尾。

所以我暫時的解決方案是從字符串修復問題中刪除轉義字符。 但這並沒有解決Cocos2d v2.1(stable)的bug。 我認爲如果有\ n轉義字符,CCLabel類的類不能計算不工作穩定。

回答

0

自從我使用CCLabelBMFont生成文字輸入動畫以來,我遇到了同樣的問題。

我意識到只要要輸入的文本有換行符\n,CCLabelBMFont就不會輸入結尾字符。

我通過簡單的黑客解決了這個問題。

首先我計算要顯示的文字中的換行符數CCLabelBMFont

NSRegularExpression *regx = [NSRegularExpression regularExpressionWithPattern:@"\n" 
                     options:0 
                     error:nil]; 

NSUInteger newlinesCount = [regx numberOfMatchesInString:typeString 
               options:0 
                range:NSMakeRange(0, typeString.length)]; 

然後,我只是追加一些白色的空間,把我都快要輸入字符串的結尾。要添加的空白數量等於換行符的數量。

for (int i = 0; i < newlinesCount; i++) { 
    typeString = [typeString stringByAppendingString:@" "]; 
} 

// This sets the string for the BMFont, it should now display all the characters 
// that you wanted to type originally. 
[self.labelBMFont setString:typeString]; 

測試在cocos2d的2.1