美文网首页
iOS 任意布局UIButton中的图片与文字

iOS 任意布局UIButton中的图片与文字

作者: JohnayXiao | 来源:发表于2017-08-04 09:02 被阅读20次
- (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;
}

相关文章

网友评论

      本文标题:iOS 任意布局UIButton中的图片与文字

      本文链接:https://www.haomeiwen.com/subject/auxvlxtx.html