how to create uitextfield dynamically using for loop - iphone

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];

Related

How can set right margin in UItextfield and also placeholder text?

I tried to set the right side margin in UItextfield but I can't get success for this.
I tried below code:
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(1, 1, 60, 25) ];
[lbl setText:#"Partenza:"];
[lbl setFont:[UIFont fontWithName:#"Verdana" size:12]];
[lbl setTextColor:[UIColor grayColor]];
[lbl setTextAlignment:UITextAlignmentLeft];
txtFCarat.rightView = lbl;
txtFCarat.rightViewMode = UITextFieldViewModeAlways;
txtFCarat.delegate = self;
Please help me if any body have a solution for this!
You can override this below two methods
#implementation rightTextField
// placeholder position
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectInset( self.bounds , margin position , margin position );
}
// text position
- (CGRect)editingRectForBounds:(CGRect)bounds {
return CGRectInset( self.bounds , margin position ,margin position );
}
Edit: use to bind code like
textField.bounds = [self editingRectForBounds:textField.bounds];
textField.bounds = [self textRectForBounds:textField.bounds];
Your expected Answer:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"icon.png"]];
textfield.rightView = imageView;
textfield.rightViewMode = UITextFieldViewModeAlways;
Add the below line where you are creating textfield..and set value as per requirement
textField.layer.sublayerTransform = CATransform3DMakeTranslation(-20, 000, 0);
Another way to achieve this by overriding the textRectForBounds
Let me to give complete Description how to do this or achieve it
First you create a class and name "textField"(or what ever you Want) and must be sure that IT IS SUBCLASS OF UITextField.
2.Now open the textfield.m class and use this code or paste below code in this class (textfield):
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectMake(bounds.origin.x + 50, bounds.origin.y,
bounds.size.width - 20, bounds.size.height);
}
-(CGRect)editingRectForBounds:(CGRect)bounds {
return [self textRectForBounds:bounds];
}
If you want to customize the textfield more then you can use here only or in calling Class like
use the below code :Note it is only for testing purpose.it depend on your requirement
- (void)settingTextField {
[self setTextField];
[self setKeyboardAppearance:UIKeyboardAppearanceAlert];
}
- (void)setTextField {
[self setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[self setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[self setTextAlignment:NSTextAlignmentCenter];
[self setValue:[UIColor colorWithRed:120.0/225.0 green:120.0/225.0 blue:120.0/225.0 alpha:1.0] forKeyPath:#"_placeholderLabel.textColor"];
[self setFont:[UIFont boldSystemFontOfSize:15.0f]];
}
3.improt textField.h class into your view-controller where you wants to create textfield
and you call it like this.
#import "textField.h"
//Viewcontroller Class
- (void)viewDidLoad
{
[super viewDidLoad];
textField *field1 = [[textField alloc] initWithFrame:CGRectMake(50, 50, 200, 20)];
field1.backgroundColor=[UIColor greenColor];
field1.placeholder=#"0";
[self.view addSubview:field1];
}
Try to adjust the value textRectForBounds as per you reqitement
NSString *myString = #"Partenza:";
UIFont *myFont = [UIFont fontWithName:#"Verdana" size:12];
CGSize myStringSize = [myString sizeWithFont:myFont];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, myStringSize.width + 10, 25) ];
[lbl setText:myString];
[lbl setFont:[UIFont fontWithName:#"Verdana" size:12]];
[lbl setTextColor:[UIColor grayColor]];
[lbl setTextAlignment:UITextAlignmentLeft];
txtFCarat.rightView = lbl;
txtFCarat.rightViewMode = UITextFieldViewModeAlways;

Make UINavigationBar title be user editable

How do I make UINavigationBar title to be user editable, I've tried setting the titleView to be a textfield however it doesn't look the same.. It's missing that outline or drop shadow. I'm aiming for it to look like the default one.
This is what i'm implementing at the moment:
_titleField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 26)];
[_titleField setDelegate:self];
_titleField.text = _bugDoc.data.title;
_titleField.font = [UIFont boldSystemFontOfSize:20];
_titleField.textColor = [UIColor whiteColor];
_titleField.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = _titleField;
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadTitle];
}
- (void)loadTitle
{
txtField_navTitle = [[UITextField alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x,7,self.view.bounds.size.width,31)];
txtField_navTitle.delegate = self;
[txtField_navTitle setBackgroundColor:[UIColor clearColor]];
txtField_navTitle.textColor = [UIColor whiteColor];
txtField_navTitle.textAlignment = UITextAlignmentCenter;
txtField_navTitle.borderStyle = UITextBorderStyleNone;
txtField_navTitle.font = [UIFont boldSystemFontOfSize:20];
txtField_navTitle.autocorrectionType = UITextAutocorrectionTypeNo;
txtField_navTitle.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.navigationItem setTitleView:txtField_navTitle];
txtField_navTitle.layer.masksToBounds = NO;
txtField_navTitle.layer.shadowColor = [UIColor whiteColor].CGColor;
txtField_navTitle.layer.shadowOpacity = 0;
[txtField_navTitle release];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
self.title = textField.text;
return YES;
}
Please dont forget to #import <QuartzCore/QuartzCore.h>
Try this way this works for me.
// Custom Navigation Title.
UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 40)];
tlabel.text=self.title;
tlabel.textAlignment=NSTextAlignmentCenter;
tlabel.font = [UIFont boldSystemFontOfSize:17.0];
tlabel.textColor=[UIColor colorWithRed:7.0/255.0 green:26.0/255.0 blue:66.0/255.0 alpha:1.0];
tlabel.backgroundColor =[UIColor clearColor];
tlabel.adjustsFontSizeToFitWidth=YES;
self.navigationItem.titleView=tlabel;

Creating custom UITextView with UILabels as well as text in it

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.

Vertically align text in a UILabel

I am new to iPhone,
How do I vertically align my text in the UILabel?
Here is my Code snippet,
UILabel *lbl=[[UILabel alloc]init];
lbl.frame=CGRectMake(10,10,100,100);
lbl.backgroundColor=[UIColor clearColor];
lbl.font=[UIFont systemFontOfSize:13];
lbl.text=#"123456";
[scrollVw addSubview:lbl];
text displayed is in the format of 123456 but i want text should be display vertically like,
6
5
4
3
2
1
Any help will be appriciated.
Its impossible to align the text in UILabel vertically. But, you can dynamically change the height of the label using sizeWithFont: method of NSString, and just set its x and y as you want.
As an alternative you can use UITextField. It supports the contentVerticalAlignment peoperty as it is a subclass of UIControl. You have to set its userInteractionEnabled to NO to prevent user from typing text on it.
EDIT 1
Well instead of a UILabel , Make a UITableView like :-
TableActivityLevel=[[UITableView alloc] initWithFrame:CGRectMake(224, 203, 27, 0) style:UITableViewStylePlain];
TableActivityLevel.delegate=self;
TableActivityLevel.dataSource=self;
TableActivityLevel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
TableActivityLevel.rowHeight = 17;
TableActivityLevel.backgroundColor=[UIColor clearColor];
TableActivityLevel.layer.borderColor = [[UIColor clearColor]CGColor];
TableActivityLevel.separatorStyle = UITableViewCellSeparatorStyleNone;
EDIT 2 Found the method using UILabels too !! :) Check this out....
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 10.0, 100.0)];
[label1 setBackgroundColor:[UIColor clearColor]];
[label1 setNumberOfLines:0];
[label1 setCenter:self.view.center];
[label1 setFont:[UIFont fontWithName:#"Helvetica" size:12.0]];
[label1 setTextColor:[UIColor whiteColor]];
[label1 setTextAlignment:UITextAlignmentCenter];
[label1 setText:#"1 2 3 4 5 6"];
[self.view addSubview:label1];
If it just a single lined text as in your example you can use various workarounds. I personally would create a UILabel subclass as follows,
#implementation VerticalLabel
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.numberOfLines = 0;
}
return self;
}
- (void)setText:(NSString *)text {
NSMutableString *newString = [NSMutableString string];
for (int i = text.length-1; i >= 0; i--) {
[newString appendFormat:#"%c\n", [text characterAtIndex:i]];
}
super.text = newString;
}
#end
You now just have to replace
UILabel *lbl = [[UILabel alloc] init];
with
UILabel *lbl = [[VerticalLabel alloc] init];
to get vertical text.
Hi the following code is work well
UILabel *lbl=[[UILabel alloc]init];
lbl.frame=CGRectMake(10,10,10,300);
lbl.backgroundColor=[UIColor clearColor];
lbl.numberOfLines=6; // Use one to 6 numbers
lbl.font=[UIFont systemFontOfSize:13];
lbl.text=#"123456";
NSString *reverse=lbl.text;
NSMutableString *reversedString = [NSMutableString string];
NSInteger charIndex = [reverse length];
while (charIndex > 0) {
charIndex--;
NSRange subStrRange = NSMakeRange(charIndex, 1);
[reversedString appendString:[reverse substringWithRange:subStrRange]];
}
NSLog(#"%#", reversedString);
CGSize labelSize = [lbl.text sizeWithFont:lbl.font
constrainedToSize:lbl.frame.size
lineBreakMode:lbl.lineBreakMode];
lbl.frame = CGRectMake(
lbl.frame.origin.x, lbl.frame.origin.y,
lbl.frame.size.width, labelSize.height);
lbl.text=reversedString;
[scrollview addSubview:lbl];
UILabel *lbl=[[UILabel alloc]init];
lbl.frame=CGRectMake(10,10,10,100);
lbl.backgroundColor=[UIColor clearColor];
lbl.font=[UIFont systemFontOfSize:13];
lbl.text=#"123456";
lbl.numberOfLines = 10;
[self.view addSubview:lbl];
You cant do vertical allignment but use another way to display it
UILabel *lbl=[[UILabel alloc]init];
lbl.frame=CGRectMake(10,10,100,400);
lbl.backgroundColor=[UIColor clearColor];
lbl.font=[UIFont systemFontOfSize:13];
lbl.text=#"1\n2\n3\n4\n5\n6";
[scrollVw addSubview:lbl];

how to acces the textfield value created manually from a Click on Button

i am doing sample application where i am creating two TextField programitically i.e,Textfield1 and Textfield2 and Having one Button which is Dragged on UI.
Now My requirement is I have two textfield created manually and a Button, When i click a button it should access the value entered in both textfield and display its value on console using NSLog.
so can anyone suggest me how to do it?As textifeild is created programmitically it is not having IBOutlet. so i want to read textfield value when i click button and display on Console.so please suggest me with sample code
{
[self Textfiled1];
[Self TextField2];
}
-(void)textField1
{
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 250, 200, 35)];
textField.returnKeyType = UIReturnKeyDone;
textField.adjustsFontSizeToFitWidth = TRUE;
[textField addTarget:self
action:#selector(textFieldDone:)
forControlEvents:UIControlEventEditingDidEndOnExit];
[self.view addSubview:textField];
[textField release];
}
Same as above for Textfield2.
Set tag for both the UITextField.
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 250, 200, 35)];
textField.returnKeyType = UIReturnKeyDone;
textField.tag = 1;
...
for the second,
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 250, 200, 35)];
textField.returnKeyType = UIReturnKeyDone;
textField.tag = 2;
...
In Button click action method,
UITextField *textField = (UITextField*)[self.view viewWithTag:1];
NSString *text1 = textField.text;
Similarly for the next UITextField.
Set the text field' s tag property before adding it and get the text field back using it.
textField.tag = 1;
UITextField* tf1 = (UITextField*)[self.view viewWithTag:1];
when you are adding textfield to self.view use the tag like below.
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 250, 200, 35)];
textField.returnKeyType = UIReturnKeyDone;
textField.adjustsFontSizeToFitWidth = TRUE;
[textField addTarget:self
action:#selector(textFieldDone:)
forControlEvents:UIControlEventEditingDidEndOnExit];
textField.tag = 3;
[self.view addSubview:textField];
[textField release];
whenever you need it then do like this.
UITextField *tex = [self.view viewWithTag:3];
NSLog(#"%#",tex.text);