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)];
PengOne / Alex Lockwood's answer is simple and useful. If you intend to support rotation, add the following to keep it centered:
Use NSTextAlignmentCenter if what you have is the label already set and you want to center its content.
If the label is a subview of view:
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.
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];
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.
I think that should do what you are asking for.
How about: