Monday, 3 June 2013

How to create/display UILabel programmatically:cocos2d

To create an UILabel in the scene, you can use the below code:

    // in .h File //

    UIView *profileView;
    UILabel *labelName;

    // in .m File //

    -(void)show_{

        // Create a UIView //
        profileView = [[UIView alloc] init];
        profileView.frame = CGRectMake(0, 0, 320, 480);
        profileView.backgroundColor = [UIColor blueColor];
        [[[CCDirector sharedDirector] openGLView] addSubview:profileView];

        // Create the UIlabel and add it to the UIView //
        labelName = [[UILabel alloc] init];
        labelName setText:[NSString stringWithFormat:@"%d",0]];
        labelName.frame = CGRectMake(0, 0,  35, 35);
        labelName.center = CGPointMake(160, 160);
        labelName.backgroundColor = [UIColor clearColor]; 
        labelName.textAlignment = NSTextAlignmentCenter;  
        [profileView addSubview:labelName];     
       
        //----------Line break for UIlabel(optional)----------//
        labelName.lineBreakMode = NSLineBreakByWordWrapping;
        labelName.numberOfLines = 0;
        //---------------------------------------------------//
    }

No comments:

Post a Comment