首页 > 代码库 > UIButton 设置图片文字垂直居中排列
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 设置图片文字垂直居中排列
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。