title: Visually centering a UILabel to it's superview when the actual center looks off
body: <p>I have a container <code>UIView</code>, and a <code>UILabel</code>. The container view size is set, I then add text to the <code>UILabel</code>, call <code>sizeToFit</code> on the label, and add the label as a subview of the container view.</p>
  
  <p>I then use the following code to center the label inside the container view:</p>
  
  <pre><code>viewCountLabel.frame = CGRectMake(viewCountContainer.frame.size.width/2 - viewCountLabel.frame.size.width/2, viewCountContainer.frame.size.height/2 - viewCountLabel.frame.size.height/2, viewCountLabel.frame.size.width, viewCountLabel.frame.size.height);
  </code></pre>
  
  <p>This is pretty standard code to center something to it's superview/container, and it always works pretty well.</p>
  
  <p>However, right now I have a small problem. This label's text is a simple number. It could be 1, 9, 15, 22, etc.</p>
  
  <p>My problem is that when I center everything, some numbers look off center even though they technically are centered perfectly.</p>
  
  <p>Here's an example pic with borders for both the container and the label so you can see that they are centered perfectly: </p>
  
  <p><img src="https://i.stack.imgur.com/Y4vTE.png" alt="enter image description here"></p>
  
  <p>But here's how it looks without the borders:</p>
  
  <p><img src="https://i.stack.imgur.com/9IFzi.png" alt="enter image description here"></p>
  
  <p>The <code>15</code> does not look like it's centered even though it technically is. It looks like its a little too far to the right. If I subtract 2 or 3 from the x value, it will visually appear centered, but see that's the problem.</p>
  
  <p>How can you setup logic to "visually center" various values for a label like this? I feel like setting a bunch of <code>if</code> statements based on the text value is overkill and only applying a bandaid to the problem.</p>
  
  <p>I have also tried using <code>NSTextAlignmentCenter</code> and that does not make a difference. I'm not sure if I'm using <code>NSTextAlignmentCenter</code> incorrectly, or if there's a potential problem with <code>sizeToFit</code>. </p>
  
  <p>You'll notice that that label's blue border has extra space on all sides, but most importantly for this problem it has extra space on the left and right sides. I feel like that could be causing this issue, but I'm not sure how to fix that or if that's even the problem.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>// Setup view count label container
  UIView *viewCountContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 95, 95)];
  viewCountContainer.frame = CGRectMake(self.view.frame.size.width/2 - viewCountContainer.frame.size.width/2, 87, viewCountContainer.frame.size.width, viewCountContainer.frame.size.height);
  
  
  // Setup + add number label to container
  UILabel *viewCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.pushModal.frame.size.width, 95)];
  viewCountLabel.textAlignment = NSTextAlignmentCenter;
  viewCountLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:72];
  viewCountLabel.textColor = [UIColor colorWithRed:0.278 green:0.278 blue:0.278 alpha:1];
  viewCountLabel.text = [NSString stringWithFormat:@"%d", viewCount];
  [viewCountLabel sizeToFit];
  
  // Center the label to it's super view (viewCountContainer)
  viewCountLabel.frame = CGRectMake(viewCountContainer.frame.size.width/2 - viewCountLabel.frame.size.width/2, viewCountContainer.frame.size.height/2 - viewCountLabel.frame.size.height/2, viewCountLabel.frame.size.width, viewCountLabel.frame.size.height);
  
  [viewCountContainer addSubview:viewCountLabel];
  [self.view addSubview:viewCountContainer];
  </code></pre>