IOS 按钮 文字图片的位置

-(void)initButton:(UIButton*)btn{

    float  spacing =3;//图片和文字的上下间距

    CGSize imageSize = btn.imageView.frame.size;

    CGSize titleSize = btn.titleLabel.frame.size;

    CGSize textSize = [btn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName : btn.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);

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

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


}

你可能感兴趣的:(IOS 按钮 文字图片的位置)