CCLabelTTF invisible issue in iOS 7.0 - ipad

I'm using cocos2d-x 2.0.4 for my game.
CCLabelTTF works well on both of device and simulator in iOS 6. But when i test it in iOS 7.0, it doesn't work.
Here is my code.
int nScore = 10;
char str[50];
sprintf(str, "SCORE : %d", nScore);
CCLabelTTF *lbl = CCLabelTTF::create(str, "Marker Felt", 50);
lbl->setPosition(ccp(size.width*0.5, size.height*0.88));
lbl->setColor(ccRED);
this->addChild(lbl);
Score doesn't show now. But it shows in iOS 6.
One more strange problem.
If i change above code like this, it works.
CCLabelTTF *lbl = CCLabelTTF::create("SCORE", "Marker Felt", 50);
lbl->setPosition(ccp(size.width*0.5, size.height*0.88));
lbl->setColor(ccRED);
this->addChild(lbl);
But if i change this code like below again, it doesn't work.(invisible)
CCLabelTTF *lbl = CCLabelTTF::create("Score", "Marker Felt", 50);
lbl->setPosition(ccp(size.width*0.5, size.height*0.88));
lbl->setColor(ccRED);
this->addChild(lbl);
Maybe it's case sensitive issue.
Finally below code doesn't work too even if text is upper case. I only added number 10.
CCLabelTTF *lbl = CCLabelTTF::create("SCORE : 10", "Marker Felt", 50);
lbl->setPosition(ccp(size.width*0.5, size.height*0.88));
lbl->setColor(ccRED);
this->addChild(lbl);
Any help will be appreciate.
Thanks in advance.

I ran into this same problem while using cocos2d-x 2.1.3. I found this link stating that the issue is a bug that affects labels in iOS 7. In order to fix the issue, you'll need to either update the engine, or merge this pull request manually.

upgrade your cocos2d-x version it fixed in 3.0 and if you are using cocos2dx older version then change into CCimage.mm this statment
CGContextRef context = CGBitmapContextCreate(data,dim.width,dim.height, 8,dim.width * 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextRef context = CGBitmapContextCreate(data, (size_t)dim.width, (size_t)dim.height, 8, (size_t)dim.width * 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

Related

Could not make a CTParagraphStyleRef on iPhone

I found a strange problem when using the CoreText. Below is the code:
CGFloat headindent = theme.horizontalMargin.floatValue; // 10.0
CTParagraphStyleSetting head;
head.spec = kCTParagraphStyleSpecifierHeadIndent;
head.value = &headindent;
head.valueSize = sizeof(float);
CGFloat tailindent = -theme.horizontalMargin.floatValue; //-10.0
CTParagraphStyleSetting tail;
tail.spec = kCTParagraphStyleSpecifierTailIndent;
tail.value = &tailindent;
tail.valueSize = sizeof(float);
CGFloat firstlineindent =theme.horizontalMargin.floatValue;//10.0
CTParagraphStyleSetting firstline;
firstline.spec = kCTParagraphStyleSpecifierFirstLineHeadIndent;
firstline.value = &firstlineindent;
firstline.valueSize = sizeof(float);
CTParagraphStyleSetting settings[]={
head,
tail,
firstline,
};
CTParagraphStyleRef style = CTParagraphStyleCreate(settings, 3);
Then the style is added to make an attributed string. I get the values from a theme stored in CoreData. That works well when I run the app on iPad, but on iPhone the code does not work.
I made a breakpoint to check what is the problem, and I found that the theme values are both right in the iPad and iPhone condition,but the value of style ,which is a CTParagraphStyleRef, are all ZERO when running on iPhone.
Anyone has encounter same problem before? I just could not figure where is wrong.
Thanks for any help!
I just find the where the problem is...
In iPhone ,the sizeof(CGFloat) = 8 ,sizefo(float) = 4,
and in iPad , they are both 4.
But what still confused me is that since
typedef float CGFloat;
why their size are different.

Drawing text with an MKOverlayRenderer

I'm trying to render text on a map using an MKOverlayRenderer. I have an existing, functional MKOverlayRenderer rendering a set of points, so my only problem is rendering a piece of text for each point within the '-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context' function.
All solutions I have found through SO and Google use annotations or UILabels. But I want to have the text drawing code in the same location as the code rendering the points. Also there are about 10,000 points, though I'm ensuring it's not rendering them all at the same time through zoom and bounds checking. I am reasonably sure I don't want to create 10,000 objects with the other solutions.
This is the current test code I have to try to render one of the 'Text Text' items. It is a combination of some of the methods I have found on the net to try to render something.
CGPoint* point = self.pointList.pointArray + pointIndex;
CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextSelectFont(context, "Helvetica", 20.f, kCGEncodingFontSpecific);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGAffineTransform xform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
CGContextSetTextMatrix(context, xform);
CGContextShowTextAtPoint(context, point->x, point->y, "Test Text 1", 11);
CGContextShowTextAtPoint(context, 10, 10, "Test Text 2", 11);
CGContextShowText(context, "Test Text 4", 11);
UIFont* font = [UIFont fontWithName:#"Helvetica" size:12.0];
[#"Test Text 3" drawAtPoint:*point withFont:font];
This is my first SO questions, so sorry if it isn't that correct.
Edit: I just saw the text when zoomed in as far as I can go, so realise I haven't been accounting for the zoom scale. Assuming I need to do a scale transform before rendering to account for it. Haven't solved it currently, but I think I am on my way.
I have solved it. Sorry for posting this, but I was at my wit's end and thought I needed help.
The line that rendered was:
CGContextShowTextAtPoint(context, point->x, point->y, "Test Text 1", 11);
Which is a deprecated function, but I don't know any other way to render to a specific context.
To fix it, the affine transform became:
CGAffineTransform xform = CGAffineTransformMake(1.0 / zoomScale, 0.0, 0.0, -1.0 / zoomScale, 0.0, 0.0);
The other error was that the 'select font' call needed to become:
CGContextSelectFont(context, "Helvetica", 12.f, kCGEncodingMacRoman);
I had copied the other encoding from some example code I had seen on the net, but it causes the text to have wrong characters.
If there is still a way I can do it without using the deprecated CGContextShowTextAtPoint function I would still love to know.

How to display dashed lines in objective-c?

I have this code to display a grid with dashed line. In runtime on an iphone 5 below it shows fine, but if I run the app on iphone 5s there's no grid. I tested in iPhone Simulator and on real devices and happens the same.
Here's the code:
if (self.dashLongitude) {
CGFloat lengths[] = {3.0, 3.0};
CGContextSetLineDash(context, 0.0, lengths, 2);
}
//other stuff here
CGContextSetLineDash(context, 0, nil, 0);
So anyone could help??
EDIT: Hey guys I solved the issue using the same code I posted here, but in a different method. So I have now two methods: one just for the drawing the grid and another on to draw the line with data and finally got everything working.
The code looks fine.
As I look into the code I saw you are setting the nil in the context. nil is used to remove the dashed line.
Try to use
CGFloat dash[] = {2.0, 2.0};
CGContextSetLineDash(context, 0.0, dash, 2);
where you are creating or provide stroke your line(underneath).
CGContextSetLineDash(context, 0, NULL, 0);
is used to remove that dash pattern.
Instead of setting nil in CGContextSetLineDash, just wrap your code into CGContextSaveGState/CGContextRestoreGState to preserve context state before applying line dash:
CGContextSaveGState(context);
CGFloat dash[] = {2.0, 2.0};
CGContextSetLineDash(context, 0.0, dash, 2);
// Draw some lines here
CGContextRestoreGState(context);

Cocos2d-x CClabelTTF invisible

In my game I use cclabelttf to display the score the player made. It was working fine back in the end of July, I've changed nothing in my code, but there was a:
- IOS upgraded (6.1 to 7.0)
- OSX updated
- cocos2d-x
- Xcode update
I'm not using helvetica fonts.
I have a floating text to show the score, if I kill a terrorist a "+10" string floats up and than disappears, if I write " +10 " then it's visible, otherwise it's not.
I've tried to change the text alignment in ccimage.mm, from UITextAlignmentLeft to the same with NS,
uncomment these lines:
if( [font isKindOfClass:[UIFont class] ] )
{
[str drawInRect:CGRectMake(0, startH, dim.width, dim.height) withFont:font lineBreakMode:(UILineBreakMode)UILineBreakModeWordWrap alignment:align];
}
I've read these modifications on the cococs2d-x forum, there was a bug back than, and these was the solution. No luck for me.
Weird part is on my gameScene one of the labels is visible, but only on iPhone simulator, but starting from this, I think it must be an alignment/wrapping problem.
Met the same issue, found a solution works for me, try this.
Modify _initWithString in CCImage.mm, at line:
CGContextRef context = CGBitmapContextCreate(data, dim.width, dim.height, 8, dim.width * 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
change it to:
CGContextRef context = CGBitmapContextCreate(data, (int)dim.width, (int)dim.height, 8, (int)dim.width * 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
Just three (int) casts.
maybe you can try it like this:
CCLabelTTF* YouClassName::stringNewLine(string orgStr,CCSize sizeTable,const char* fontName,float fontSize){
CCLabelTTF *m_label_content = CCLabelTTF::create( "hello", fontName, fontSize ,sizeTable, kCCTextAlignmentCenter, kCCVerticalTextAlignmentCenter);
m_label_content->setString(orgStr.c_str());
return m_label_content;
}
and use it like this
CCLabelTTF * ttf = stringNewLine("test",CCSizeMake(200,200), "Arial", 28);

Differences in Core-Text between ios 6 and ios 5.1?

It appears there are some differences in the CoreText implementation between ios 5.1 and ios 6, as you can see from these two screenshots :
ios 6:
ios 5:
Firstly the text color isn't applied correctly. It seems that on ios 5.1 the kCTForegroundColorAttributeName requires that you give it a CGColor, whereas on ios 6, passing it a UIColor will suffice. So I solved the issue by changing my code to:
[attributes setObject:(id)[color CGColor]
forKey:(NSString*)kCTForegroundColorAttributeName];
Secondly, the paragraph spacing is a bit off. The distance between "sight" and "According" is 11px vs 25px (measured in the screenshot). In both cases paragraph spacing is set to 5:
NSMutableData *styleSettingsArray = [NSMutableData data];
CGFloat spaceBefore,spaceAfter;
...
CTParagraphStyleSetting styleSettingB = {kCTParagraphStyleSpecifierParagraphSpacingBefore ,sizeof(CGFloat),&spaceBefore};
CTParagraphStyleSetting styleSettingA = {kCTParagraphStyleSpecifierParagraphSpacing ,sizeof(CGFloat),&spaceAfter};
[styleSettingsArray appendBytes:&styleSettingB length:sizeof(styleSettingB)];
[styleSettingsArray appendBytes:&styleSettingA length:sizeof(styleSettingA)];
...
if(styleSettingsArray.length > 0)
{
CTParagraphStyleRef paragraphStyleRef = CTParagraphStyleCreate([styleSettingsArray bytes], [styleSettingsArray length] / sizeof(CTParagraphStyleSetting));
[dictionary setObject:(__bridge id)(paragraphStyleRef) forKey:(NSString*)kCTParagraphStyleAttributeName];
CFRelease(paragraphStyleRef);
}
Description of paragraphStyleRef in console:
iOS 6:
CTParagraphStyle:
base writing direction = -1, alignment = 3, line break mode = 0, default tab interval = 0
first line head indent = 0, head indent = 0, tail indent = 0
line height multiple = 0, maximum line height = 0, minimum line height = 0
line spacing adjustment = 0, paragraph spacing = 5, paragraph spacing before = 5
iOS 5:
CTParagraphStyle:
writing direction = -1, alignment = 3, line break mode = 0, default tab interval = 0
first line head indent = 0, head indent = 0, tail indent = 0
line height multiple = 0, maximum line height = 0, minimum line height = 0
line spacing adjustment = 0, paragraph spacing = 5, paragraph spacing before = 5
which seem the same to me, so I don't know what the problem is. Other than the spacing between the paragraphs, they're identical.
So how can I fix this? Also are there any other things I should be aware of that might cause the text to display differently?
EDIT:
After some investigation, it turnes out that the difference in paragraph styling was actually caused by my line breaks which printed "\r\n". Changing that to "\n" solved the spacing problem.
Core Text got an overhaul in iOS 6. You check out all the changes by watching the WWDC 2012 videos that are available for free if you have an Apple Developer Account.
So now in iOS 6, you can't use any of the low level Core Text attributes like kCTForegroundColorAttributeName or kCTParagraphStyleAttributeName.
Instead you use a new set of high level attributes, like NSForegroundColorAttributeName & NSParagraphStyle.
So your code would change to:
/*Note that you have use the Foundation class
for the attribute value instead of it's Core-Foundation counterpart.*/
[attributes setObject:color
forKey:NSForegroundColorAttributeName];
CGFloat spaceBefore, spaceAfter;
NSMutableParagraphStyle *mutableParagraphStyle = [NSMutableParagraphStyle defaultParagraphStyle];
mutableParagraphStyle.paragraphSpacing = spaceAfter;
mutableParagraphStyle.paragraphSpacingBefore = spaceBefore;
[attributes setObject:mutableParagraphStyle
forKey:NSParagraphStyleAttributeName];
You can find the documentation for the all new attributes here:
http://developer.apple.com/library/ios/#documentation/uikit/reference/NSAttributedString_UIKit_Additions/Reference/Reference.html

Resources