首页 > 代码库 > cocos2d-x 中 TTF 字体文件的位置

cocos2d-x 中 TTF 字体文件的位置

cocos2d-x 中,字体文件需要保存在 fonts 文件夹中,如果字体路径中没有 fonts/ 会自动添加上这个文件夹。

如果字体名称没有 .ttf 后缀,也会自动加上这个后缀。

unsigned char* CCFreeTypeFont::loadFont(const char *pFontName, ssize_t *size) {    std::string lowerCase(pFontName);    std::string path(pFontName);    for (unsigned int i = 0; i < lowerCase.length(); ++i)    {        lowerCase[i] = tolower(lowerCase[i]);    }    if (std::string::npos == lowerCase.find("fonts/"))    {        path = "fonts/";        path += pFontName;    }    if (std::string::npos == lowerCase.find(".ttf"))    {        path += ".ttf";    }    std::string fullpath  = FileUtils::getInstance()->fullPathForFilename(path.c_str());    return FileUtils::sharedFileUtils()->getFileData(fullpath.c_str(), "rb", size);}

string::npos 就是 -1, 表示没有找到子串,find 方法用来查找字体名称中是否包含了 fonts.

cocos2d-x 中 TTF 字体文件的位置