diff --git a/cocos/platform/android/CCDevice-android.cpp b/cocos/platform/android/CCDevice-android.cpp index ccc4b43967..0b7c1c47ef 100644 --- a/cocos/platform/android/CCDevice-android.cpp +++ b/cocos/platform/android/CCDevice-android.cpp @@ -96,13 +96,15 @@ public: // Do a full lookup for the font path using FileUtils in case the given font name is a relative path to a font file asset, // or the path has been mapped to a different location in the app package: - std::string fullPathOrFontName = FileUtils::getInstance()->fullPathForFilename(textDefinition._fontName); - - // If the path name returned includes the 'assets' dir then that needs to be removed, because the android.content.Context - // requires this portion of the path to be omitted for assets inside the app package. - if (fullPathOrFontName.find("assets/") == 0) - { - fullPathOrFontName = fullPathOrFontName.substr(strlen("assets/")); // Chop out the 'assets/' portion of the path. + std::string fullPathOrFontName = textDefinition._fontName; + if(FileUtils::getInstance()->isFileExist(fullPathOrFontName)) { + fullPathOrFontName = FileUtils::getInstance()->fullPathForFilename(textDefinition._fontName); + // If the path name returned includes the 'assets' dir then that needs to be removed, because the android.content.Context + // requires this portion of the path to be omitted for assets inside the app package. + if (fullPathOrFontName.find("assets/") == 0) + { + fullPathOrFontName = fullPathOrFontName.substr(strlen("assets/")); // Chop out the 'assets/' portion of the path. + } } /**create bitmap @@ -114,7 +116,7 @@ public: int count = strlen(text); jbyteArray strArray = methodInfo.env->NewByteArray(count); methodInfo.env->SetByteArrayRegion(strArray, 0, count, reinterpret_cast(text)); - jstring jstrFont = methodInfo.env->NewStringUTF(fullPathOrFontName.empty() ? textDefinition._fontName.c_str() : fullPathOrFontName.c_str()); + jstring jstrFont = methodInfo.env->NewStringUTF(fullPathOrFontName.c_str()); if(!methodInfo.env->CallStaticBooleanMethod(methodInfo.classID, methodInfo.methodID, strArray, jstrFont, textDefinition._fontSize, textDefinition._fontFillColor.r, textDefinition._fontFillColor.g, diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxEditBoxHelper.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxEditBoxHelper.java index 1e20100cbd..3dc746f3f3 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxEditBoxHelper.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxEditBoxHelper.java @@ -243,7 +243,19 @@ public class Cocos2dxEditBoxHelper { if (editBox != null) { Typeface tf; if (!fontName.isEmpty()) { - tf = Typeface.create(fontName, Typeface.NORMAL); + if (fontName.endsWith(".ttf")) { + try { + tf = Cocos2dxTypefaces.get(mCocos2dxActivity.getContext(), fontName); + } catch (final Exception e) { + Log.e("Cocos2dxEditBoxHelper", "error to create ttf type face: " + + fontName); + // The file may not find, use system font. + tf = Typeface.create(fontName, Typeface.NORMAL); + } + } else { + tf = Typeface.create(fontName, Typeface.NORMAL); + } + }else{ tf = Typeface.DEFAULT; } diff --git a/cocos/ui/UIEditBox/UIEditBoxImpl-android.cpp b/cocos/ui/UIEditBox/UIEditBoxImpl-android.cpp index b0377b8742..d9ef41c2ca 100644 --- a/cocos/ui/UIEditBox/UIEditBoxImpl-android.cpp +++ b/cocos/ui/UIEditBox/UIEditBoxImpl-android.cpp @@ -36,6 +36,7 @@ #include "math/Vec2.h" #include "ui/UIHelper.h" #include "base/CCDirector.h" +#include "platform/CCFileUtils.h" NS_CC_BEGIN @@ -112,8 +113,17 @@ void EditBoxImplAndroid::setNativeFont(const char* pFontName, int fontSize) { auto director = cocos2d::Director::getInstance(); auto glView = director->getOpenGLView(); - JniHelper::callStaticVoidMethod(editBoxClassName, "setFont", - _editBoxIndex, pFontName, + auto isFontFileExists = cocos2d::FileUtils::getInstance()->isFileExist(pFontName); + std::string realFontPath = pFontName; + if(isFontFileExists) { + realFontPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(pFontName); + if (realFontPath.find("assets/") == 0) + { + realFontPath = realFontPath.substr(strlen("assets/")); // Chop out the 'assets/' portion of the path. + } + } + JniHelper::callStaticVoidMethod(editBoxClassName, "setFont", + _editBoxIndex, realFontPath, (float)fontSize * glView->getScaleX()); } diff --git a/tests/lua-tests/project/proj.ios_mac/mac/Info.plist b/tests/lua-tests/project/proj.ios_mac/mac/Info.plist index 608a82cdac..f15ec7184f 100644 --- a/tests/lua-tests/project/proj.ios_mac/mac/Info.plist +++ b/tests/lua-tests/project/proj.ios_mac/mac/Info.plist @@ -2,6 +2,8 @@ + ATSApplicationFontsPath + fonts CFBundleDevelopmentRegion en CFBundleExecutable