这是我作为iOS开发人员的新工作的第二天。到目前为止,我真的很喜欢编码目标-C。如何让我的1行标签成为父母的中心,同时在顶部呢?
到目前为止我做了什么:
这是我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// loading the viewcontroller, load the controllers/objects.
// set background and title of the view
[self.view setBackgroundColor:RGB(19,181,234)];
self.title = @"UI Main";
// add the label1
UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, 200, 40)];
[myLabel setBackgroundColor:[UIColor clearColor]];
myLabel.textColor = RGB(255,255,255);
[myLabel setText:@"Enter Amount"];
myLabel.numberOfLines = 0;
[myLabel setFont:[UIFont fontWithName:@"DejaVu Sans" size:10]];
[myLabel sizeToFit];
[[self view] addSubview:myLabel];
// [myLabel release];
}
这是我的模拟器截图:http://i.stack.imgur.com/AN2JX.png
编辑:我的问题是获取父视图的大小,以便我可以集中控件。但它已经回答了!谢谢!
答案 0 :(得分:1)
如果您的Label具有self.view的超级视图,或者您希望您的标签位于self.view(UIView)的中心,请更改此
int WidthOfLabel = 100;
int someHeight = 50;
UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake((self.view.frame.size.width/2)-(widthOfLabel/2), 80, widthOfLabel, someHeight)];
myLabel.textAlignment = NSTextAlignmentCenter;