- 本帖最后由 ccwccy 于 2012-7-7 12:56 编辑 *
找了好久没找到类似的代码,不想跨平台写两套代码,就自己写了个效率有点低的代码,大量中英文混写的情况下会有偏差。。大家交流下,希望能有朋友能改进下。。
inline int getStringHeight(const char * string,int width,int fontSize){ if (strlen(string)==0) { return 0; } std::vectorstd::string tmpList; std::string aString(string); int pos; int rowcount = 0; while((pos=aString.find("
")) != -1) { rowcount++; tmpList.push_back(aString.substr(0,pos));
if(pos < (int)(aString.length()-1)) { aString=aString.substr(pos+1); } } CCLabelTTF *label = CCLabelTTF::labelWithString(“a”, “Arial”, fontSize); if(tmpList.size() > 0) { std::vectorstd::string::iterator itr = tmpList.begin(); for (; itr!= tmpList.end(); itr++) { std::string tmpString = itr; label = CCLabelTTF::labelWithString(tmpString.c_str(), “Arial”, fontSize); int strWidth = label->getContentSize().width; if (itr != tmpList.end()&&strWidth>width) { int tmpcount = strWidth%width==0?strWidth/width:strWidth/width+1; rowcount += tmpcount; } } } else { label = CCLabelTTF::labelWithString(string, “Arial”, fontSize); int strWidth = label->getContentSize().width; rowcount = strWidth%width==0?strWidth/width:strWidth/width+1; }
return rowcountlabel->getContentSize().height;}