自定义按钮之:文字图片位置随意定制

https://www.jianshu.com/p/547a21ad9993


#import "TSSquareButton.h"

@implementation TSSquareButton

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // 设置图片的其他属性
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont systemFontOfSize:12.0];
        [self setBackgroundColor:[UIColor whiteColor]];
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    return self;
}

// 重写layoutSubviews方法,手动设置按钮子控件的位置
- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.imageView.TS_width = self.TS_width * 0.4;
    self.imageView.TS_height = self.imageView.TS_width;
    self.imageView.TS_y = self.TS_height * 0.25;
    self.imageView.TS_centerX = self.TS_width * 0.5;
    
    self.titleLabel.TS_width = self.TS_width;
    self.titleLabel.TS_y = self.imageView.TS_buttom ;
    self.titleLabel.TS_height = 25;
    self.titleLabel.TS_x = 0;
}

@end


你可能感兴趣的:(自定义按钮之:文字图片位置随意定制)