2015-09-04 45 views
0

这是我作为iOS开发人员的第二天工作。到目前为止,我非常喜欢编码目标-C。我怎样才能让我的1线标签在父母的中心,同时在顶部?中心顶级标签iOS Objective-C

我迄今所做的:

  1. 学会了如何运用字体大小和字体颜色。通过编程学习了解如何创建ui。学会了向ui添加标签。
  2. 居中标签,如在父母的中心。
  3. 设置标签numberOfLines 0

这里是我的代码:

- (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

的[如何中心一个UILabel可能重复在UIView](http://stackoverflow.com/questions/6753794/how-to-center-a-uilabel-on-uiview) –

+0

set ** [self.label setCenter:CGPointMake(view.frame.size.width/2,view.frame.size.height/2)] ** –

+0

谢谢安布。你的解决方案与下面的答案完全一样,但我理解Saheb的回答比你的回答更好。再次感谢! – user5295097

回答

1

如果标签上有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; 
+0

嘿非常感谢你给了我这个想法,让我对中心的X轴我的标签!谢谢,谢谢。我正在逐步学习。并感谢你们没有投下我的问题:) – user5295097

+0

很高兴它帮助你,祝你工作顺利:) –