Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Text Fields programmatically [closed]

I'm using Xcode 4.6.1. I want to know how can I create various text fields on the same view controller, separated by some space between them, with the text set programmatically.

like image 221
Johnny Dahdah Avatar asked Mar 28 '13 19:03

Johnny Dahdah


People also ask

What is UITextField?

An object that displays an editable text area in your interface.


1 Answers

Havent test this code but your question is fairly simple, quick google would give you the results.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //first one
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(45, 30, 200, 40)];
    tf.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
    tf.font = [UIFont fontWithName:@"Helvetica-Bold" size:25];
    tf.backgroundColor=[UIColor whiteColor];
    tf.text=@"Hello World";

    //second one
    UITextField *tf1 = [[UITextField alloc] initWithFrame:CGRectMake(45, tf.frame.origin.y+75, 200, 40)];
    tf1.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
    tf1.font = [UIFont fontWithName:@"Helvetica-Bold" size:25];
    tf1.backgroundColor=[UIColor whiteColor];
    tf1.text=@"second field";

    //and so on adjust your view size according to your needs
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 400, 400)];
    [view addSubview:tf];
    [view addSubview:tf1];

    [self.view addSubview:view];

}
like image 148
u.gen Avatar answered Nov 11 '22 15:11

u.gen