Skip to content

Instantly share code, notes, and snippets.

@sdpjswl
Last active August 29, 2015 14:18
Show Gist options
  • Save sdpjswl/ae8162ad500e1bdd4dcf to your computer and use it in GitHub Desktop.
Save sdpjswl/ae8162ad500e1bdd4dcf to your computer and use it in GitHub Desktop.
Programatically adjust title and image insets of button so that they appear centred and title below the image
- (void)fixTitleAndImageInsetsForButton:(UIButton *)btn {
// the space between the image and text
CGFloat spacing = 6.0;
// lower the text and push it left so it appears centered
// below the image
CGSize imageSize = btn.imageView.image.size;
btn.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, - (imageSize.height + spacing), 0.0);
// raise the image and push it right so it appears centered
// above the text
CGSize titleSize = [btn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName: btn.titleLabel.font}];
btn.imageEdgeInsets = UIEdgeInsetsMake(- (titleSize.height + spacing), 0.0, 0.0, - titleSize.width);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment