中心顶级标签iOS Objective-C

时间:2015-09-04 05:24:08

标签: ios objective-c

这是我作为iOS开发人员的新工作的第二天。到目前为止,我真的很喜欢编码目标-C。如何让我的1行标签成为父母的中心,同时在顶部呢?

到目前为止我做了什么:

  1. 学习了如何应用字体大小和字体颜色。
  2. 学会了以编程方式创建ui。
  3. 学会了向ui添加标签。
  4. 以标签为中心,如在父母的中心。
  5. 将标签numberOfLines设置为0.
  6. 这是我的代码:

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

    编辑:我的问题是获取父视图的大小,以便我可以集中控件。但它已经回答了!谢谢!

1 个答案:

答案 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;