码迷,mamicode.com
首页 > 其他好文 > 详细

UIButton 设置图片文字垂直居中排列

时间:2017-05-08 12:21:54      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:span   gray   controls   调用   pac   bsp   elf   排列   img   

#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 设置图片文字垂直居中排列

标签:span   gray   controls   调用   pac   bsp   elf   排列   img   

原文地址:http://www.cnblogs.com/-yun/p/6824105.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!