首先声明,我是在git上看到的这个封装方法.我感觉挺好用的.就给大家推荐一下(具体的位置我找不到了,不好意思了).好了,还是直接上代码吧,让我们也来学习一下大神的方法.
这个代码是对UIButton的扩展(category)
首先是.h的代码
#import
typedef enum : NSUInteger {
ButtonImgViewStyleTop,
ButtonImgViewStyleLeft,
ButtonImgViewStyleBottom,
ButtonImgViewStyleRight,
} ButtonImgViewStyle;
@interface UIButton (HXButton)
/**
设置 按钮 图片所在的位置
@param style 图片位置类型(上、左、下、右)
@param size 图片的大小
@param space 图片跟文字间的间距
*/
- (void)setImgViewStyle:(ButtonImgViewStyle)style imageSize:(CGSize)size space:(CGFloat)space;
@end
.m的代码
#import "UIButton+HXButton.h"
#import
static const char Btn_ImgViewStyle_Key;
static const char Btn_ImgSize_key;
static const char Btn_ImgSpace_key;
@implementation UIButton (HXButton)
- (void)setImgViewStyle:(ButtonImgViewStyle)style imageSize:(CGSize)size space:(CGFloat)space
{
objc_setAssociatedObject(self, &Btn_ImgViewStyle_Key, @(style), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self, &Btn_ImgSpace_key, @(space), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self, &Btn_ImgSize_key, NSStringFromCGSize(size), OBJC_ASSOCIATION_COPY_NONATOMIC);
}
+ (void)load
{
Method m1 = class_getInstanceMethod([self class], @selector(layoutSubviews));
Method m2 = class_getInstanceMethod([self class], @selector(layoutSubviews1));
method_exchangeImplementations(m1, m2);
}
- (void)layoutSubviews1
{
[self layoutSubviews1];
NSNumber *typeNum = objc_getAssociatedObject(self, &Btn_ImgViewStyle_Key);
if (typeNum) {
NSNumber *spaceNum = objc_getAssociatedObject(self, &Btn_ImgSpace_key);
NSString *imgSizeStr = objc_getAssociatedObject(self, &Btn_ImgSize_key);
CGSize imgSize = self.currentImage ? CGSizeFromString(imgSizeStr) : CGSizeZero;
CGSize labelSize = self.currentTitle.length ? [self.currentTitle sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}] : CGSizeZero;
CGFloat space = (labelSize.width && self.currentImage) ? spaceNum.floatValue : 0;
CGFloat width = self.frame.size.width;
CGFloat height = self.frame.size.height;
CGFloat imgX, imgY, labelX, labelY;
switch (typeNum.integerValue) {
case ButtonImgViewStyleLeft:
{
imgX = (width - imgSize.width - labelSize.width - space)/2.0;
imgY = (height - imgSize.height)/2.0;
labelX = imgX + imgSize.width + space;
labelY = (height - labelSize.height)/2.0;
self.imageView.contentMode = UIViewContentModeRight;
break;
}
case ButtonImgViewStyleTop:
{
imgX = (width - imgSize.width)/2.0;
imgY = (height - imgSize.height - space - labelSize.height)/2.0;
labelX = (width - labelSize.width)/2;
labelY = imgY + imgSize.height + space;
self.imageView.contentMode = UIViewContentModeBottom;
break;
}
case ButtonImgViewStyleRight:
{
labelX = (width - imgSize.width - labelSize.width - space)/2.0;
labelY = (height - labelSize.height)/2.0;
imgX = labelX + labelSize.width + space;
imgY = (height - imgSize.height)/2.0;
self.imageView.contentMode = UIViewContentModeLeft;
break;
}
case ButtonImgViewStyleBottom:
{
labelX = (width - labelSize.width)/2.0;
labelY = (height - labelSize.height - imgSize.height -space)/2.0;
imgX = (width - imgSize.width)/2.0;
imgY = labelY + labelSize.height + space;
self.imageView.contentMode = UIViewContentModeTop;
break;
}
default:
break;
}
self.imageView.frame = CGRectMake(imgX, imgY, imgSize.width, imgSize.height);
self.titleLabel.frame = CGRectMake(labelX, labelY, labelSize.width, labelSize.height);
}
}
@end
我感觉挺好用的.下面是代码示例
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 30, 50)];
[button setTitle:@"小熊" forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"小熊.png"] forState:UIControlStateNormal];
[button setImgViewStyle:ButtonImgViewStyleTop imageSize:CGSizeMake(30, 30) space:5];
[self.view addSubview:button];
效果图
分层图
还有一种就是直接修改button.这样做的方法就是创建button的时候,代码会更少
代码如下:
.m
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initialize];
}
return self;
}
- (void)initialize {
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.titleLabel.font = [UIFont systemFontOfSize:14];
}
-(CGRect)titleRectForContentRect:(CGRect)contentRect{
CGFloat titleX = -contentRect.size.width * 0.2;
CGFloat titleY = contentRect.size.height * 0.7;
CGFloat titleW = contentRect.size.width * 1.4;
CGFloat titleH = contentRect.size.height * 0.2;
return CGRectMake(titleX, titleY, titleW, titleH);
}
-(CGRect)imageRectForContentRect:(CGRect)contentRect{
CGFloat imageW = CGRectGetWidth(contentRect);
CGFloat imageH = contentRect.size.height * 0.7;
return CGRectMake(0, 0, imageW, imageH);
}
示例如下
// 联系卖家
YLButton *serviceButton = [[YLButton alloc] initWithFrame:CGRectMake(16, 0, 25, self.frame.size.height)];
[serviceButton setImage:[UIImage imageNamed:@"kefu_"] forState:UIControlStateNormal];
[serviceButton setTitle:@"联系卖家" forState:UIControlStateNormal];
[serviceButton setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
serviceButton.titleLabel.font = [UIFont systemFontOfSize:FontSize];
[serviceButton addTarget:self action:@selector(service:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:serviceButton];
如下图所示