diff --git a/cocos/2d/platform/ios/CCDevice.mm b/cocos/2d/platform/ios/CCDevice.mm index 1529750760..e33dac7914 100644 --- a/cocos/2d/platform/ios/CCDevice.mm +++ b/cocos/2d/platform/ios/CCDevice.mm @@ -199,6 +199,18 @@ typedef struct } tImageInfo; +static bool s_isIOS7OrHigher = false; + +static inline void lazyCheckIOS7() +{ + static bool isInited = false; + if (!isInited) + { + s_isIOS7OrHigher = [[[UIDevice currentDevice] systemVersion] compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending; + isInited = true; + } +} + static CGSize _calculateStringSize(NSString *str, id font, CGSize *constrainSize) { NSArray *listItems = [str componentsSeparatedByString: @"\n"]; @@ -209,7 +221,6 @@ static CGSize _calculateStringSize(NSString *str, id font, CGSize *constrainSize textRect.height = constrainSize->height > 0 ? constrainSize->height : 0x7fffffff; - for (NSString *s in listItems) { CGSize tmp = [s sizeWithFont:font constrainedToSize:textRect]; @@ -222,6 +233,9 @@ static CGSize _calculateStringSize(NSString *str, id font, CGSize *constrainSize dim.height += tmp.height; } + dim.width = ceilf(dim.width); + dim.height = ceilf(dim.height); + return dim; } @@ -230,8 +244,12 @@ static CGSize _calculateStringSize(NSString *str, id font, CGSize *constrainSize #define ALIGN_CENTER 3 #define ALIGN_BOTTOM 2 + static bool _initWithString(const char * text, cocos2d::Device::TextAlign align, const char * fontName, int size, tImageInfo* info) { + // lazy check whether it is iOS7 device + lazyCheckIOS7(); + bool bRet = false; do { @@ -376,7 +394,7 @@ static bool _initWithString(const char * text, cocos2d::Device::TextAlign align, { CGContextSetTextDrawingMode(context, kCGTextStroke); - if([[[UIDevice currentDevice] systemVersion] compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending) + if(s_isIOS7OrHigher) { NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.alignment = nsAlign; @@ -412,7 +430,7 @@ static bool _initWithString(const char * text, cocos2d::Device::TextAlign align, // actually draw the text in the context [str drawInRect: rect withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:nsAlign]; - + CGContextEndTransparencyLayer(context); // pop the context diff --git a/tests/cpp-tests/Classes/LabelTest/LabelTest.cpp b/tests/cpp-tests/Classes/LabelTest/LabelTest.cpp index 2b1d00ced5..1bcff90efe 100644 --- a/tests/cpp-tests/Classes/LabelTest/LabelTest.cpp +++ b/tests/cpp-tests/Classes/LabelTest/LabelTest.cpp @@ -804,7 +804,7 @@ LabelsEmpty::LabelsEmpty() label1->setPosition(Point(s.width/2, s.height-100)); // LabelTTF - auto label2 = LabelTTF::create("", "fonts/arial.ttf", 24); + auto label2 = LabelTTF::create("", "Arial", 24); addChild(label2, 0, kTagBitmapAtlas2); label2->setPosition(Point(s.width/2, s.height/2)); @@ -1000,7 +1000,7 @@ void LabelTTFTest::updateAlignment() CC_SAFE_RELEASE(_label); - _label = LabelTTF::create(this->getCurrentAlignment(), "fonts/Marker Felt.ttf", 32, + _label = LabelTTF::create(this->getCurrentAlignment(), "Marker Felt", 32, blockSize, _horizAlign, _vertAlign); _label->retain(); @@ -1115,7 +1115,7 @@ std::string LabelTTFMultiline::subtitle() const LabelTTFChinese::LabelTTFChinese() { auto size = Director::getInstance()->getWinSize(); - auto label = LabelTTF::create("中国", "fonts/Marker Felt.ttf", 30); + auto label = LabelTTF::create("中国", "Marker Felt", 30); label->setPosition(Point(size.width / 2, size.height /2)); this->addChild(label); } @@ -1350,7 +1350,7 @@ LabelTTFA8Test::LabelTTFA8Test() addChild(layer, -10); // LabelBMFont - auto label1 = LabelTTF::create("Testing A8 Format", "fonts/Marker Felt.ttf", 48); + auto label1 = LabelTTF::create("Testing A8 Format", "Marker Felt", 48); addChild(label1); label1->setColor(Color3B::RED); label1->setPosition(Point(s.width/2, s.height/2)); @@ -1466,7 +1466,7 @@ TTFFontInit::TTFFontInit() auto font = LabelTTF::create(); - font->setFontName("fonts/Marker Felt.ttf"); + font->setFontName("Marker Felt"); font->setFontSize(48); font->setString("It is working!"); this->addChild(font); @@ -1500,7 +1500,7 @@ TTFFontShadowAndStroke::TTFFontShadowAndStroke() FontDefinition shadowTextDef; shadowTextDef._fontSize = 20; - shadowTextDef._fontName = std::string("fonts/Marker Felt.ttf"); + shadowTextDef._fontName = std::string("Marker Felt"); shadowTextDef._shadow._shadowEnabled = true; shadowTextDef._shadow._shadowOffset = shadowOffset; @@ -1519,7 +1519,7 @@ TTFFontShadowAndStroke::TTFFontShadowAndStroke() // create the stroke only label FontDefinition strokeTextDef; strokeTextDef._fontSize = 20; - strokeTextDef._fontName = std::string("fonts/Marker Felt.ttf"); + strokeTextDef._fontName = std::string("Marker Felt"); strokeTextDef._stroke._strokeEnabled = true; strokeTextDef._stroke._strokeColor = strokeColor; @@ -1539,7 +1539,7 @@ TTFFontShadowAndStroke::TTFFontShadowAndStroke() // create the label stroke and shadow FontDefinition strokeShaodwTextDef; strokeShaodwTextDef._fontSize = 20; - strokeShaodwTextDef._fontName = std::string("fonts/Marker Felt.ttf"); + strokeShaodwTextDef._fontName = std::string("Marker Felt"); strokeShaodwTextDef._stroke._strokeEnabled = true; strokeShaodwTextDef._stroke._strokeColor = strokeShadowColor;