iOS 任意布局UIButton中的图片与文字
2017-08-04 本文已影响20人
JohnayXiao
- (void)setup
{
self.titleLabel.textAlignment = NSTextAlignmentCenter;
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setup];
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
// 调整图片
self.imageView.width = 20;
self.imageView.height = self.imageView.width;
self.imageView.x = (self.width - self.imageView.width) / 2;
self.imageView.y = (self.height - self.imageView.width) / 2 - 10;
// 调整文字
self.titleLabel.x = 0;
self.titleLabel.y = self.imageView.bottom + 5;
self.titleLabel.width = self.width;
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.height = 20;
}