iOS-UIButton-设置button标题和图片位置

时间:2022-04-16 07:58:43

一.效果图

1.Button被点击之前

iOS-UIButton-设置button标题和图片位置

2.Button被点击之后

iOS-UIButton-设置button标题和图片位置

二.代码

 - (void)createBtn3
{
UIImage * buttonImage = [UIImage imageNamed:@"1.jpg"];
UIImage * selectedImage = [UIImage imageNamed:@"2.jpg"];
CGFloat buttonImageViewWidth = CGImageGetWidth(buttonImage.CGImage);
CGFloat buttonImageViewHeight = CGImageGetHeight(buttonImage.CGImage); NSString * buttonTitle = @"左图右标题";
UIFont * buttonTitleFont = [UIFont systemFontOfSize:17.0f];
NSDictionary * attributes = @{NSFontAttributeName : buttonTitleFont};
CGSize buttonTitleLabelSize = [buttonTitle sizeWithAttributes:attributes]; // button宽度,至少为imageView宽度与titleLabel宽度之和
CGFloat buttonWith = buttonImageViewWidth + buttonTitleLabelSize.width;
// button高度,至少为imageView高度与titleLabel高度之和
CGFloat buttonHeight = buttonImageViewHeight + buttonTitleLabelSize.height; UIButton * btn3 = [[UIButton alloc] init];
[btn3 setCenter:CGPointMake(, )];
[btn3 setBounds:CGRectMake(, , buttonWith, buttonHeight)];
[btn3 setBackgroundColor:[UIColor redColor]]; [btn3.titleLabel setFont:buttonTitleFont];
[btn3 setImage:buttonImage forState:UIControlStateNormal]; [btn3 setImage:selectedImage forState:UIControlStateSelected]; [btn3 setTitle:buttonTitle forState:UIControlStateNormal]; [btn3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn3.titleLabel setBackgroundColor:[UIColor whiteColor]]; [btn3 addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn3]; }
- (void)clicked:(UIButton *)btn3
{
NSLog(@"btn1被点击了");
NSString * buttonSelectedTitle = @"上图下标题";
[btn3 setTitle:buttonSelectedTitle forState:UIControlStateNormal]; CGPoint buttonBoundsCenter = CGPointMake(CGRectGetMidX(btn3.bounds), CGRectGetMidY(btn3.bounds)); // 找出imageView最终的center
CGPoint endImageViewCenter = CGPointMake(buttonBoundsCenter.x, CGRectGetMidY(btn3.imageView.bounds)); // 找出titleLabel最终的center
CGPoint endTitleLabelCenter = CGPointMake(buttonBoundsCenter.x, CGRectGetHeight(btn3.bounds)-CGRectGetMidY(btn3.titleLabel.bounds)); // 取得imageView最初的center
CGPoint startImageViewCenter = btn3.imageView.center; // 取得titleLabel最初的center
CGPoint startTitleLabelCenter = btn3.titleLabel.center; // 设置imageEdgeInsets
CGFloat imageEdgeInsetsTop = endImageViewCenter.y - startImageViewCenter.y; CGFloat imageEdgeInsetsLeft = endImageViewCenter.x - startImageViewCenter.x; CGFloat imageEdgeInsetsBottom = -imageEdgeInsetsTop; CGFloat imageEdgeInsetsRight = -imageEdgeInsetsLeft; btn3.imageEdgeInsets = UIEdgeInsetsMake(imageEdgeInsetsTop, imageEdgeInsetsLeft, imageEdgeInsetsBottom, imageEdgeInsetsRight); // 设置titleEdgeInsets
CGFloat titleEdgeInsetsTop = endTitleLabelCenter.y-startTitleLabelCenter.y; CGFloat titleEdgeInsetsLeft = endTitleLabelCenter.x - startTitleLabelCenter.x; CGFloat titleEdgeInsetsBottom = -titleEdgeInsetsTop; CGFloat titleEdgeInsetsRight = -titleEdgeInsetsLeft; btn3.titleEdgeInsets = UIEdgeInsetsMake(titleEdgeInsetsTop, titleEdgeInsetsLeft, titleEdgeInsetsBottom, titleEdgeInsetsRight);
}