0

Like, CGRectMake(), but have it specify the center, both horizontally and vertically?

Doug Smith
  • 29,668
  • 57
  • 204
  • 388
  • You want the frame to be the same size as the superview, then center it? http://stackoverflow.com/questions/1054558/vertically-align-text-within-a-uilabel?rq=1 – rog May 19 '13 at 20:33

3 Answers3

3
  1. Get the dimensions of the containing view.
  2. Get the dimensions of the view to be inserted.
  3. x_position = width_of_containing_view / 2.0 - width_of_inserted_view / 2.0
  4. y_position = height_of_containing_view / 2.0 - height_of_inserted_view / 2.0
Hot Licks
  • 47,103
  • 17
  • 93
  • 151
0

You can also do label.center = [superview convertPoint:superview.center fromView:superview.superview]; =P

fumoboy007
  • 5,345
  • 4
  • 32
  • 49
0

Set the label's center property to the center point of it's superview's bounds. Since the superview's center and frame properties are in coordinate space of their superview, those properties aren't helpful (unless you do the conversion like fumoboy does).

label.center = CGRectGetMidPoint(label.superview.bounds);

I made this function for convenience:

CGPoint CGRectGetMidPoint(CGRect rect)
{
    return CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
}
MaxGabriel
  • 7,617
  • 4
  • 35
  • 82