项目中我们经常会有这样的需求,button中图片左标题右,图片上标图下,图片下标题上,图片右标题左;感觉挺简单的是吧!可是在自定义的时候可能总和需求有些差距。
(相关点:
titleEdgeInsets是titleLabel相对于其上下左右的inset,跟tableView的contentInset是类似的;
如果只有title,那titleLabel的 上下左右 都是 相对于Button 的;
如果只有image,那imageView的 上下左右 都是 相对于Button 的;
如果同时有image和label,那image的 上下左 是 相对于Button 的,右 是 相对于label 的;
label的 上下右 是 相对于Button的, 左 是 相对于label 的。)
方法确实也是那样:写给button的category然后重写方法:
//设置标题的位置
- (CGRect)titleRectForContentRect:(CGRect)contentRect{}
- (CGRect)imageRectForContentRect:(CGRect)contentRect{}
没错,我也是这么搞得,可是不能满足需求,所以在简书找了一下方法;(http://www.jianshu.com/p/3052a3b14a4e)mokong写的方法真的不错,亲测;解决了我一个需求问题;
在原来的方法中加了两个参数,满足了我的需求:
.m文件中实现方法:
define IOS_VERSION_8_OR_Above (([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)? (YES):(NO))
const CGFloat kRcsCommonButtonAverageValue = 2.0f;
//设置item标题和icon
-
(void)setTitle:(NSString *)title
image:(UIImage *)image
withEdgeInsetsStyle:(XXYButtonEdgeInsetsStyle)style
imageTitleSpace:(CGFloat)space{
//1. 基本设置
[self setTitle:title forState:UIControlStateNormal];
[self setImage:image forState:UIControlStateNormal];
// self.titleLabel.numberOfLines = 2;
// self.titleLabel.font = [UIFont systemFontOfSize:14.f];
[self setTitleColor:kRCSColor_BlackColor forState:UIControlStateNormal];
//2. 得到imageview和titlelabel的宽、高
CGFloat imageWidth = self.imageView.frame.size.width;
CGFloat imageHeight = self.imageView.frame.size.height;CGFloat labelWidth = 0.0f;
CGFloat labelHeight = 0.0f;if (IOS_VERSION_8_OR_Above) {
//iOS8中titleLabel的size为0,用下面的这种设置
labelWidth = self.titleLabel.intrinsicContentSize.width;
labelHeight = self.titleLabel.intrinsicContentSize.height;
}else{
labelWidth = self.titleLabel.frame.size.width;
labelHeight = self.titleLabel.frame.size.height;
}
//3. 声明全局的imageEdgeInsets和labelEdgeInsets
UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
//4. 根据style和space得到imageEdgeInsets和labelEdgeInsets的值
switch (style) {
case XXYButtonEdgeInsetsStyleTop:
{
imageEdgeInsets = UIEdgeInsetsMake(-labelHeight - space / kRcsCommonButtonAverageValue, 0, 0, -labelWidth);
labelEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, -imageHeight - space / kRcsCommonButtonAverageValue, 0);
}
break;
case XXYButtonEdgeInsetsStyleLeft:
{
imageEdgeInsets = UIEdgeInsetsMake(0, -space / kRcsCommonButtonAverageValue, 0, space / kRcsCommonButtonAverageValue);
labelEdgeInsets = UIEdgeInsetsMake(0, space / kRcsCommonButtonAverageValue, 0, -space / kRcsCommonButtonAverageValue);
}break; case XXYButtonEdgeInsetsStyleBottom: { imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight - space / kRcsCommonButtonAverageValue, -labelWidth); labelEdgeInsets = UIEdgeInsetsMake(-imageHeight - space / kRcsCommonButtonAverageValue, -imageWidth, 0, 0); } break; case XXYButtonEdgeInsetsStyleRight: { imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth + space / kRcsCommonButtonAverageValue, 0, -labelWidth - space / kRcsCommonButtonAverageValue); labelEdgeInsets = UIEdgeInsetsMake(0, -imageWidth - space / kRcsCommonButtonAverageValue, 0, imageWidth + space / kRcsCommonButtonAverageValue); } break; default: break;
}
//5. 赋值
self.titleEdgeInsets = labelEdgeInsets;
self.imageEdgeInsets = imageEdgeInsets;
}
这篇文章确实也挺好的 http://www.cnblogs.com/Phelthas/p/4452235.html