Skip to content

Instantly share code, notes, and snippets.

@altyus
Last active August 29, 2015 13:58
Show Gist options
  • Save altyus/10299517 to your computer and use it in GitHub Desktop.
Save altyus/10299517 to your computer and use it in GitHub Desktop.
Center two labels in view with Visual Format Language
self.stepLabel = [[UILabel alloc] init];
self.stepLabel.text = @"0 Steps";
self.stepLabel.textAlignment = NSTextAlignmentCenter;
self.stepLabel.font = [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:50];
self.historicalLabel = [[UILabel alloc] init];
self.historicalLabel.text = @"Historical";
self.historicalLabel.textAlignment = NSTextAlignmentCenter;
self.historicalLabel.font = [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:50];
[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.stepLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.historicalLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:self.stepLabel];
[self.view addSubview:self.historicalLabel];
NSDictionary *views = @{@"superview" : self.view,
@"stepLabel" : self.stepLabel,
@"historicalLabel" : self.historicalLabel};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[superview]-(<=1)-[stepLabel]" options:NSLayoutFormatAlignAllCenterY metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[superview]-(<=1)-[stepLabel]-(20)-[historicalLabel]" options:NSLayoutFormatAlignAllCenterX metrics:nil views:views]];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment