![]()
ViewController中使用实例
UIView*headerView
= [[UIViewalloc]initWithFrame:CGRectMake(0,0,CKScreenW,50)];
headerView.backgroundColor=HexColor(0xceeffe);
[self.viewaddSubview:headerView];
UIButton*btn
= [[UIButtonalloc]initWithFrame:CGRectMake(10,10,CKScreenW*2/3,30)];
btn.backgroundColor=
[UIColorwhiteColor];
[btnsetTitle:@"按检查批次查询"forState:0];
[btnTiaoZhengButtonWithOffsit:100TextImageSite:UIButtonTextLeft];//UIButton
+ Helper
btn.titleLabel.font=
[UIFontsystemFontOfSize:12];
[btnsetTitleColor:[UIColorgrayColor]forState:0];
[headerViewaddSubview:btn];
(1)UIButton+Helper.h
//
// UIButton+Helper.h
// zack
//
// Created by apple on 17/7/2.
#import<UIKit/UIKit.h>
#define MsgCodeTime 60
typedefNS_OPTIONS(NSUInteger,
UIButtonTextSite) {
UIButtonTextRight=0,//图片在左文字在右
UIButtonTextLeft ,//图片在右文字在左
UIButtonTextBottom,//图片在上文字在下
UIButtonTextTop,//图片在下文字在上
};
@interfaceUIButton(Helper)
/**
* 按钮上的图片跟文字位置
*
* @param offset间距
* @param site 位置
*/
-(void)TiaoZhengButtonWithOffsit:(CGFloat)offset
TextImageSite:(UIButtonTextSite)site;
/**
* 开始倒计时
*/
-(void)StartMsgCodeTimeDown;
@end
(2)UIButton+Helper.m
//
// UIButton+Helper.m
// zack
//
// Created by apple on 17/7/2.
#import"UIButton+Helper.h"
@implementationUIButton(Helper)
/**
* 按钮上的图片跟文字,居中对齐
*/
-(void)TiaoZhengButtonWithOffsit:(CGFloat)offset
TextImageSite:(UIButtonTextSite)site{
if(site==UIButtonTextLeft)
{
[selfTextImageLeftWithOffsit:offset];
}elseif
(site==UIButtonTextTop) {
[selfTextImageTopWithOffsit:offset];
}else if(site==UIButtonTextBottom)
{
[selfTextImageBottomWithOffsit:offset];
}else{
[selfTextImageRightWithOffsit:offset];
}
}
/**
* 文字在右边,图片在左边,居中对齐
*
* @param offset文字跟图片间的间距大小
*/
-(void)TextImageRightWithOffsit:(CGFloat)offset{
CGFloatoffsetBetweenImageAndText
= offset;
CGFloatinsetAmount
= offsetBetweenImageAndText /
2.0;
self.imageEdgeInsets=UIEdgeInsetsMake(0,-insetAmount,0,insetAmount);
self.titleEdgeInsets=UIEdgeInsetsMake(0,insetAmount,0,
-insetAmount);
self.contentEdgeInsets=UIEdgeInsetsMake(0,
insetAmount,
0, insetAmount);
}
/**
* 文字在左边,图片在右边,居中对齐
*
* @param offset文字跟图片间的间距大小
*/
-(void)TextImageLeftWithOffsit:(CGFloat)offset{
CGFloatimgW=self.imageView.frame.size.width;
CGFloattitleW=self.titleLabel.frame.size.width;
CGFloatoffsetBetweenImageAndText
= offset;
CGFloatinsetAmount
= offsetBetweenImageAndText /
2.0;
self.imageEdgeInsets=UIEdgeInsetsMake(0,titleW+insetAmount,0,-titleW-insetAmount);
self.titleEdgeInsets=UIEdgeInsetsMake(0,-imgW-insetAmount,0,
imgW+insetAmount);
self.contentEdgeInsets=UIEdgeInsetsMake(0,
insetAmount,
0, insetAmount);
}
/**
* 文字在下边,图片在上边,居中对齐
*
* @param offset文字跟图片间的间距大小
*/
-(void)TextImageBottomWithOffsit:(CGFloat)offset{
CGPointbuttonBoundsCenter
=
CGPointMake(CGRectGetMidX(self.bounds),CGRectGetMidY(self.bounds));
//找出imageView最终的center
CGPointendImageViewCenter
=
CGPointMake(buttonBoundsCenter.x,CGRectGetMidY(self.imageView.bounds));
//找出titleLabel最终的center
CGPointendTitleLabelCenter
=
CGPointMake(buttonBoundsCenter.x,CGRectGetHeight(self.bounds)-CGRectGetMidY(self.titleLabel.bounds));
//取得imageView最初的center
CGPointstartImageViewCenter
=
self.imageView.center;
//取得titleLabel最初的center
CGPointstartTitleLabelCenter
=
self.titleLabel.center;
//设置imageEdgeInsets
CGFloatimgH=self.imageView.frame.size.height/2;
CGFloattitleH=self.titleLabel.frame.size.height/2;
CGFloatoffsetBetweenImageAndText
= offset;
CGFloatinsetAmount
= offsetBetweenImageAndText /
2.0;
CGFloatimageEdgeInsetsTop
= -titleH-insetAmount;
CGFloatimageEdgeInsetsLeft
= endImageViewCenter.x- startImageViewCenter.x;
CGFloatimageEdgeInsetsBottom
= -imageEdgeInsetsTop;
CGFloatimageEdgeInsetsRight
= -imageEdgeInsetsLeft;
self.imageEdgeInsets=UIEdgeInsetsMake(imageEdgeInsetsTop,
imageEdgeInsetsLeft, imageEdgeInsetsBottom, imageEdgeInsetsRight);
//设置titleEdgeInsets
CGFloattitleEdgeInsetsTop
= imgH+insetAmount;
CGFloattitleEdgeInsetsLeft
= endTitleLabelCenter.x- startTitleLabelCenter.x;
CGFloattitleEdgeInsetsBottom
= -titleEdgeInsetsTop;
CGFloattitleEdgeInsetsRight
= -titleEdgeInsetsLeft;
self.titleEdgeInsets=UIEdgeInsetsMake(titleEdgeInsetsTop,
titleEdgeInsetsLeft, titleEdgeInsetsBottom, titleEdgeInsetsRight);
}
/**
* 文字在上边,图片在下边,居中对齐
*
* @param offset文字跟图片间的间距大小
*/
-(void)TextImageTopWithOffsit:(CGFloat)offset{
CGPointbuttonBoundsCenter
=
CGPointMake(CGRectGetMidX(self.bounds),CGRectGetMidY(self.bounds));
//找出imageView最终的center
CGPointendImageViewCenter
=
CGPointMake(buttonBoundsCenter.x,CGRectGetMidY(self.imageView.bounds));
//找出titleLabel最终的center
CGPointendTitleLabelCenter
=
CGPointMake(buttonBoundsCenter.x,CGRectGetHeight(self.bounds)-CGRectGetMidY(self.titleLabel.bounds));
//取得imageView最初的center
CGPointstartImageViewCenter
=
self.imageView.center;
//取得titleLabel最初的center
CGPointstartTitleLabelCenter
=
self.titleLabel.center;
//设置imageEdgeInsets
CGFloatimgH=self.imageView.frame.size.height/2;
CGFloattitleH=self.titleLabel.frame.size.height/2;
CGFloatoffsetBetweenImageAndText
= offset;
CGFloatinsetAmount
= offsetBetweenImageAndText /
2.0;
CGFloatimageEdgeInsetsTop
= titleH+insetAmount;
CGFloatimageEdgeInsetsLeft
= endImageViewCenter.x- startImageViewCenter.x;
CGFloatimageEdgeInsetsBottom
= -imageEdgeInsetsTop;
CGFloatimageEdgeInsetsRight
= -imageEdgeInsetsLeft;
self.imageEdgeInsets=UIEdgeInsetsMake(imageEdgeInsetsTop,
imageEdgeInsetsLeft, imageEdgeInsetsBottom, imageEdgeInsetsRight);
//设置titleEdgeInsets
CGFloattitleEdgeInsetsTop
= -imgH-insetAmount;
CGFloattitleEdgeInsetsLeft
= endTitleLabelCenter.x- startTitleLabelCenter.x;
CGFloattitleEdgeInsetsBottom
= -titleEdgeInsetsTop;
CGFloattitleEdgeInsetsRight
= -titleEdgeInsetsLeft;
self.titleEdgeInsets=UIEdgeInsetsMake(titleEdgeInsetsTop,
titleEdgeInsetsLeft, titleEdgeInsetsBottom, titleEdgeInsetsRight);
}
#pragma mark -获取验证码倒计时
NSTimer*_timer;
NSIntegertimeout1;
-(void)StartMsgCodeTimeDown
{
// NSTimer *_timer;
// NSInteger timeout;
timeout1=
MsgCodeTime;
[selfsetTitle:[NSStringstringWithFormat:@"%@秒后重发",@(timeout1)]forState:UIControlStateDisabled];
self.enabled=NO;
_timer=
[NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(timeji)userInfo:nilrepeats:YES];
[[NSRunLoopcurrentRunLoop]addTimer:_timerforMode:UITrackingRunLoopMode];
}
-(void)timeji
{
// NSTimer *_timer;
// NSInteger timeout;
timeout1--;
if(timeout1<=0)
{
[_timerinvalidate];
_timer=
nil;
self.enabled=YES;
}else{
[selfsetTitle:[NSStringstringWithFormat:@"%@秒后重发",@(timeout1)]forState:UIControlStateDisabled];
self.enabled=NO;
}
}
-(void)dealloc{
if(_timer)
{
[_timerinvalidate];
_timer=
nil;
}
}
@end