ios 以编程方式创建文本字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15690308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:56:13  来源:igfitidea点击:

Creating Text Fields programmatically

iosobjective-c

提问by Johnny Dahdah

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.

我正在使用 Xcode 4.6.1。我想知道如何在同一个视图控制器上创建各种文本字段,它们之间用一些空格分隔,并以编程方式设置文本。

回答by u.gen

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

}