#import <UIKit/UIKit.h>
@interface UIButton (GetVerifyCode)
- (void)setTimeForTwinkle:(NSInteger)time ;
//twinkleTitle:(NSString *)title;
@end
#import "UIButton+GetVerifyCode.h"
/**
* 记录原始的title
*/
static NSString *titleForOriginal;
/**
* 闪烁时间
*/
static NSInteger timeForTwinkle;
@implementation UIButton (GetVerifyCode)
- (void)setTimeForTwinkle:(NSInteger)Time
//twinkleTitle:(NSString *)Title
{
self.enabled = NO;
if (self.titleLabel.text != nil) {
titleForOriginal = self.titleLabel.text;
}
//设置获取验证码定时器的时间
timeForTwinkle = Time;
[self setTitle:[NSString stringWithFormat:@"%zd秒",timeForTwinkle] forState:UIControlStateDisabled];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
}
//定时器的功劳
-(void)timerFired:(NSTimer *)timer
{
if (timeForTwinkle !=1) {
timeForTwinkle -=1;
[self setTitle:[NSString stringWithFormat:@"%ld秒",timeForTwinkle] forState:UIControlStateDisabled];
} else {
[timer invalidate];
self.enabled = YES;
[self setTitle:titleForOriginal forState:UIControlStateNormal];
}
}
@end
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 16px; line-height: normal; font-family: Menlo; color: rgb(201, 27, 19);"><pre name="code" class="objc">#import <Foundation/Foundation.h>
@interface NSString (Extension)
- (BOOL)isPhoneNumber;
- (BOOL)isEmailAdress;
/**
* 返回字符串所占用的尺寸
*
* @param font 字体
* @param maxSize 最大尺寸
*/
- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize;
@end
#import "NSString+Extension.h"
@implementation NSString (Extension)
- (BOOL)match:(NSString *)pattern
{
// 1.创建正则表达式
NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:nil];
// 2.测试字符串
NSArray *results = [regex matchesInString:self options:0 range:NSMakeRange(0, self.length)];
return results.count > 0;
}
- (BOOL)isPhoneNumber
{
NSString *pattern = @"^1[3578]\\d{9}$";
return [self match:pattern];
}
- (BOOL)isEmailAdress
{
NSString *pattern = @"^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$";
return [self match:pattern];
}