UIButton 图片居上文字居下

UIButton扩展一个方法:

spacing 图片和文字的间隔

- (void)verticalImageAndTitle:(CGFloat)spacing

{


    CGSize imageSize = self.imageView.frame.size;

    CGSize titleSize = self.titleLabel.frame.size;

    CGSize textSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font];

    CGSizeframeSize =CGSizeMake(ceilf(textSize.width),ceilf(textSize.height));

    if(titleSize.width+0.5< frameSize.width) {

        titleSize.width= frameSize.width;

    }

    CGFloattotalHeight = (imageSize.height+ titleSize.height+ spacing);

    self.imageEdgeInsets=UIEdgeInsetsMake(- (totalHeight - imageSize.height),0.0,0.0, - titleSize.width);

    self.titleEdgeInsets=UIEdgeInsetsMake(0, - imageSize.width, - (totalHeight - titleSize.height),0);

}

你可能感兴趣的:(UIButton 图片居上文字居下)