Here is the code to show.
UIViewController* viewControoler = [[UIViewController alloc]init];
textfield = [[UITextField alloc]initWithFrame:CGRectMake(0,0,280,90)];
textfield.placeholder = #"word";
textfield.keyboardType = UIKeyboardTypeDefault;
textfield.returnKeyType = UIReturnKeySearch;
textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
textfield.delegate = self;
textfield.userInteractionEnabled = YES;
[textfield becomeFirstResponder];
[viewControoler.view addSubview:textfield];
[[[CCDirector sharedDirector]openGLView] addSubview:viewControoler.view];
It is running well. But textfield is not editable because keyboard is not coming when I am clicking on uitextfiled.
Remove UIViewController,directly add on OpenGLView,
textfield = [[UITextField alloc]initWithFrame:CGRectMake(0,0,280,90)];
textfield.placeholder = #"word";
textfield.keyboardType = UIKeyboardTypeDefault;
textfield.returnKeyType = UIReturnKeySearch;
textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
textfield.delegate = self;
textfield.userInteractionEnabled = YES;
[textfield becomeFirstResponder];
[[[CCDirector sharedDirector]openGLView] addSubview:textfield;
Related
I would like to create a custom UITextView with the ability to enter text in it, as well as programmatically adding some UILabels there that would act like text (I need to delete them with the 'backspace' button when the cursor comes near them).
This UITextView should be expandable and the labels can have different width.
Any ideas of how you can create this sort of thing, any tutorials or such?
you can create textfield using this code.
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = #"enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
[self.view addSubview:textField];
[textField release];
And for creating label you can use this code:
CGRect labelFrame = CGRectMake( 10, 40, 100, 30 );
UILabel* label = [[UILabel alloc] initWithFrame: labelFrame];
[label setText: #"My Label"];
[label setTextColor: [UIColor orangeColor]];
label.backgroundColor =[UIColor clearColor];
[view addSubview: label];
and for deleting label when backspace tape use this method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ([string isEqualToString:#""]) {
NSLog(#"backspace button pressed");
[label removeFromSuperview];
}
return YES;
}
If the backspace button is pressed, then the replacementString(string) will have null value. So we can identify backspace button press using this.
I wants add multiple UITextFields in a single loop. And identify them when user interact with UITextFields . So please help me.
Thanks
Currently I place UITextField Individually, like this
txt_FirstName=[[UITextField alloc]initWithFrame:CGRectMake(framex1, framey1, framex2, framey2)];
txt_FirstName.borderStyle = UITextBorderStyleRoundedRect;
txt_FirstName.font = [UIFont systemFontOfSize:15];
txt_FirstName.placeholder = #"First Name";
txt_FirstName.autocorrectionType = UITextAutocorrectionTypeNo;
txt_FirstName.keyboardType = UIKeyboardTypeDefault;
txt_FirstName.returnKeyType = UIReturnKeyDone;
txt_FirstName.clearButtonMode = UITextFieldViewModeWhileEditing;
txt_FirstName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
txt_FirstName.delegate = self;
[frameView addSubview:txt_FirstName];
[txt_FirstName release];
framey1=framey1+50;
txt_MiddleName=[[UITextField alloc]initWithFrame:CGRectMake(framex1, framey1, framex2, framey2)];
txt_MiddleName.borderStyle = UITextBorderStyleRoundedRect;
txt_MiddleName.font = [UIFont systemFontOfSize:15];
txt_MiddleName.placeholder = #"Middle Name";
txt_MiddleName.autocorrectionType = UITextAutocorrectionTypeNo;
txt_MiddleName.keyboardType = UIKeyboardTypeDefault;
txt_MiddleName.returnKeyType = UIReturnKeyDone;
txt_MiddleName.clearButtonMode = UITextFieldViewModeWhileEditing;
txt_MiddleName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
txt_MiddleName.delegate = self;
[frameView addSubview:txt_MiddleName];
[txt_MiddleName release];
Please use this, but Implement UITextField delegate in your .h file. but You should have to change your place holder according to your requirement like 1 for FirstName, 2 for MiddleName etc.
First create common function like this,
-(void)setTextfieldStyle:(UITextField *)pTmpTextField
{
pTmpTextField.borderStyle = UITextBorderStyleRoundedRect;
pTmpTextField.font = [UIFont systemFontOfSize:15];
pTmpTextField.placeholder = #"First Name";
pTmpTextField.autocorrectionType = UITextAutocorrectionTypeNo;
pTmpTextField.keyboardType = UIKeyboardTypeDefault;
pTmpTextField.returnKeyType = UIReturnKeyDone;
pTmpTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
pTmpTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
pTmpTextField.delegate = self;
}
and use this for creating UITextFiled,
int y = 0;
for(int i=0;i < 5;i++)
{
UITextField *txtTemp=[[UITextField alloc]initWithFrame:CGRectMake(0, y, 300, 31)];
txtTemp.tag = i;
[self setTextfieldStyle:txtTemp];
[self.view addSubview:txtTemp];
[txtTemp release];
y+=36;
}
This is UITextField's delegate method,
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(#"%#",textField.text);
}
Hope this works for you..
If you want to set attributes of a bunch of text fields in a loop, you could also do something like:
NSMutableArray* allTextFields = [NSMutableArray array];
// create the textFields here, I'm assuming you need to keep individual
// references to them.
txt_FirstName=[[UITextField alloc]init];
pTmpTextField.placeholder = #"First Name";
[allTextFields addObject:txt_FirstName];
// now add the common style stuff and frame
[allTextFields enumerateObjectsUsingBlock:^(UITextField* textField, NSUInteger idx, BOOL* stop) {
textField.frame = CGRectMake(framex1, framey1 + ( 50 * idx), framex2, framey2);
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = #"First Name";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
}];
when I press on it I can change position freely in View
-(IBAction) add :(id)sender {
CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight);
UITextField * textfieldToAdd = [[[UITextField alloc] initWithFrame:frame] autorelease];
textfieldToAdd.borderStyle = UITextBorderStyleRoundedRect;
textfieldToAdd.textColor = [UIColor blackColor];
textfieldToAdd.font = [UIFont systemFontOfSize:17.0];
textfieldToAdd.placeholder = #"";
textfieldToAdd.backgroundColor = [UIColor whiteColor];
textfieldToAdd.autocorrectionType = UITextAutocorrectionTypeNo ; // no auto correction support
textfieldToAdd.keyboardType = UIKeyboardTypeDefault; // use the default type input method (entire keyboard)
textfieldToAdd.returnKeyType = UIReturnKeyDone;
textfieldToAdd.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
textfieldToAdd.tag = kViewTag; // tag this control so we can remove it later for recycled cells
textfieldToAdd.delegate = self; // let us be the delegate so we know when the keyboard's "Done" button is pressed
// Add an accessibility label that describes what the text field is for.
[textfieldToAdd setAccessibilityLabel:NSLocalizedString(#"textfieldToAdd", #"")];
[self.view addSubview:textfieldToAdd];
}
First add the gestureRecognizer to your ViewDidLoad and then create the function
or better look here MoveME example
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panPiece:)];
[panGesture setMaximumNumberOfTouches:2];
[panGesture setDelegate:self];
[self addGestureRecognizer:panGesture];
- (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGPoint translation = [gestureRecognizer translationInView:self.view];
textfieldToAdd.center = CGPointMake([self center].x + translation.x, [self center].y + translation.y);
[gestureRecognizer setTranslation:CGPointZero inView:[self superview]];
}
}
i want dragging this textField
(void)viewDidLoad {
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(add:)] autorelease];
}
-(IBAction) add :(id)sender {
CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight);
UITextField * textfieldToAdd = [[[UITextField alloc] initWithFrame:frame] autorelease];
textfieldToAdd.borderStyle = UITextBorderStyleRoundedRect;
textfieldToAdd.textColor = [UIColor blackColor];
textfieldToAdd.font = [UIFont systemFontOfSize:17.0];
textfieldToAdd.placeholder = #"";
textfieldToAdd.backgroundColor = [UIColor whiteColor];
textfieldToAdd.autocorrectionType = UITextAutocorrectionTypeNo ; // no auto correction support
textfieldToAdd.keyboardType = UIKeyboardTypeDefault; // use the default type input method (entire keyboard)
textfieldToAdd.returnKeyType = UIReturnKeyDone;
textfieldToAdd.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
textfieldToAdd.tag = kViewTag; // tag this control so we can remove it later for recycled cells
textfieldToAdd.delegate = self; // let us be the delegate so we know when the keyboard's "Done" button is pressed
// Add an accessibility label that describes what the text field is for.
[textfieldToAdd setAccessibilityLabel:NSLocalizedString(#"textfieldToAdd", #"")];
[self.view addSubview:textfieldToAdd];
}
i would like to know configuration about UITextField standard please
- (IBAction)add:(id)sender
{
UITextField * textfieldToAdd = [[[UITextField alloc] init] autorelease];
// ... configuration code for textfield ...
[self.view addSubview:textfieldToAdd];
}
Here is a code from Apple's example UICatalog
#pragma mark -
#pragma mark Text Fields
- (UITextField *)textFieldNormal
{
if (textFieldNormal == nil)
{
CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight);
textFieldNormal = [[UITextField alloc] initWithFrame:frame];
textFieldNormal.borderStyle = UITextBorderStyleBezel;
textFieldNormal.textColor = [UIColor blackColor];
textFieldNormal.font = [UIFont systemFontOfSize:17.0];
textFieldNormal.placeholder = #"<enter text>";
textFieldNormal.backgroundColor = [UIColor whiteColor];
textFieldNormal.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
textFieldNormal.keyboardType = UIKeyboardTypeDefault; // use the default type input method (entire keyboard)
textFieldNormal.returnKeyType = UIReturnKeyDone;
textFieldNormal.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
textFieldNormal.tag = kViewTag; // tag this control so we can remove it later for recycled cells
textFieldNormal.delegate = self; // let us be the delegate so we know when the keyboard's "Done" button is pressed
// Add an accessibility label that describes what the text field is for.
[textFieldNormal setAccessibilityLabel:NSLocalizedString(#"NormalTextField", #"")];
}
return textFieldNormal;
}
- (UITextField *)textFieldRounded
{
if (textFieldRounded == nil)
{
CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight);
textFieldRounded = [[UITextField alloc] initWithFrame:frame];
textFieldRounded.borderStyle = UITextBorderStyleRoundedRect;
textFieldRounded.textColor = [UIColor blackColor];
textFieldRounded.font = [UIFont systemFontOfSize:17.0];
textFieldRounded.placeholder = #"<enter text>";
textFieldRounded.backgroundColor = [UIColor whiteColor];
textFieldRounded.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
textFieldRounded.keyboardType = UIKeyboardTypeDefault;
textFieldRounded.returnKeyType = UIReturnKeyDone;
textFieldRounded.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
textFieldRounded.tag = kViewTag; // tag this control so we can remove it later for recycled cells
textFieldRounded.delegate = self; // let us be the delegate so we know when the keyboard's "Done" button is pressed
// Add an accessibility label that describes what the text field is for.
[textFieldRounded setAccessibilityLabel:NSLocalizedString(#"RoundedTextField", #"")];
}
return textFieldRounded;
}
- (UITextField *)textFieldSecure
{
if (textFieldSecure == nil)
{
CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight);
textFieldSecure = [[UITextField alloc] initWithFrame:frame];
textFieldSecure.borderStyle = UITextBorderStyleBezel;
textFieldSecure.textColor = [UIColor blackColor];
textFieldSecure.font = [UIFont systemFontOfSize:17.0];
textFieldSecure.placeholder = #"<enter password>";
textFieldSecure.backgroundColor = [UIColor whiteColor];
textFieldSecure.keyboardType = UIKeyboardTypeDefault;
textFieldSecure.returnKeyType = UIReturnKeyDone;
textFieldSecure.secureTextEntry = YES; // make the text entry secure (bullets)
textFieldSecure.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
textFieldSecure.tag = kViewTag; // tag this control so we can remove it later for recycled cells
textFieldSecure.delegate = self; // let us be the delegate so we know when the keyboard's "Done" button is pressed
// Add an accessibility label that describes what the text field is for.
[textFieldSecure setAccessibilityLabel:NSLocalizedString(#"SecureTextField", #"")];
}
return textFieldSecure;
}
- (UITextField *)textFieldLeftView
{
if (textFieldLeftView == nil)
{
CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight);
textFieldLeftView = [[UITextField alloc] initWithFrame:frame];
textFieldLeftView.borderStyle = UITextBorderStyleBezel;
textFieldLeftView.textColor = [UIColor blackColor];
textFieldLeftView.font = [UIFont systemFontOfSize:17.0];
textFieldLeftView.placeholder = #"<enter text>";
textFieldLeftView.backgroundColor = [UIColor whiteColor];
textFieldLeftView.keyboardType = UIKeyboardTypeDefault;
textFieldLeftView.returnKeyType = UIReturnKeyDone;
textFieldLeftView.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
textFieldLeftView.tag = kViewTag; // tag this control so we can remove it later for recycled cells
// Add an accessibility label that describes the text field.
[textFieldLeftView setAccessibilityLabel:NSLocalizedString(#"CheckMarkIcon", #"")];
textFieldLeftView.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"segment_check.png"]];
textFieldLeftView.leftViewMode = UITextFieldViewModeAlways;
textFieldLeftView.delegate = self; // let us be the delegate so we know when the keyboard's "Done" button is pressed
}
return textFieldLeftView;
}
Go through the reference doc for UITextField Class also try to read UITextFieldDelegate. Look for the different tasks that can be performed and see the methods, properties available for it. And use it according to your requirement.
I try to create the uitextfield dynamically, but i can't create the textfield name.Anybody knows please help me.
from How do I create a text field?
To create a text field, use the UITextField class, as shown in Listing 11.
Listing 11: Creating a text field
CGRect textFieldFrame = CGRectMake(0.0, 0.0, 100.0, 30.0);
UITextField *textField = [[UITextField alloc] initWithFrame:textFieldFrame];
[textField setBorderStyle:UITextFieldBorderStyleBezel];
[textField setTag:1234];
[textField setTextColor:[UIColor blackColor]];
[textField setFont:[UIFont systemFontOfSize:20]];
[textField setDelegate:self];
[textField setPlaceholder:#"<enter text>"];
[textField setBackgroundColor:[UIColor whiteColor]];
textField.keyboardType = UIKeyboardTypeDefault;
Assign tag for each UITextField..
then access the textField using
UITextField *tf=(UITextField *)[yourView viewWithTag:tag];
for (int i=0; i<[checklistArray count]; i++) {
[self addUITextFieldMethod:i]
}
-(void) addUITextFieldMethod:(int)y {
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(20, 70*y+40, 280, 31)];
textField.placeholder = #"Click here to type";
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.textAlignment = NSTextAlignmentLeft;
textField.returnKeyType = UIReturnKeyDefault;
int DropDownTag = [[NSString stringWithFormat:#"10%d",y]integerValue];
textField.tag = DropDownTag ;
textField.delegate = self;
[textField addTarget:self action:#selector(returnFromKeyboard:) forControlEvents:UIControlEventEditingDidEndOnExit];
[scrollViewChecklist addSubview:textField];
}
int textFieldTagValue = textField.tag;
/****************Code In Your Method****************/
UITextField *myTextField = (UITextField *)[self.view viewWithTag:textFieldTagValue];
myTextField.text = [arrayTest objectAtIndex:indexPath.row];