美文网首页
按钮图片在上文字在下

按钮图片在上文字在下

作者: ljzza | 来源:发表于2018-03-28 13:48 被阅读0次

[self initButton:button];调用即可

//将按钮设置为图片在上,文字在下
-(void)initButton:(UIButton*)btn{
    float  spacing = 5;//图片和文字的上下间距
    CGSize imageSize = btn.imageView.frame.size;
    CGSize titleSize = btn.titleLabel.frame.size;
    CGSize textSize = [btn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName : btn.titleLabel.font}];
    CGSize frameSize = CGSizeMake(ceilf(textSize.width), ceilf(textSize.height));
    if (titleSize.width + 0.5 < frameSize.width) {
        titleSize.width = frameSize.width;
    }
    CGFloat totalHeight = (imageSize.height + titleSize.height + spacing);
    btn.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height), 0.0, 0.0, - titleSize.width);
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, - imageSize.width, - (totalHeight - titleSize.height), 0);
}

相关文章

网友评论

      本文标题:按钮图片在上文字在下

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