Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel set to center of view

How come the UILabel drawn in this code is not in the center of the view?

//create the view and make it gray
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor darkGrayColor];

//everything for label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,42,21)];

//set text of label
NSString *welcomeMessage = [@"Welcome, " stringByAppendingString:@"username"];
welcomeMessage = [welcomeMessage stringByAppendingString:@"!"];
label.text = welcomeMessage;

//set color
label.backgroundColor = [UIColor darkGrayColor];
label.textColor = [UIColor whiteColor];

//properties
label.textAlignment = NSTextAlignmentCenter;
[label sizeToFit];

//add the components to the view
[view addSubview: label];
label.center = view.center;

//show the view
self.view = view;

The line, label.center = view.center; should move the label to the center of the view. But instead moves it to where the center of the label is in the left hand corner of the view as shown below.

screenshot
(source: gyazo.com)

Does anyone know why?

like image 775
Dummy Code Avatar asked Jun 05 '13 20:06

Dummy Code


People also ask

How do I center an image and label in Swift?

This can be achieved by creating a container view for the image view and the text label and then centering the container view in the original parent view. This should work for a label with dynamic content. Add constraints on the container view so that its width is the sum of that of the image view and the label.

How do I center a label in Xcode?

click on Align button on bottom toolbar in IB then select Horizontal or Vertical center. First you might want to fix width and height as well, if you want to preserve size.

How to center label in UIView?

Use NSTextAlignmentCenter if what you have is the label already set and you want to center its content. This is the best answer, having the label frame is centered is not enough for centering the text, very useful if your labels are localized!


2 Answers

You need to init your view with a frame:

UIView *view = [[UIView alloc] initWithFrame:self.view.frame];
like image 103
Corey Avatar answered Sep 24 '22 12:09

Corey


This is caused by your view variable not having a frame defined. By default it has a frame set to (0, 0, 0, 0), so its center is (0, 0).

Hence when you do label.center = view.center;, you set the center of your label to (0 - label.width / 2, 0 - label.height /2). (-80.5 -10.5; 161 21) in your case.

There is no need for a new UIView if your UIViewController already have one, just work with self.view.

- (void)viewDidLoad
{
    [super viewDidLoad];

    //create the view and make it gray
    self.view.backgroundColor = [UIColor darkGrayColor];

    //everything for label
    UILabel *label = [[UILabel alloc] init];

    //set text of label
    // stringWithFormat is useful in this case ;)
    NSString *welcomeMessage = [NSString stringWithFormat:@"Welcome, %@!", @"username"];
    label.text = welcomeMessage;

    //set color
    label.backgroundColor = [UIColor darkGrayColor];
    label.textColor = [UIColor whiteColor];

    //properties
    label.textAlignment = NSTextAlignmentCenter;
    [label sizeToFit];

    //add the components to the view
    [self.view addSubview: label];
    label.center = self.view.center;
}

Also note that doing label.center = self.view.center will not work properly when rotating to landscape mode.

like image 37
Guillaume Algis Avatar answered Sep 25 '22 12:09

Guillaume Algis