Author Archive

If you are using Cocos2d v3, use CCTextField for text input. For example:

CCTextField *enterName = [CCTextField textFieldWithSpriteFrame:[CCSpriteFrame frameWithImageNamed:@"textfield_background.png"]];
enterName.fontSize = 16.0f;
enterName.contentSize = CGSizeMake(100.0f, 50.0f);
enterName.preferredSize =CGSizeMake(100.0f, 50.0f);
enterName.positionType = CCPositionTypeNormalized;
enterName.position = ccp(0.5f, 0.5f);
[self addChild:enterName z:5];

If you are using Cocos2d v2, use UIKit components. For example:

CGSize size = [[CCDirector sharedDirector] winSize];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(size.width * 0.5,
size.height* 0.1,
100, 100)];
textField.borderStyle = UITextBorderStyleRoundedRect;
[[[CCDirector sharedDirector] view]addSubview:textField];

As a note, Cocos2d v3 actually uses a UITextField in iOS and a NSTextField in Mac.

/** iOS: UITextField used by the CCTextField. */
@property (nonatomic,readonly) UITextField* textField;
#elif defined(__CC_PLATFORM_MAC)

/** Mac: NSTextField used by the CCTextField. */
@property (nonatomic,readonly) NSTextField* textField;

Changes in cocos2d 3.x

CCNodes, CCScenes and CCLayers

CCLayer does not exist in cocos2d v3.x anymore. CCLayer used to be the class you should use, when you wanted to handle touch input by a user. In cocos2d v3.x any CCNode can handle touch input, thus there is no use for CCLayer anymore. CCNode is the new CCLayer. For scenes use CCScene for everything in your scene use CCNode.

Touch handling

For now it is most important to know how to enable touch handling on your CCNode:
self.userInteractionEnabled = TRUE;

How to catch a touch and its touch position:

- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
CGPoint touchLocation = [touch locationInNode:self];

//you can put your touch handling code here

And to know the three other methods that can be implemented to determine moving, ending, or canceled touches:

- (void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event

- (void)touchEnded:(UITouch *)touch withEvent:(UIEvent *)event

- (void)touchCancelled:(UITouch *)touch withEvent:(UIEvent *)event

How to change the font size in UIWebview

Uee the following codein your webViewDidFinishLoad delegate method

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    int fontSize = 20;
    NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", fontSize];
    [webView stringByEvaluatingJavaScriptFromString:jsString];
    [jsString release];

NSString to NSDate

NSString *dateString = @"01-01-2014";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];

NSDate convert to NSString:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"%@", strDate);
[dateFormatter release];

Rename files in Documents directory

To get NSDocuments directory use :

NSString *originalfileName = @"MyFile.txt";
NSString *newfileName = @"MyFileNameChanged.txt";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePathOriginal = [documentsDirectory stringByAppendingPathComponent:originalfileName];
NSString *filePathNew = [documentsDirectory stringByAppendingPathComponent:newfileName];

You can use moveItemAtPath to rename.

NSError * err = NULL;
NSFileManager * fileManager = [[NSFileManager alloc] init];
BOOL result = [fileManager moveItemAtPath:filePathOriginal toPath:filePathNew error:&err];
NSLog(@"Error: %@", err);
[fileManager release];

If you do not know the names of the files in documents directory you can use :

NSArray *directoryContent = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:nil];

This array contains all filenames you need. So you can rename them all.

Comparing UIButton backgroundImage

We can use the following code to compare a UIButton’s background image

if ([[myButton backgroundImageForState:UIControlStateNormal] isEqual:[UIImage imageNamed@"myImage.png"]]) {
// Button has a background image named 'myImage.png'
// Button has not a background image named 'myImage.png'

Add an activity indicator as shown here:

UIActivityIndicatorView *activityIndicator =
[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
UIBarButtonItem * barButton =
[[UIBarButtonItem alloc] initWithCustomView:activityIndicator];

// Set to Left or Right
[[self navigationItem] setRightBarButtonItem:barButton];

[barButton release];
[activityIndicator startAnimating];

//Remove the spinner and stop animating:

[[self navigationItem] setLRightBarButtonItem:nil];
[activityIndicator stopAnimating];

UIAlertView – a simple text input dialog box

Create the alertView with style UIAlertViewStylePlainTextInput:

UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Hello!" message:@"Please enter your name:" delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeNumberPad;
alertTextField.placeholder = @"Enter your name";
[alert show];

This produces an alertView:

When pressing any buttons, the regular delegate methods will be called and you can extract the textInput there like so:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"Entered: %@",[[alertView textFieldAtIndex:0] text]);

Detect Retina Display

The following lines of code can be used to detect whether the device is retina display

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] == YES && [[UIScreen mainScreen] scale] == 2.00) {
         // Retina Display
    else {
         // Not Retina Display

CSS3 Text Shadow

text-shadow: 5px 5px 5px #FF0000;

<h1>Text-shadow effect!</h1>

<p><b>Note:</b> Internet Explorer 9 and earlier versions, does not support the text-shadow property.</p>

Page 1 of 912345...Last »
We Can Build Your App for You. Lets Get Started !