tags:

views:

21

answers:

1

I've got a view which is shown in landscape mode and I want to add a label which is int he center of the view both horizontal and vertically.

    CGRect frame = CGRectMake(0, 0, 400, 44);
    UILabel *lab = [[[UILabel alloc] initWithFrame:frame] autorelease];
    lab.font = [UIFont boldSystemFontOfSize:16.0];
    lab.textAlignment = UITextAlignmentCenter;
    lab.text = @"No data!";
    [self.view addSubview:lab];

However, its not the right width and its at the top of the view ?

+2  A: 

The texts are vertically center aligned by default. If you make the frame height equal to super view (which covers whole window, I assume) height then it will be vertically center aligned in that view.

For horizontal alignment you need to set textAlignment property which you are already setting correctly. But you need to fix the frame width which should be 480.

CGRect frame = CGRectMake(0, 0, 480, 320);
taskinoor