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.
Related
I encounter a problem is that I need a formatted grid which contain serious of UISwitch, and my solution is to put those UISwitches on each UILabel .
However, these switches can't operate normally. Any suggestions?
Below is my code:
-(void) drawSummaryColumn : (UILabel *) myLabel :(NSString *) title
{
NSArray *sHeaders = #[NSLocalizedString(#"Tue", nil),
NSLocalizedString(#"Wed", nil),
NSLocalizedString(#"Thu", nil),
NSLocalizedString(#"Fri", nil),
NSLocalizedString(#"Sat", nil),
NSLocalizedString(#"Sun", nil),
NSLocalizedString(#"Total", nil)];
float basizSize = myLabel.frame.size.width;
CGRect r;
CGRect pos;
pos.origin.x =8;
pos.origin.y = 3;
UILabel *firstCol = [[UILabel alloc] initWithFrame:CGRectZero];
r.origin.x =0.5;
r.origin.y =0.5;
r.size = CGSizeMake(120, 40);
firstCol.text =title;
firstCol.font = [ UIFont fontWithName :#"Georgia-Bold" size : 17];
firstCol.backgroundColor = [UIColor whiteColor];
firstCol.textAlignment = NSTextAlignmentCenter;
firstCol.frame =r ;
[myLabel addSubview:firstCol ];
basizSize -= 120;
basizSize = (float) basizSize / 7;
for (NSString *label in sHeaders)
{
UISwitch *objSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
objSwitch.frame = pos;
[objSwitch addTarget:self action:#selector(switchAction:) forControlEvents:UIControlEventValueChanged];
UILabel *columns = [[UILabel alloc] initWithFrame:CGRectZero];
columns.backgroundColor = [UIColor whiteColor];
columns.textAlignment = NSTextAlignmentCenter;
[columns addSubview:objSwitch];
r.origin.x += r.size.width + 0.5f;
r.size = CGSizeMake(basizSize, 40);
columns.frame =r ;
[myLabel addSubview:columns];
}
}
try
myLabel.userInteractionEnabled = YES;
firstCol.userInteractionEnabled = YES;
columns.userInteractionEnabled = YES;
by default, a label's userInteraction property is false. Hence all it's subviews(including uiswitch) has this property set to FALSE initially. parent view (uilabel) needs to have this property ENABLED so that it's subviews also get enabled for user interaction.
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;
}];
I have this textField
who doing text on center?///////////////////////////////////
textPredmet = [[UITextField alloc] initWithFrame:CGRectMake(20, 55, 200, 40)];
textPredmet.borderStyle = UITextBorderStyleNone;
textPredmet.textColor = [UIColor blackColor]; //text color
textPredmet.font = [UIFont systemFontOfSize:15.0]; //font size
[textPredmet drawTextInRect:CGRectMake( 30,55,200,40)];
textPredmet.placeholder = #"Введите текст"; //place holder
textPredmet.backgroundColor = [UIColor clearColor]; //background color
textPredmet.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
textPredmet.keyboardType = UIKeyboardTypeDefault; // type of the keyboard
textPredmet.returnKeyType = UIReturnKeyDone; // type of the return key
textPredmet.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
textPredmet.delegate = self; // let us be the delegate so we know when the keyboard's "Done" button is pressed
textPredmet.background = [UIImage imageNamed:#"input.png"];
textPredmet.textAlignment=UITextAlignmentLeft+1;
[self.view addSubview:textPredmet];
Set below line to your code.
textPredmet.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
It will set the text center vertically.
textPredmet.textAlignment = UITextAlignmentCenter;
It will set the text center horizontally.
Also, remove below lines from your code.
[textPredmet drawTextInRect:CGRectMake( 30,55,200,40)];
I guess, UITextAlignmentLeft+1 == UITextAlignmentCenter
I have a UITextField inside of a UITableView cell. When I rotate the device, the textfield is still the same width it was for Portrait mode. I know that I need to use autoresizing to make the field fill the cell when I am in landscape, but I am not sure how to do this, as I have tried and failed.
How would I do this?
cell.textLabel.text = #"Email";
UITextField *field2 = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, width, 30)];
field2.adjustsFontSizeToFitWidth = YES;
field2.textColor = [UIColor blackColor];
field2.keyboardType = UIKeyboardTypeEmailAddress;
field2.returnKeyType = UIReturnKeyNext;
field2.placeholder = #"Required";
field2.tag = 3;
field2.delegate = self;
field2.autoresizingMask = UIViewAutoresizingFlexibleWidth;
field2.backgroundColor = [UIColor blueColor];
// Finish building cell
field2.autocorrectionType = UITextAutocorrectionTypeNo;
field2.autocapitalizationType = UITextAutocapitalizationTypeNone;
field2.textAlignment = UITextAlignmentLeft;
[field2 addTarget:self action:#selector(updateFields:) forControlEvents:UIControlEventEditingChanged];
field2.text = email;
self.emailField = field2;
[cell addSubview:field2];
[field2 release];
Check if you tableView has Auto Resize subivew is set to YES.
Check you are returning YES for all the supported rotations.
(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:
(NSTimeInterval)duration{
}
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];
}