代码如下:
CCLabelTTF* labelUntils:: verticalSpacing(string _string,const char* fontName,float fontSize,float horizontalSpacing,float verticalSpacing,float lineWidth,const ccColor3B &color3){
CCArray* labelTTF_arr = CCArray::create();
int index = 0;
int index_max = strlen(_string.c_str());
bool is_end = true;
while (is_end) {
if (_stringindex] >= 0 && _stringindex] <= 127) {
string englishStr =_string.substr(index,1).c_str();
labelTTF_arr->addObject(CCLabelTTF::create(englishStr.c_str(), fontName, fontSize));
index+= 1;
}
else{
string chineseStr =_string.substr(index,3).c_str();
labelTTF_arr->addObject(CCLabelTTF::create(chineseStr.c_str(), fontName, fontSize));
index+= 3;
}
if (index>=index_max) {
is_end=false;
}
}
//以上步骤是根据ASCII码找出中英文字符,并创建成一个CCLabelTTF对象存入labelTTF_arr数组中。
//下面创建的原理是在CCLabelTTF对象上添加子对象CCLabelTTF,以此组合成一句话,以左上角第一个字为锚点。。
CCLabelTTF* returnTTF = (CCLabelTTF*)labelTTF_arr->objectAtIndex(0);
float nowWidth = returnTTF->getContentSize().width;
CCLabelTTF* dangqiangTTF = returnTTF;
CCLabelTTF* lineBeginTTF = returnTTF;
int arr_count = labelTTF_arr->count();
for (int i=1; i < arr_count; i++) {
CCLabelTTF* beforeTTF = (CCLabelTTF*)labelTTF_arr->objectAtIndex(i);
beforeTTF->setColor(color3);
beforeTTF->setAnchorPoint(ccp(0, 0.5));
nowWidth+=beforeTTF->getContentSize().width;
if (nowWidth >= lineWidth) {
nowWidth = returnTTF->getContentSize().width;
dangqiangTTF = lineBeginTTF;
beforeTTF->setPosition(ccp(0, -dangqiangTTF->getContentSize().height*0.5-verticalSpacing));
lineBeginTTF = beforeTTF;
}else{
beforeTTF->setPosition(ccp(dangqiangTTF->getContentSize().width+horizontalSpacing, dangqiangTTF->getContentSize().height*0.5));
}
dangqiangTTF->addChild(beforeTTF);
dangqiangTTF = beforeTTF;
}
returnTTF->setColor(color3);
return returnTTF;
}