UIButton 设置图片文字垂直居中排列
Posted Sinner_Yun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UIButton 设置图片文字垂直居中排列相关的知识,希望对你有一定的参考价值。
#pragma mark 按钮图片文字垂直居中排列 -(void)setButtonContentCenter:(UIButton *)button { CGSize imgViewSize,titleSize,btnSize; UIEdgeInsets imageViewEdge,titleEdge; CGFloat heightSpace = 10.0f; //设置按钮内边距 imgViewSize = button.imageView.bounds.size; titleSize = button.titleLabel.bounds.size; btnSize = button.bounds.size; imageViewEdge = UIEdgeInsetsMake(heightSpace,0.0, btnSize.height -imgViewSize.height - heightSpace, - titleSize.width); [button setImageEdgeInsets:imageViewEdge]; titleEdge = UIEdgeInsetsMake(imgViewSize.height +heightSpace, - imgViewSize.width, 0.0, 0.0); [button setTitleEdgeInsets:titleEdge]; }
调用示例:
[Btn setImage:[UIImage imageNamed:@"test.png"] forState:UIControlStateNormal]; [Btn setTitle:@"测试" forState:UIControlStateNormal]; [Btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal]; [self setButtonContentCenter:Btn];
以上是关于UIButton 设置图片文字垂直居中排列的主要内容,如果未能解决你的问题,请参考以下文章