J-LO
J-LO

Reputation: 229

How do I center UILabel TEXT horizontally?

Now I've seen a lot of examples that teach you either how to center the UILabel Text in the middle of the screen, but that's not what I'm asking for. I'm asking on how to make sure that the text of UILabel remains half way between the left and right bounds of the screen and All I have to do is make adjustments Vertically. I have the following:

self.aboutMee = [[UILabel alloc] initWithFrame:CGRectMake(0, 400, 120, self.view.bounds.size.width)];
            self.aboutMee.text = @"About Me";
            self.aboutMee.textAlignment = NSTextAlignmentCenter;
            self.aboutMee.backgroundColor = [UIColor clearColor];
            self.aboutMee.font = [UIFont fontWithName:@"Helvetica" size:14];
            [self.aboutMee setTextColor:[UIColor colorWithRed:0.0/255.0 green:140.0/255.0 blue:199.0/255.0 alpha:1.0]];
            [self.aboutMee setFont:[UIFont fontWithName:@"HelveticaNeueLTStd-ThCn" size:35]];
            [self.scrollView addSubview:self.aboutMee];

Now I've tried to make the entire label 320 in width and then just center the text, but that doesn't work. Any ideas?

Upvotes: 3

Views: 13658

Answers (1)

Guntis Treulands
Guntis Treulands

Reputation: 4762

You should set frame correctly: CGRectMake(pos.x, pos.y, width, height)

self.aboutMee = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        self.aboutMee.text = @"About Me";
        self.aboutMee.textAlignment = NSTextAlignmentCenter;
        self.aboutMee.backgroundColor = [UIColor clearColor];
        self.aboutMee.font = [UIFont fontWithName:@"Helvetica" size:14];
        [self.aboutMee setTextColor:[UIColor colorWithRed:0.0/255.0 green:140.0/255.0 blue:199.0/255.0 alpha:1.0]];
        [self.aboutMee setFont:[UIFont fontWithName:@"HelveticaNeueLTStd-ThCn" size:35]];
        [self.scrollView addSubview:self.aboutMee];

Upvotes: 9

Related Questions