iOS UIButton上下排列(上图下文)

 
 

1.

 

-(void)setButtonContentCenter:(UIButton *) btn

 

{

    

    CGSize imgViewSize,titleSize,btnSize;

    

    UIEdgeInsets imageViewEdge,titleEdge;

    

    CGFloat heightSpace = 10.0f;

    

    

    

    //设置按钮内边距

    

    imgViewSize = btn.imageView.bounds.size;

    

    titleSize = btn.titleLabel.bounds.size;

    

    btnSize = btn.bounds.size;

    

    

    

    imageViewEdge = UIEdgeInsetsMake(heightSpace,0.0, btnSize.height -imgViewSize.height - heightSpace, - titleSize.width);

    

    [btn setImageEdgeInsets:imageViewEdge];

    

    titleEdge = UIEdgeInsetsMake(imgViewSize.height +heightSpace, - imgViewSize.width, 0.0, 0.0);

    

    [btn setTitleEdgeInsets:titleEdge];

    

}

2.

-(void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat midX = self.frame.size.width / 2;
    CGFloat midY = self.frame.size.height/ 2 ;
    self.titleLabel.center = CGPointMake(midX, midY + 15);
    self.imageView.center = CGPointMake(midX, midY - 10);
}

3.

    self.titleLabel.backgroundColor = [UIColor greenColor];
    CGSize imageSize = CGSizeMake(self.imageView.frame.size.width-15*RatioWidth, self.imageView.frame.size.height-15*RatioHeight);
    CGSize titleSize = self.titleLabel.frame.size;
    CGSize textSize = [self.titleLabel.text sizeWithFont:self.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);
    self.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height), 0.0, 0.0, - titleSize.width);
    self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageSize.width, - (totalHeight - titleSize.height), 0);

4.

    CGSize imgViewSize,titleSize,btnSize;
    imgViewSize = self.imageView.bounds.size;
    titleSize = self.titleLabel.bounds.size;
    btnSize = self.bounds.size;
    //设置按钮内边距
    CGFloat widthSpace = 10*RatioWidth;
//    CGFloat heightSpace = 5*RatioHeight;
    
    UIEdgeInsets imageViewEdge,titleEdge;
    imageViewEdge = UIEdgeInsetsMake(widthSpace,0.0, btnSize.height -imgViewSize.height, - titleSize.width);
    [self setImageEdgeInsets:imageViewEdge];
    
    titleEdge = UIEdgeInsetsMake(imgViewSize.height, - imgViewSize.width, 0.0, 0.0);
    [self setTitleEdgeInsets:titleEdge];

5.

    CGFloat imageW = self.imageView.frame.size.width;
    CGFloat imageH = self.imageView.frame.size.height;
    CGFloat titleW = self.titleLabel.frame.size.width;
    CGFloat titleH = self.titleLabel.frame.size.height;
    [self setImageEdgeInsets:UIEdgeInsetsMake(-titleH, 0.f, 0.f,-titleW)];
    [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -imageW, -imageH - 10, 0.f)];
 注意:titleLabel 的frame 是在 Imageview 加载出来之后才出现的

参考文章:【IOS实战】UIButton 设置图片文字垂直居中排列

 

转载于:https://www.cnblogs.com/guchunli/p/6531957.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值