UIButton图文上下对齐

- (void)centerImageAndTitle:(float)spacing

{

    // get the size of the elements here for readability

    CGSize imageSize = self.imageBtn.imageView.frame.size;

    CGSize titleSize = self.imageBtn.titleLabel.frame.size;

    

    // get the height they will take up as a unit

    CGFloat totalHeight = (imageSize.height + titleSize.height + spacing);

    

    // raise the image and push it right to center it

    self.imageBtn.imageEdgeInsets = UIEdgeInsetsMake(

                                            - (totalHeight - imageSize.height), 0.0, 0.0, - titleSize.width);

    

    // lower the text and push it left to center it

    self.imageBtn.titleEdgeInsets = UIEdgeInsetsMake(

                                            0.0, - imageSize.width, - (totalHeight - titleSize.height), 0.0);

}

 

- (void)centerImageAndTitle

{

    const int DEFAULT_SPACING = 6.0f;

    [self centerImageAndTitle:DEFAULT_SPACING];

}

posted on 2015-04-16 10:37  jack_yan  阅读(605)  评论(0编辑  收藏  举报