如何在iPhone中以编程方式添加标签和文本框

时间:2011-01-28 12:34:52

标签: iphone objective-c ios ios4

如何以编程方式在iphone中添加标签和文本框? 如何为标签和文本框设置框架?

2 个答案:

答案 0 :(得分:16)

以下代码创建UILabelUITextField,并将它们添加到视图控制器视图中。在loadView方法或视图控制器中的某个位置添加此代码。

// Create Label
UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, 200, 40)];
[myLabel setBackgroundColor:[UIColor clearColor]];
[myLabel setText:@"Hi Label"];
[[self view] addSubview:myLabel];
[myLabel release];

// Create Text Field
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, 200, 40)];
[myTextField setBackgroundColor:[UIColor clearColor]];
[myTextField setText:@"Hi Text Field"];
[[self view] addSubview:myTextField];
[myTextField release];

您还可以使用setter方法设置其他属性。

答案 1 :(得分:1)

UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 29, 40)];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
[self.view addSubview:textField];

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 29, 40)];
label.text = @"Custom Label";
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:m_label];