cocos2d-objc icon indicating copy to clipboard operation
cocos2d-objc copied to clipboard

[BUG]CCLableTTF works wrong with 64bit

Open kthtes opened this issue 11 years ago • 2 comments

There's a strange bug.

1.Create a cocos2d v3.x project with "cocos2d iOS" 2.In "IntroScene.m", find "-(id)init" function, and add the following lines under "[self addChild:helloWorldButton];"

//************** under [self addChild:helloWordButton]; (line53)
NSMutableAttributedString* as=[[NSMutableAttributedString alloc] initWithString:@"ABCDE"];
NSRange fullRange=NSMakeRange(0, as.length);

UIFont* font1=[UIFont fontWithName:@"Times New Roman" size:30.0];
[as addAttribute:NSFontAttributeName value:font1 range:fullRange];
[as addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:fullRange];

UIFont* font2=[font1 fontWithSize:15.0];
[as addAttribute:NSFontAttributeName value:font2 range:NSMakeRange(2,2)];
[as addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(2, 2)];

CCLabelTTF* la=[CCLabelTTF labelWithAttributedString:as];
la.position=ccp(100,100);
[self addChild:la];
//**************

3.The result should be a label with "ABCDE". Among the 5 letters, "A" "B" and "E" should be in white color and full size(30.0), "C" and "D" should be in yellow color and half size(15.0). See the picture below: ios 2014528 3 38 40

4.However, the actual result is:

ios 2014528 3 37 12

To avoid that bug, you'll have to either change the size of "UIFont* font2" from "15.0" to "15.001" or use 32-bit CPU simulator or device.

Here are my develop environment: OS X: 10.9.3 XCode: 5.1.1 cocos2d-iphone: v3.0.0 iOS simulator: iPhone Retina 64-bit with iOS 7.1 iOS device: iPad Air with iOS 7.1

kthtes avatar May 28 '14 07:05 kthtes

I know where the problem is: in "NSAttributedString+CCAdditions.m" there is a function "- (NSAttributedString*) copyAdjustedForContentScaleFactor" as follow:

//***************** in the function ***********

NSMutableAttributedString* copy = [self mutableCopy];

NSRange fullRange = NSMakeRange(0, copy.length);

CGFloat scale = [CCDirector sharedDirector].contentScaleFactor;

//!!! Replace "copy" with "self" !!! Because you can't enumerate something while changing it
[copy enumerateAttribute:NSFontAttributeName inRange:fullRange options:0 usingBlock:^(id value, NSRange range, BOOL* stop){
    if (value)
    {

kthtes avatar May 28 '14 08:05 kthtes

This sounds similar to an issue we fixed in the past, but it needs testing.

slembcke avatar Jan 14 '15 02:01 slembcke