Cocos2d Archives

Consider a UISegmentedControl object as follows :

UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"ON",@"OFF",nil]];
segment.selectedSegmentIndex = 0;
[self changeUISegmentFont:segment]; // Call this method to edit the label of the segment with the UISegmentedControl object as parameter. The method definition is given below.

// Method Definition

-(void) changeUISegmentFont:(UIView*) myView {

if ([myView isKindOfClass:[UILabel class]]) {  // Getting the label subview of the passed view

UILabel* label = (UILabel*)myView;

[label setTextAlignment:UITextAlignmentCenter];

[label setFont:[UIFont boldSystemFontOfSize:14]]; // Set the font size you want to change to


NSArray* subViewArray = [myView subviews]; // Getting the subview array

NSEnumerator* iterator = [subViewArray objectEnumerator]; // For enumeration

UIView* subView;

while (subView = [iterator nextObject]) { // Iterating through the subviews of the view passed

[self changeUISegmentFont:subView]; // Recursion



Incoming search terms:

  • ios uisegment label
  • uisegmentedcontrol size
  • xcode UILabel change font size

The following code will make the UIImageView round cornered with border to it :

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(42, 2, 55, 26)]; // Creating a UIImageView object

imgView.image = [UIImage imageNamed:@"myImage.png"]; // Adding image to the UIImageVIew object

imgView.contentMode = UIViewContentModeScaleAspectFill; // Setting the content mode for the UIImageView

imgView.clipsToBounds = YES;

// For rounded corners:

imgView.layer.cornerRadius = 6.0; // Setting the corner radius

imgView.layer.masksToBounds = YES; // For enabling masking


//For adding border

imgView.layer.borderColor = [UIColor whiteColor].CGColor; // Setting the border color

imgView.layer.borderWidth = 2.0; // Setting the border width

Incoming search terms:

  • border radius mask on image in objective c
  • border radius masking on uiimage in objective c
  • set border and corner radius to uiimage in objective c
  • set corner radius and border on image in objective c
  • xcode obj c uiimageview set border and rounded
  • xcode round image
  • xcode uiimageview border

CCSprite to CCTexture2d conversion

To get texture and sprite from an image using the flowing code:
NSString* file = @"myImage.png";
CCTexture2D* texture = [[CCTextureCache sharedTextureCache] addImage:file];
CGSize texSize = texture.contentSize;
CGRect texRect = CGRectMake(0, 0, texSize.width, texSize.height);
CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:texture rect:texRect];

Particle Effect in cocos 2d

Here is the code to give particle effect in cocos 2d using a plist:

CCParticleSystemQuad *effect = [CCParticleSystemQuad particleWithFile:@"bubbles.plist"];
[self addChild:effect];

Wave Effect in cocos 2d

To give wave effect to a particular sprite, use the code as given below:

CCWaves *waves = [CCWaves actionWithWaves:-1 amplitude:3 horizontal:YES vertical:NO grid:ccg(15,10) duration:4];
[bgImage runAction: [CCRepeatForever actionWithAction:waves]];

How to select random numbers within a range?

In cocos 2d, for selecting a random number between a given range, use the code as given below:

arc4random()%(endValue-startValue+1) + startValue;

For example, for selecting a random number between 50 and 149, use the code given below:

arc4random()%100 + 50;

How to select random numbers without repetition?

The below function in cocos 2d is used for selecting a specified number of random numbers from a particular range without repetition.

//Function to select random numbers without repetition

-(void) randomarrayInitialisation:(int)totalRandomNumbers:(int)minNumber:(int)maxNumber
    int randCount=totalRandomNumbers;
    int maxRandom=maxNumber;
    for(int i=0;i<100;i++)
    for(int i=0;i<randCount;i++)
        int rand=minNumber+arc4random()%(maxRandom-minNumber);
        int flag=0;
        for(int j=0;j<randCount;j++)

We can call the function as given below:

[self randomarrayInitialisation:3:0:10];

The above code will select 3 random numbers from the range 0 to 9 and stores it in the variables randomArray[0],randomArray[1],randomArray[2].

Don’t forget to declare randomArray in .h class as follows:

int randomArray[100];

Add the sprite to a separate layer. Give required anchor points for the object and the layer.

Then give  rotations to the object and layer.

Updating labels in cocos2d

[labelname setString:@"string"];

How to convert a UIView into a UIImage

To convert uiview into a uiimage ..You can use the following code.



[certificateView.layer renderInContext:UIGraphicsGetCurrentContext()];

uiimage collageImage = UIGraphicsGetImageFromCurrentImageContext();


[collageImage retain];




Page 20 of 44« First...10...1819202122...3040...Last »
We Can Build Your App for You. Lets Get Started !