How can I center the UILabel on the UIView? I am using the following code
float width = weatherView.bounds.size.width;
float height = weatherView.bounds.size.height;
[self.label setFrame:CGRectMake(width-100,height-100, 100, 100)];
How can I center the UILabel on the UIView? I am using the following code
float width = weatherView.bounds.size.width;
float height = weatherView.bounds.size.height;
[self.label setFrame:CGRectMake(width-100,height-100, 100, 100)];
How about:
[self.label setCenter:view.center];
If the label is a subview of view:
[self.label setCenter:CGPointMake(view.frame.size.width / 2, view.frame.size.height / 2)]
Don't use view.center
unless the label and view have the same superview.
If there is no immediate connection, then you'll have to transform view's center to the coordinate space of label's parent. Then you could use PengOne's answer with the transformed point.
Use NSTextAlignmentCenter if what you have is the label already set and you want to center its content.
cell.menuLabel.textAlignment = NSTextAlignmentCenter;
A better posible solution is:
First: If you are doing this programmatically you'll need to initialize with frame and some customizations:
// Simple example
int yPosition = 10;
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0, yPosition, self.view.frame.size.width, 0)];
[label setText: @"Testing..."];
[label setBackgroundColor: [UIColor clearColor]];
[label setNumberOfLines: 0];
[label sizeToFit];
Second: If a label what you are trying to center you can just run this after setNumberOflines
selector called and 0 value
assigned, your text will have all lines needed, and sizeToFit
method called to have a good customization, and finally:
[self.label setCenter: CGPointMake(self.view.center.x, self.label.center.y)];
This will center only the X axis
, and the Y axis
will stay as you desired in the frame initialization.
PS: It's also valid if not a UILabel
but depends on the control you are using will need another simple customization or neither, and if you only want to center programmatically but interface builder
designed, just need to run the second code
.
Something like this should do the trick... Make sure that your label is set to have centered alignment and is sufficiently big/wide to handle your text string.
float viewWidth = weatherView.frame.size.width;
float viewHeight = weatherView.frame.size.height;
float labelWidth = label.frame.size.width;
float labelHeight = label.frame.size.height;
float xpos = (viewWidth/2.0f) - (labelWidth/2.0f);
float ypos = (viewHeight/2.0f) - (labelHeight/2.0f);
[label setFrame:CGRectMake(xpos,ypos,labelWidth,labelHeight)];
I think that should do what you are asking for.
There are two possibility if your UILabel is added via .xib or else added programmatically .
If first case i.e added via .xib then you can set the position from xib file size inspector tab with the 'Arrange' property
And if second case persist then you can set as --- [self.label setCenter:view.center];
PengOne / Alex Lockwood's answer is simple and useful. If you intend to support rotation, add the following to keep it centered:
[self.myLabel setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth];