Centering a label in a UIView
What’s the best way to center a label in a
UIView? If you do something such as
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(view.frame.origin.x / 2, view.frame.origin.y / 2, 50.0, 50.0)];
Then you’re setting the origin point of the label to the center of the view. The best bet would be to set the center of the view to that point using the center property. So I tried using the following code:
UIView *aView = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]; aView.backgroundColor = [UIColor darkGrayColor]; CGRect frame = aView.frame; UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 125.0f, 30.0f)]; [aLabel setCenter:CGPointMake(frame.origin.x / 2, frame.origin.y / 2)];
That yields a label which is pretty much outside the bounds of the view in the upper left hand corner.
- Ask for User Permission to Receive UILocalNotifications in iOS 8
- Independent Volume control of AVAudioPlayer and MPMusicPlayerController in iOS App.
- UIDatePicker locale does nothing?
- Draw triangle cutout for selection indication in UIView
- Is NSNotificationCenter removeObserver in ARC needed?
- Custom Input View in Swift
2 Solutions Collect From Internet About “Centering a label in a UIView”
The main thing you are doing wrong is taking half the origin values, rather than half the sizes
However, you don’t need to even calculate that in this case – just do something like the following:
UIView *aView = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]; aView.backgroundColor = [UIColor darkGrayColor]; UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 125, 30)]; aLabel.center = aView.center;
(note, you don’t need to force those coordinates to floats – in this case writing them as ints seems more readable).
Also, it’s a matter of style – but since you’re already using property syntax (
aView.backgroundColor) you may as well use it for the center property too 🙂
To position any child horizontally centered in a parent you would calculate its position like so;
childX = (parentWidth - childWidth) / 2
(This also applies to height).
- Undo with multitouch drawing in iOS
- What is most common and correct practice to get a CGFloat from an NSNumber?
- iOS – indexPathForRowAtPoint don't return correct indexPath with different cell height
- iOS Swift – UITableViewCell Custom Subclass not displaying content
- UIView animation jumps at beginning
- Copy Text with Formatting – iOS 6 NSAttributedString to Pasteboard
- How to resize a UIModalPresentationFormSheet?
- Adding UICollectionView inside UIView without Storyboards
- UIWebview not openning the Installed Application but opens in safari
- Why we can't change FetchRequest at NSFetchedResultsController?
- Is it possible to access a photo's geotag metadata from within the simulator?
- NSURLRequest hanging SpringBoard
- Can't find SBJson library in XCode 4.2
- Custom UITableViewCell in edit mode does not move my UILabels
- Storyboard prepareForSegue