在UIButton中的图像下标签

我试图创build一个button,其中有一些文字下方的图标(像应用程序buttonsorta),但它似乎是相当困难的实现。 任何想法,我怎么能得到的文字显示下面的图像与UIButton?

在Xcode中,您可以简单地将边缘标题左侧插入设置为负的图像宽度。 这将在图像的中心显示标签。

要使标签显示在图像下方(类似于应用程序button),您可能需要将边缘标题顶部插入设置为某个正数。

希望这有助于某人。

或者你可以使用这个类别:

ObjC

@interface UIButton (VerticalLayout) - (void)centerVerticallyWithPadding:(float)padding; - (void)centerVertically; @end @implementation UIButton (VerticalLayout) - (void)centerVerticallyWithPadding:(float)padding { CGSize imageSize = self.imageView.frame.size; CGSize titleSize = self.titleLabel.frame.size; CGFloat totalHeight = (imageSize.height + titleSize.height + padding); self.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height), 0.0f, 0.0f, - titleSize.width); self.titleEdgeInsets = UIEdgeInsetsMake(0.0f, - imageSize.width, - (totalHeight - titleSize.height), 0.0f); self.contentEdgeInsets = UIEdgeInsetsMake(0.0f, 0.0f, titleSize.height, 0.0f); } - (void)centerVertically { const CGFloat kDefaultPadding = 6.0f; [self centerVerticallyWithPadding:kDefaultPadding]; } @end 

Swift扩展

 extension UIButton { func centerVertically(padding: CGFloat = 6.0) { guard let imageViewSize = self.imageView?.frame.size, let titleLabelSize = self.titleLabel?.frame.size else { return } let totalHeight = imageViewSize.height + titleLabelSize.height + padding self.imageEdgeInsets = UIEdgeInsets( top: -(totalHeight - imageViewSize.height), left: 0.0, bottom: 0.0, right: -titleLabelSize.width ) self.titleEdgeInsets = UIEdgeInsets( top: 0.0, left: -imageViewSize.width, bottom: -(totalHeight - titleLabelSize.height), right: 0.0 ) self.contentEdgeInsets = UIEdgeInsets( top: 0.0, left: 0.0, bottom: titleLabelSize.height, right: 0.0 ) } } 

子类UIButton。 覆盖-layoutSubviews将内置的子视图移动到新的位置:

 - (void)layoutSubviews { [super layoutSubviews]; CGRect frame = self.imageView.frame; frame = CGRectMake(truncf((self.bounds.size.width - frame.size.width) / 2), 0.0f, frame.size.width, frame.size.height); self.imageView.frame = frame; frame = self.titleLabel.frame; frame = CGRectMake(truncf((self.bounds.size.width - frame.size.width) / 2), self.bounds.size.height - frame.size.height, frame.size.width, frame.size.height); self.titleLabel.frame = frame; } 

这是“与我同在”在斯威夫特的伟大答案 。

 extension UIButton { func alignImageAndTitleVertically(padding: CGFloat = 6.0) { let imageSize = self.imageView!.frame.size let titleSize = self.titleLabel!.frame.size let totalHeight = imageSize.height + titleSize.height + padding self.imageEdgeInsets = UIEdgeInsets( top: -(totalHeight - imageSize.height), left: 0, bottom: 0, right: -titleSize.width ) self.titleEdgeInsets = UIEdgeInsets( top: 0, left: -imageSize.width, bottom: -(totalHeight - titleSize.height), right: 0 ) } } 

这是一个简单的居中标题button在Swift中通过覆盖titleRect(forContentRect:)imageRect(forContentRect:) 。 它还实现了与AutoLayout一起使用的intrinsicContentSize。

 import UIKit class CenteredButton: UIButton { override func titleRect(forContentRect contentRect: CGRect) -> CGRect { let rect = super.titleRect(forContentRect: contentRect) return CGRect(x: 0, y: contentRect.height - rect.height + 5, width: contentRect.width, height: rect.height) } override func imageRect(forContentRect contentRect: CGRect) -> CGRect { let rect = super.imageRect(forContentRect: contentRect) let titleRect = self.titleRect(forContentRect: contentRect) return CGRect(x: contentRect.width/2.0 - rect.width/2.0, y: (contentRect.height - titleRect.height)/2.0 - rect.height/2.0, width: rect.width, height: rect.height) } override var intrinsicContentSize: CGSize { let size = super.intrinsicContentSize if let image = imageView?.image { var labelHeight: CGFloat = 0.0 if let size = titleLabel?.sizeThatFits(CGSize(width: self.contentRect(forBounds: self.bounds).width, height: CGFloat.greatestFiniteMagnitude)) { labelHeight = size.height } return CGSize(width: size.width, height: image.size.height + labelHeight + 5) } return size } override init(frame: CGRect) { super.init(frame: frame) centerTitleLabel() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) centerTitleLabel() } private func centerTitleLabel() { self.titleLabel?.textAlignment = .center } } 

如果您inheritance了UIButton并覆盖了layoutSubviews,则可以使用下面的代码来居中显示图像,并将标题置于其下方:

kTextTopPadding是一个常量,你将不得不介绍,它决定了图像和它下面的文本之间的空间。

 -(void)layoutSubviews { [super layoutSubviews]; // Move the image to the top and center it horizontally CGRect imageFrame = self.imageView.frame; imageFrame.origin.y = 0; imageFrame.origin.x = (self.frame.size.width / 2) - (imageFrame.size.width / 2); self.imageView.frame = imageFrame; // Adjust the label size to fit the text, and move it below the image CGRect titleLabelFrame = self.titleLabel.frame; CGSize labelSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font constrainedToSize:CGSizeMake(self.frame.size.width, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping]; titleLabelFrame.size.width = labelSize.width; titleLabelFrame.size.height = labelSize.height; titleLabelFrame.origin.x = (self.frame.size.width / 2) - (labelSize.width / 2); titleLabelFrame.origin.y = self.imageView.frame.origin.y + self.imageView.frame.size.height + kTextTopPadding; self.titleLabel.frame = titleLabelFrame; } 

这是Erik W优秀答案的修改版本。 但不是将图像置于视图的顶部,而是将图像和标签集中在视图中作为一个组。

区别在于:

 +-----------+ | ( ) | | Hello | // Erik W's code | | | | +-----------+ 

VS

 +-----------+ | | | ( ) | // My modified version | Hello | | | +-----------+ 

来源如下:

 -(void)layoutSubviews { [super layoutSubviews]; CGRect titleLabelFrame = self.titleLabel.frame; CGSize labelSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font constrainedToSize:CGSizeMake(self.frame.size.width, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping]; CGRect imageFrame = self.imageView.frame; CGSize fitBoxSize = (CGSize){.height = labelSize.height + kTextTopPadding + imageFrame.size.height, .width = MAX(imageFrame.size.width, labelSize.width)}; CGRect fitBoxRect = CGRectInset(self.bounds, (self.bounds.size.width - fitBoxSize.width)/2, (self.bounds.size.height - fitBoxSize.height)/2); imageFrame.origin.y = fitBoxRect.origin.y; imageFrame.origin.x = CGRectGetMidX(fitBoxRect) - (imageFrame.size.width/2); self.imageView.frame = imageFrame; // Adjust the label size to fit the text, and move it below the image titleLabelFrame.size.width = labelSize.width; titleLabelFrame.size.height = labelSize.height; titleLabelFrame.origin.x = (self.frame.size.width / 2) - (labelSize.width / 2); titleLabelFrame.origin.y = fitBoxRect.origin.y + imageFrame.size.height + kTextTopPadding; self.titleLabel.frame = titleLabelFrame; } 

仅供参考:当与UIViewanimation结合使用时,可能会中断它们,因为在它们之间调用layoutSubviews。

纠正了其中一个答案:

Swift 3:

 class CenteredButton: UIButton { override func titleRect(forContentRect contentRect: CGRect) -> CGRect { let rect = super.titleRect(forContentRect: contentRect) let imageRect = super.imageRect(forContentRect: contentRect) return CGRect(x: 0, y: imageRect.maxY + 10, width: contentRect.width, height: rect.height) } override func imageRect(forContentRect contentRect: CGRect) -> CGRect { let rect = super.imageRect(forContentRect: contentRect) let titleRect = self.titleRect(forContentRect: contentRect) return CGRect(x: contentRect.width/2.0 - rect.width/2.0, y: (contentRect.height - titleRect.height)/2.0 - rect.height/2.0, width: rect.width, height: rect.height) } override init(frame: CGRect) { super.init(frame: frame) centerTitleLabel() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) centerTitleLabel() } private func centerTitleLabel() { self.titleLabel?.textAlignment = .center } } 

Dave在Swift中的解决scheme:

 override func layoutSubviews() { super.layoutSubviews() if let imageView = self.imageView { imageView.frame.origin.x = (self.bounds.size.width - imageView.frame.size.width) / 2.0 imageView.frame.origin.y = 0.0 } if let titleLabel = self.titleLabel { titleLabel.frame.origin.x = (self.bounds.size.width - titleLabel.frame.size.width) / 2.0 titleLabel.frame.origin.y = self.bounds.size.height - titleLabel.frame.size.height } } 

更新了Kenny Winker的回答,因为sizeWithFont在iOS 7中已被弃用。

 -(void)layoutSubviews { [super layoutSubviews]; int kTextTopPadding = 3; CGRect titleLabelFrame = self.titleLabel.frame; CGRect labelSize = [self.titleLabel.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGRectGetHeight(self.bounds)) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.titleLabel.font} context:nil]; CGRect imageFrame = self.imageView.frame; CGSize fitBoxSize = (CGSize){.height = labelSize.size.height + kTextTopPadding + imageFrame.size.height, .width = MAX(imageFrame.size.width, labelSize.size.width)}; CGRect fitBoxRect = CGRectInset(self.bounds, (self.bounds.size.width - fitBoxSize.width)/2, (self.bounds.size.height - fitBoxSize.height)/2); imageFrame.origin.y = fitBoxRect.origin.y; imageFrame.origin.x = CGRectGetMidX(fitBoxRect) - (imageFrame.size.width/2); self.imageView.frame = imageFrame; // Adjust the label size to fit the text, and move it below the image titleLabelFrame.size.width = labelSize.size.width; titleLabelFrame.size.height = labelSize.size.height; titleLabelFrame.origin.x = (self.frame.size.width / 2) - (labelSize.size.width / 2); titleLabelFrame.origin.y = fitBoxRect.origin.y + imageFrame.size.height + kTextTopPadding; self.titleLabel.frame = titleLabelFrame; } 

使用Kenny Winker和simeon的代码,我使这个代码适合我。

 import UIKit @IBDesignable class TopIconButton: UIButton { override func layoutSubviews() { super.layoutSubviews() let kTextTopPadding:CGFloat = 3.0; var titleLabelFrame = self.titleLabel!.frame; let labelSize = titleLabel!.sizeThatFits(CGSizeMake(CGRectGetWidth(self.contentRectForBounds(self.bounds)), CGFloat.max)) var imageFrame = self.imageView!.frame; let fitBoxSize = CGSizeMake(max(imageFrame.size.width, labelSize.width), labelSize.height + kTextTopPadding + imageFrame.size. height) let fitBoxRect = CGRectInset(self.bounds, (self.bounds.size.width - fitBoxSize.width)/2, (self.bounds.size.height - fitBoxSize. height)/2); imageFrame.origin.y = fitBoxRect.origin.y; imageFrame.origin.x = CGRectGetMidX(fitBoxRect) - (imageFrame.size.width/2); self.imageView!.frame = imageFrame; // Adjust the label size to fit the text, and move it below the image titleLabelFrame.size.width = labelSize.width; titleLabelFrame.size.height = labelSize.height; titleLabelFrame.origin.x = (self.frame.size.width / 2) - (labelSize.width / 2); titleLabelFrame.origin.y = fitBoxRect.origin.y + imageFrame.size.height + kTextTopPadding; self.titleLabel!.frame = titleLabelFrame; self.titleLabel!.textAlignment = .Center } } 

重构的icecrystal23答案。

Swift 3,与自动布局,Xib,故事板,可以animation。

在原来冰晶23的答案button有一个严重的计算框架。 我想我解决了这个问题。

 import UIKit class VerticallyButton: UIButton { var padding: CGFloat = 20.0 { didSet { setNeedsLayout() } } override var intrinsicContentSize: CGSize { let maxSize = CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude) if let titleSize = titleLabel?.sizeThatFits(maxSize), let imageSize = imageView?.sizeThatFits(maxSize) { let width = ceil(max(imageSize.width, titleSize.width)) let height = ceil(imageSize.height + titleSize.height + padding) return CGSize(width: width, height: height) } return super.intrinsicContentSize } override func layoutSubviews() { if let image = imageView?.image, let title = titleLabel?.attributedText { let imageSize = image.size let titleSize = title.size() titleEdgeInsets = UIEdgeInsetsMake(0.0, -imageSize.width, -(imageSize.height + padding), 0.0) imageEdgeInsets = UIEdgeInsetsMake(-(titleSize.height + padding), 0.0, 0.0, -titleSize.width) } super.layoutSubviews() } } 

您只需根据图像和标题标签的大小调整所有三个边缘插页:

 button.contentEdgeInsets = UIEdgeInsetsMake(0, 0, titleLabelBounds.height + 4, 0) button.titleEdgeInsets = UIEdgeInsetsMake(image.size.height + 8, -image.size.width, 0, 0) button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -titleLabelBounds.width) 

设置文本后,您可以通过调用sizeToFit来获得标题边界。 不pipe文本,字体和图像的大小,水平间距应该工作,但我不知道一个单一的解决scheme,以获得垂直间距和底部内容边缘插入一致。

这里是“熊与我”的答案作为Swift 2.0一个子类。 要使用它,只需将Interface Builderbutton类更改为VerticalButton ,它会奇迹般地更新预览。

我也更新了它来计算自动布局的正确内在内容大小。

 import UIKit @IBDesignable class VerticalButton: UIButton { @IBInspectable var padding: CGFloat = 8 override func prepareForInterfaceBuilder() { super.prepareForInterfaceBuilder() update() } override func layoutSubviews() { super.layoutSubviews() update() } func update() { let imageBounds = self.imageView!.bounds let titleBounds = self.titleLabel!.bounds let totalHeight = CGRectGetHeight(imageBounds) + padding + CGRectGetHeight(titleBounds) self.imageEdgeInsets = UIEdgeInsets( top: -(totalHeight - CGRectGetHeight(imageBounds)), left: 0, bottom: 0, right: -CGRectGetWidth(titleBounds) ) self.titleEdgeInsets = UIEdgeInsets( top: 0, left: -CGRectGetWidth(imageBounds), bottom: -(totalHeight - CGRectGetHeight(titleBounds)), right: 0 ) } override func intrinsicContentSize() -> CGSize { let imageBounds = self.imageView!.bounds let titleBounds = self.titleLabel!.bounds let width = CGRectGetWidth(imageBounds) > CGRectGetWidth(titleBounds) ? CGRectGetWidth(imageBounds) : CGRectGetWidth(titleBounds) let height = CGRectGetHeight(imageBounds) + padding + CGRectGetHeight(titleBounds) return CGSizeMake(width, height) } } 

我认为最好的方法之一是通过inheritanceUIButton并重写一些呈现方法:

 - (void)awakeFromNib { [super awakeFromNib]; [self setupSubViews]; } - (instancetype)init { if (self = [super init]) { [self setupSubViews]; } return self; } - (void)setupSubViews { [self.imageView setTranslatesAutoresizingMaskIntoConstraints:NO]; [self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.imageView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]]; [self.titleLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[imageView][titleLabel]|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:@{@"imageView": self.imageView, @"titleLabel": self.titleLabel}]]; } - (CGSize)intrinsicContentSize { CGSize imageSize = self.imageView.image.size; CGSize titleSize = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName: self.titleLabel.font}]; return CGSizeMake(MAX(imageSize.width, titleSize.width), imageSize.height + titleSize.height); } 

我在这里把这些答案结合起来,在Swift中提出了一个似乎正在为我工​​作的问题。 我不喜欢如何超越插入,但它的作品。 我会接受build议的改进意见。 它似乎与sizeToFit()和自动布局正确工作。

 import UIKit /// A button that displays an image centered above the title. This implementation /// only works when both an image and title are set, and ignores /// any changes you make to edge insets. class CenteredButton: UIButton { let padding: CGFloat = 0.0 override func layoutSubviews() { if imageView?.image != nil && titleLabel?.text != nil { let imageSize: CGSize = imageView!.image!.size titleEdgeInsets = UIEdgeInsetsMake(0.0, -imageSize.width, -(imageSize.height + padding), 0.0) let labelString = NSString(string: titleLabel!.text!) let titleSize = labelString.sizeWithAttributes([NSFontAttributeName: titleLabel!.font]) imageEdgeInsets = UIEdgeInsetsMake(-(titleSize.height + padding), 0.0, 0.0, -titleSize.width) let edgeOffset = abs(titleSize.height - imageSize.height) / 2.0; contentEdgeInsets = UIEdgeInsetsMake(edgeOffset, 0.0, edgeOffset, 0.0) } super.layoutSubviews() } override func sizeThatFits(size: CGSize) -> CGSize { let defaultSize = super.sizeThatFits(size) if let titleSize = titleLabel?.sizeThatFits(size), let imageSize = imageView?.sizeThatFits(size) { return CGSize(width: ceil(max(imageSize.width, titleSize.width)), height: ceil(imageSize.height + titleSize.height + padding)) } return defaultSize } override func intrinsicContentSize() -> CGSize { let size = sizeThatFits(CGSize(width: CGFloat.max, height: CGFloat.max)) return size } } 

这是我解决这个问题的UIButton子类:

 @implementation MyVerticalButton @synthesize titleAtBottom; // BOOL property - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.titleAtBottom = YES; } return self; } - (CGSize)sizeThatFits:(CGSize)size { self.titleLabel.text = [self titleForState: self.state]; UIEdgeInsets imageInsets = self.imageEdgeInsets; UIEdgeInsets titleInsets = self.titleEdgeInsets; CGSize imageSize = [self imageForState: self.state].size; if (!CGSizeEqualToSize(imageSize, CGSizeZero)) { imageSize.width += imageInsets.left + imageInsets.right; imageSize.height += imageInsets.top + imageInsets.bottom; } CGSize textSize = [self.titleLabel sizeThatFits: CGSizeMake(size.width - titleInsets.left - titleInsets.right, size.height -(imageSize.width + titleInsets.top+titleInsets.bottom))]; if (!CGSizeEqualToSize(textSize, CGSizeZero)) { textSize.width += titleInsets.left + titleInsets.right; textSize.height += titleInsets.top + titleInsets.bottom; } CGSize result = CGSizeMake(MAX(textSize.width, imageSize.width), textSize.height + imageSize.height); return result; } - (void)layoutSubviews { // needed to update all properities of child views: [super layoutSubviews]; CGRect bounds = self.bounds; CGRect titleFrame = UIEdgeInsetsInsetRect(bounds, self.titleEdgeInsets); CGRect imageFrame = UIEdgeInsetsInsetRect(bounds, self.imageEdgeInsets); if (self.titleAtBottom) { CGFloat titleHeight = [self.titleLabel sizeThatFits: titleFrame.size].height; titleFrame.origin.y = CGRectGetMaxY(titleFrame)-titleHeight; titleFrame.size.height = titleHeight; titleFrame = CGRectStandardize(titleFrame); self.titleLabel.frame = titleFrame; CGFloat imageBottom = CGRectGetMinY(titleFrame)-(self.titleEdgeInsets.top+self.imageEdgeInsets.bottom); imageFrame.size.height = imageBottom - CGRectGetMinY(imageFrame); self.imageView.frame = CGRectStandardize(imageFrame); } else { CGFloat titleHeight = [self.titleLabel sizeThatFits: titleFrame.size].height; titleFrame.size.height = titleHeight; titleFrame = CGRectStandardize(titleFrame); self.titleLabel.frame = titleFrame; CGFloat imageTop = CGRectGetMaxY(titleFrame)+(self.titleEdgeInsets.bottom+self.imageEdgeInsets.top); imageFrame.size.height = CGRectGetMaxY(imageFrame) - imageTop; self.imageView.frame = CGRectStandardize(imageFrame); } } - (void)setTitleAtBottom:(BOOL)newTitleAtBottom { if (titleAtBottom!=newTitleAtBottom) { titleAtBottom=newTitleAtBottom; [self setNeedsLayout]; } } @end 

而已。 像魅力一样工作。 问题可能会出现,如果button将很小,以适应标题和文字。

@Tiago我改变你的答案是这样的。 它的工作正常与所有大小

 func alignImageAndTitleVertically(padding: CGFloat = 5.0) { self.sizeToFit() let imageSize = self.imageView!.frame.size let titleSize = self.titleLabel!.frame.size let totalHeight = imageSize.height + titleSize.height + padding self.imageEdgeInsets = UIEdgeInsets( top: -(totalHeight - imageSize.height), left: 0, bottom: 0, right: -titleSize.width ) self.titleEdgeInsets = UIEdgeInsets( top: 0, left: 0, bottom: -(totalHeight - titleSize.height), right: titleSize.height ) } 

要精确控制大小和自动布局,你可以试试这个: https://github.com/albert-zhang/AZCenterLabelButton : https://github.com/albert-zhang/AZCenterLabelButton ( Link )

我使用了Chris提出的边缘插入方法,但由于button宽度小于图像和button标题的总宽度,标题显示为3个点(…)。

由于布局原因,我无法增加button的宽度。

我通过将Line Break属性更改为“Clip”来解决这个问题。 这使得标题在图像下方下移。 然后我用插页纠正了它的位置。 只有当上述问题存在时,这才起作用。

UIButton子类中是这样的

 public override func layoutSubviews() { super.layoutSubviews() imageEdgeInsets = UIEdgeInsetsMake(-10, 0, 0, 0) titleEdgeInsets = UIEdgeInsetsMake(0, -bounds.size.width/2 - 10, -30, 0) } 

它非常简单。

而不是这个:

  button.setImage(UIImage(named: "image"), forState: .Normal) 

用这个:

  button.setBackgroundImage(UIImage(named: "image", forState: .Normal) 

然后,您可以轻松地使用button添加文本:

// button.titleLabel!.font = UIFont(name:“FontName”,size:30)

  button.setTitle("TitleText", forState: UIControlState.Normal) 

Objective-C中的@simeon 解决scheme

 #import "CenteredButton.h" @implementation CenteredButton - (CGRect)titleRectForContentRect:(CGRect)contentRect { CGRect rect = [super titleRectForContentRect: contentRect]; return CGRectMake(0, contentRect.size.height - rect.size.height - 5, contentRect.size.width, rect.size.height); } - (CGRect)imageRectForContentRect:(CGRect)contentRect { CGRect rect = [super imageRectForContentRect: contentRect]; CGRect titleRect = [self titleRectForContentRect: contentRect]; return CGRectMake(contentRect.size.width / 2.0 - rect.size.width / 2.0, (contentRect.size.height - titleRect.size.height)/2.0 - rect.size.height/2.0, rect.size.width, rect.size.height); } - (CGSize)intrinsicContentSize { CGSize imageSize = [super intrinsicContentSize]; if (self.imageView.image) { UIImage* image = self.imageView.image; CGFloat labelHeight = 0.0; CGSize labelSize = [self.titleLabel sizeThatFits: CGSizeMake([self contentRectForBounds: self.bounds].size.width, CGFLOAT_MAX)]; if (CGSizeEqualToSize(imageSize, labelSize)) { labelHeight = imageSize.height; } return CGSizeMake(MAX(labelSize.width, imageSize.width), image.size.height + labelHeight + 5); } return imageSize; } - (id) initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self centerTitleLabel]; } return self; } - (id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { [self centerTitleLabel]; } return self; } - (void)centerTitleLabel { self.titleLabel.textAlignment = NSTextAlignmentCenter; } @end 

制作一张带有文字的图片,并将该图片用于您的button。