首页 > 代码库 > iOS·UIButton如何文字在下图片在上

iOS·UIButton如何文字在下图片在上

创建子类继承自UIButton,在layoutSubviews方法中改变文字和图片的位置就可以了,同理,稍作改变,可以写出文字在上图片在下.本文只给出文字在下图片在上的代码

 

 1 -(void)layoutSubviews {
 2     [super layoutSubviews];
 3     
 4     // image center
 5     CGPoint center;
 6     center.x = self.frame.size.width/2;
 7     center.y = self.imageView.frame.size.height/2;
 8     self.imageView.center = center;
 9     
10     //text
11     CGRect newFrame = [self titleLabel].frame;
12     newFrame.origin.x = 0;
13     newFrame.origin.y = self.imageView.frame.size.height + 5;
14     newFrame.size.width = self.frame.size.width;
15     
16     self.titleLabel.frame = newFrame;
17     self.titleLabel.textAlignment = NSTextAlignmentCenter;
18 }

 

效果:

技术分享

 

技术分享

 

iOS·UIButton如何文字在下图片在上