使用setImageEdgeInSet和setTitleEdgeInSet让按钮的图片和文字垂直居中上下显示
2011-11-30 22:27
591 查看
使用setImageEdgeInSet和setTitleEdgeInSet让按钮的图片和文字垂直居中上下显示
今天完成了这个,整理为公用代码,分享一下,调用时只要设置好按钮图片和文字后,直接调用 centerImageAndTitle 即可 #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface UIButton (UIButtonExt) - (void)centerImageAndTitle:(float)space; - (void)centerImageAndTitle; @end @implementation UIButton (UIButtonExt) - (void)centerImageAndTitle:(float)spacing { // get the size of the elements here for readability CGSize imageSize = self.imageView.frame.size; CGSize titleSize = self.titleLabel.frame.size; // get the height they will take up as a unit CGFloat totalHeight = (imageSize.height + titleSize.height + spacing); // raise the image and push it right to center it self.imageEdgeInsets = UIEdgeInsetsMake( - (totalHeight - imageSize.height), 0.0, 0.0, - titleSize.width); // lower the text and push it left to center it self.titleEdgeInsets = UIEdgeInsetsMake( 0.0, - imageSize.width, - (totalHeight - titleSize.height), 0.0); } - (void)centerImageAndTitle { const int DEFAULT_SPACING = 6.0f; [self centerImageAndTitle:DEFAULT_SPACING]; } @end
相关文章推荐
- Delphi7如何实现让Tedit显示文字垂直居中(上下居中)
- ios 开发中,如何设置 uitabbar 里面 tabbaritem 不显示文字,只显示图片,图片垂直居中?
- input文本框、文字、按钮、图片 垂直居中对齐的解决办法
- 自定义按钮,是图片和文字水平居中显示
- input文本框、文字、按钮、图片 垂直居中对齐
- html使用icon 并使图片和文字垂直居中
- iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐
- 一屏只显示横向排列的四张图片,如何控制点击“向下”按钮过快,导致出现图片不能在一屏中垂直居中显示?
- Delphi DBGridEh 嵌入图片,显示文字,且上下左右居中
- 需求 - 19 - 按钮图片文字上下居中
- css垂直居中怎么设置?文字上下居中和图片垂直居中
- input文本框、文字、按钮、图片 垂直居中对齐的解决办法
- 如何设置图片、文字在容器(div)中垂直居中的显示?
- 多行文字垂直居中显示,图片居中
- UIButton 设置图片和文字上下居中显示
- 图片右侧文字垂直居中显示
- 如何使带背景图片的Button按钮中的文字居中偏上显示
- input文本框、文字、按钮、图片 垂直居中对齐的解决办法
- 按钮图片和文字上下居中对齐
- 利用css将文字和图片水平垂直居中显示