iOS-精确控制UIButton 的image view与titlelabel的相对位置


最新解决办法 超简单 请看博文->iOS-随心所欲控制button内image view和title view的位置


一些开发者知道通过imageEdgeInsets及titleEdgeInsets可以调整imageView及titleLabel的位置,但不知道如何正确计算,基本上都是通过多次调整相应的值达到期望的目标(次数过多可能会失去信心),究其原因就是不能正确理解imageEdgeInsets及titleEdgeInsets.

下面步入主题.

如果button的bounds足够大(宽度至少是imageView宽度与titleLabel宽度之和,高度至少为imageView高度与titleLabel高度中的最大值),那么就可以同时显示imageView及titleLabel,如下图:

如果,想要得到imageView及titleLabel都居中显示,且要达到imageView在上,titleLabel在下的效果(通过设置imageView及titleLabel的frame是无法达到这样的效果的),如下图:

此时,button的bounds也要足够大(宽度至少是imageView宽度与titleLabel宽度之和,高度至少为imageView高度与titleLabel高度之和),并且,需要修改button的imageEdgeInsets及titleEdgeInsets.可参考下面的代码:

 

// 这里的图片,必须有Xcode.png及Xcode@2x.png,便于得到正确的以设备点为单位的宽度及高度

 UIImage *buttonImage = [UIImage imageNamed:@"Xcode"];

 CGFloat buttonImageViewWidth = CGImageGetWidth(buttonImage.CGImage);

 CGFloat buttonImageViewHeight = CGImageGetWidth(buttonImage.CGImage);

 //if ([UIScreen mainScreen].scale == 2.0f) {// iOS 4.0+

 //    buttonImageViewWidth *= 0.5f;

 //    buttonImageViewHeight *= 0.5f;

 //}

 NSString *buttonTitle = @"中华人民共和国";

 UIFont *buttonTitleFont = [UIFont systemFontOfSize:17.0f];

 CGSize buttonTitleLabelSize = [buttonTitle sizeWithFont:buttonTitleFont];

 // button宽度,至少为imageView宽度与titleLabel宽度之和

 CGFloat buttonWidth = buttonImageViewWidth + buttonTitleLabelSize.width;

 // button高度,至少为imageView高度与titleLabel高度之和

 CGFloat buttonHeight = buttonImageViewHeight + buttonTitleLabelSize.height;

 

 UIButton *b = [[UIButton alloc] init];

 [b setCenter:CGPointMake(160, 160)];

 [b setBounds:CGRectMake(0, 0, buttonWidth, buttonHeight)];

 [b.titleLabel setFont:buttonTitleFont];

 [b setBackgroundColor:[UIColor redColor]];

 

 [b setImage:buttonImage forState:UIControlStateNormal];

 [b.imageView setBackgroundColor:[UIColor greenColor]];

 

 [b setTitle:buttonTitle forState:UIControlStateNormal];

 [b setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

 [b setTitleColor:[UIColor scrollViewTexturedBackgroundColor] forState:UIControlStateHighlighted];

 [b.titleLabel setBackgroundColor:[UIColor whiteColor]];

 

 [self.view addSubview:b];

 

 CGPoint buttonBoundsCenter = CGPointMake(CGRectGetMidX(b.bounds), CGRectGetMidY(b.bounds));

 // 找出imageView最终的center

 CGPoint endImageViewCenter = CGPointMake(buttonBoundsCenter.x, CGRectGetMidY(b.imageView.bounds));

 // 找出titleLabel最终的center

 CGPoint endTitleLabelCenter = CGPointMake(buttonBoundsCenter.x, CGRectGetHeight(b.bounds)-CGRectGetMidY(b.titleLabel.bounds));

 // 取得imageView最初的center

 CGPoint startImageViewCenter = b.imageView.center;

 // 取得titleLabel最初的center

 CGPoint startTitleLabelCenter = b.titleLabel.center;

 // 设置imageEdgeInsets

 CGFloat imageEdgeInsetsTop = endImageViewCenter.y - startImageViewCenter.y;

 CGFloat imageEdgeInsetsLeft = endImageViewCenter.x - startImageViewCenter.x;

 CGFloat imageEdgeInsetsBottom = -imageEdgeInsetsTop;

 CGFloat imageEdgeInsetsRight = -imageEdgeInsetsLeft;

 b.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;

 b.titleEdgeInsets = UIEdgeInsetsMake(titleEdgeInsetsTop, titleEdgeInsetsLeft, titleEdgeInsetsBottom, titleEdgeInsetsRight);

 [b release];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值