Game Development Reference
In-Depth Information
CGRectMake(40, 20, 200, 24)];
textField.text = @"Regular UITextField";
textField.borderStyle = UITextBorderStyleRoundedRect;
// get the cocos2d view (it's the CCGLView class which inherits from UIView)
UIView* glView = [CCDirector sharedDirector].view;
// add the text field view to the cocos2d CCGLView
[glView addSubview:textField];
}
It's important to note that the process of programmatically creating any UIView class
is very similar to how you create the UITextField . You pick the desired class de-
rived from UIView and then call alloc and initWithFrame . You can create most
UIView controls by just providing a frame rectangle. However, you usually have to
set some properties afterward to configure the control; in this example, I've set the
textField to use the rounded style as well as setting the initial text.
Caution The frame rectangle is where many programmers first notice the dif-
ferent coordinate systems of cocos2d nodes and UIView classes. Whereas in
cocos2d the origin (0, 0) is at the lower left-hand corner of the screen, the origin
for UIView classes is at the upper left-hand corner of the screen. This means
that the UITextField is actually 20 pixels below the top border of the screen
and not 20 pixels above the bottom border. You'll have to keep this in mind
when working with UIView classes.
Because the UITextField , like most other UIView classes, doesn't have a show
method, you need some other way to attach it to the view hierarchy. Because the
cocos2d view is the CCGLView class, which in turn inherits from UIView , you can
simply add the UITextField to the cocos2d view as a subview. The CCDirector
has a view property that lets you access the cocos2d view and then call the addSub-
view method on it to add the textField . By default this adds the view on top of the
cocos2d view.
Search Nedrilad ::




Custom Search