UIView的扩展:
#import <UIKit/UIKit.h>
@interface UIView (Ext)
/**
width 宽度
*/
@property (nonatomic, assign) CGFloat width;
/**
height 高度
*/
@property (nonatomic, assign) CGFloat height;
/**
top 顶部y坐标
*/
@property (nonatomic, assign) CGFloat top;
/**
left 左侧x坐标
*/
@property (nonatomic, assign) CGFloat left;
/**
bottom 底部y坐标
*/
@property (nonatomic, assign) CGFloat bottom;
/**
right 右侧x坐标
*/
@property (nonatomic, assign) CGFloat right;
/**
origin
*/
@property (nonatomic, assign) CGPoint origin;
/**
size 尺寸
*/
@property (nonatomic, assign) CGSize size;
UIView+Ext.m
#import "UIView+Ext.h"
@implementation UIView (Ext)
- (void)setSize:(CGSize)size{
CGRect newFrame = self.frame;
newFrame.size = size;
self.frame = newFrame;
}
- (CGSize)size{
return self.frame.size;
}
- (void)setOrigin:(CGPoint)origin{
CGRect newFrame = self.frame;
newFrame.origin = origin;
self.frame = newFrame;
}
- (CGPoint)origin{
return self.frame.origin;
}
- (void)setLeft:(CGFloat)left {
CGRect newFrame = self.frame;
newFrame.origin.x = left;
self.frame = newFrame;
}
- (CGFloat)left {
return self.origin.x;
}
- (void)setRight:(CGFloat)right {
CGRect newFrame = self.frame;
newFrame.origin.x = right - self.width;
self.frame = newFrame;
}
- (CGFloat)right {
return self.frame.origin.x + self.width;
}
- (void)setTop:(CGFloat)top {
CGRect newFrame = self.frame;
newFrame.origin.y = top;
self.frame = newFrame;
}
- (CGFloat)top {
return self.frame.origin.y;
}
- (void)setBottom:(CGFloat)bottom {
CGRect newFrame = self.frame;
newFrame.origin.y = bottom - self.height;
self.frame = newFrame;
}
- (CGFloat)bottom {
return self.frame.origin.y + self.height;
}
- (void)setWidth:(CGFloat)width {
CGRect newFrame = self.frame;
newFrame.size.width = width;
self.frame = newFrame;
}
- (CGFloat)width {
return self.frame.size.width;
}
- (void)setHeight:(CGFloat)height {
CGRect newFrame = self.frame;
newFrame.size.height = height;
self.frame = newFrame;
}
- (CGFloat)height {
return self.frame.size.height;
}
字符串NSString的扩展:
#import <UIKit/UIKit.h>
@interface NSString (Ext)
/**
判断字符串是否为空
@return <#return value description#>
*/
- (BOOL)isNull;
/**
获取字符串的宽度
@param size 尺寸
@param fontSize 字体大小
@return <#return value description#>
*/
- (CGFloat)widthWithSize:(CGSize)size fontSize:(CGFloat)fontSize;
/**
获取字符串高度
@param size 尺寸
@param fontSize 字体大小
@return <#return value description#>
*/
- (CGFloat)heightWithSize:(CGSize)size fontSize:(CGFloat)fontSize;
/**
判断是否是有效的电话号码
@return <#return value description#>
*/
- (BOOL)isValidMobileNumber;
/**
将时间戳转换为时间
@return <#return value description#>
*/
- (NSString *)stampToDatestring;
/**
判断是否是有效的邮箱
@return <#return value description#>
*/
- (BOOL)isValidEmail;
/**
是否是字母和数字的组合
*/
-(BOOL)checkIsHaveNumAndLetter;
/**
判断是否包含汉字
*/
-(BOOL)estimateStringContainChinese:(NSString*)text;
/**
判断是否包含英文字母
*/
-(BOOL)estimateStringContainEnglish:(NSString*)text;
#import "NSString+Ext.h"
@implementation NSString (Ext)
- (BOOL)isNull {
if (self == nil
|| [self isKindOfClass:[NSNull class]]
|| [self isEqualToString:@"<null>"]
|| [self isEqualToString:@"null"]
|| [self isEqualToString:@"(null)"]
|| [self isEqualToString:@""]) {
return YES;
}
return NO;
}
- (CGFloat)widthWithSize:(CGSize)size fontSize:(CGFloat)fontSize {
CGRect rect = [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil];
return ceilf(rect.size.width);
}
- (CGFloat)heightWithSize:(CGSize)size fontSize:(CGFloat)fontSize {
CGRect rect = [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil];
return ceilf(rect.size.height);
}
- (BOOL)isValidMobileNumber {
if (self.length != 11)
{
return NO;
}
NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|7[0135678]|8[0-9])\\d{8}$";
NSString *CM = @"^1(3[4-9]|4[7]|5[0-27-9]|7[08]|8[2-478])\\d{8}$";
NSString *CU = @"^1(3[0-2]|4[5]|5[56]|7[0156]|8[56])\\d{8}$";
NSString *CT = @"^1(3[3]|4[9]|53|7[037]|8[019])\\d{8}$";
NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
if (([regextestmobile evaluateWithObject:self] == YES)
|| ([regextestcm evaluateWithObject:self] == YES)
|| ([regextestct evaluateWithObject:self] == YES)
|| ([regextestcu evaluateWithObject:self] == YES))
{
return YES;
}
else
{
return NO;
}
}
- (NSString *)stampToDatestring {
NSDate *d = [[NSDate alloc] initWithTimeIntervalSince1970:[self longLongValue]/1000.0];
NSDateFormatter*dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString*currentDateStr = [dateFormatter stringFromDate:d];
return currentDateStr;
}
- (BOOL)isValidEmail {
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:self];
}
-(BOOL)checkIsHaveNumAndLetter
{
NSRegularExpression *tNumRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[0-9]" options:NSRegularExpressionAllowCommentsAndWhitespace error:nil];
NSUInteger tNumMatchCount = [tNumRegularExpression numberOfMatchesInString:self
options:NSMatchingReportProgress
range:NSMakeRange(0, self.length)];
NSRegularExpression *sLetterRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[a-z]" options:NSRegularExpressionAllowCommentsAndWhitespace error:nil];
NSUInteger sLetterMatchCount = [sLetterRegularExpression numberOfMatchesInString:self options:NSMatchingReportProgress range:NSMakeRange(0, self.length)];
NSRegularExpression *tLetterRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[A-Z]" options:NSRegularExpressionAllowCommentsAndWhitespace error:nil];
NSUInteger tLetterMatchCount = [tLetterRegularExpression numberOfMatchesInString:self options:NSMatchingReportProgress range:NSMakeRange(0, self.length)];
if ((tLetterMatchCount == 0 && sLetterMatchCount == 0) || tNumMatchCount == 0) {
return NO;
} else {
if(tNumMatchCount + tLetterMatchCount + sLetterMatchCount == self.length){
return YES;
}else{
return NO;
}
}
}
-(BOOL)estimateStringContainChinese:(NSString*)text{
if(text){
for (int i=0; i<text.length; i++) {
NSRange range = NSMakeRange(i,1);
NSString *subString = [text substringWithRange:range];
const char *cString = [subString UTF8String];
if (strlen(cString) == 3){
return YES;
}
}
}
return NO;
}
-(BOOL)estimateStringContainEnglish:(NSString*)text{
if(text){
for (int i=0; i<text.length; i++) {
NSRange range = NSMakeRange(i,1);
NSString *subString = [text substringWithRange:range];
const char *cString = [subString UTF8String];
if (strlen(cString) == 1){
return YES;
}
}
}
return NO;
}
UIAlertView的扩展:
#import <UIKit/UIKit.h>
@interface UIAlertView (Ext)
/**
自定义alertView
@param title 标题
@param messgae 内容
@param cancelTitle 取消按钮标题
@param confirmTitle 确定按钮标题
@param cancel 取消回调
@param confirm 确定回调
@return <#return value description#>
*/
+ (instancetype)alertWithTitle:(NSString *)title messgae:(NSString *)messgae cancelTitle:(NSString *)cancelTitle confirmTitle:(NSString *)confirmTitle cancel:(void(^)())cancel confirm:(void(^)())confirm;
#import "UIAlertView+Ext.h"
#import <objc/runtime.h>
typedef void(^Confirm)();
typedef void(^Cancel)();
static const void *UIAlertViewOriginalDelegateKey = &UIAlertViewOriginalDelegateKey;
static const void *UIAlertViewCancelBlockKey = &UIAlertViewCancelBlockKey;
static const void *UIAlertViewSubmitBlockKey = &UIAlertViewSubmitBlockKey;
@implementation UIAlertView (Ext)
+ (instancetype)alertWithTitle:(NSString *)title messgae:(NSString *)messgae cancelTitle:(NSString *)cancelTitle confirmTitle:(NSString *)confirmTitle cancel:(void (^)())cancel confirm:(void (^)())confirm{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:title message:messgae delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:confirmTitle, nil];
if (confirm) {
[alert setDidConfirmBlock:confirm];
}
if (cancel) {
[alert setCancelBlock:cancel];
}
return alert;
}
- (void)setDidConfirmBlock:(Confirm)didPresentBlock {
[self _checkAlertViewDelegate];
objc_setAssociatedObject(self, UIAlertViewSubmitBlockKey, didPresentBlock, OBJC_ASSOCIATION_COPY);
}
- (Confirm)didConfirmBlock {
return objc_getAssociatedObject(self, UIAlertViewSubmitBlockKey);
}
- (void)setCancelBlock:(Cancel)cancelBlock {
[self _checkAlertViewDelegate];
objc_setAssociatedObject(self, UIAlertViewCancelBlockKey, cancelBlock, OBJC_ASSOCIATION_COPY);
}
- (Cancel)cancelBlock {
return objc_getAssociatedObject(self, UIAlertViewCancelBlockKey);
}
- (void)_checkAlertViewDelegate {
if (self.delegate != (id<UIAlertViewDelegate>)self) {
objc_setAssociatedObject(self, UIAlertViewOriginalDelegateKey, self.delegate, OBJC_ASSOCIATION_ASSIGN);
self.delegate = (id<UIAlertViewDelegate>)self;
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
{
Cancel cancel = [alertView cancelBlock];
cancel();
}
break;
case 1:
{
Confirm confirm = [alertView didConfirmBlock];
confirm();
}
break;
default:
break;
}
}
@end
UIImage的扩展:
#import <UIKit/UIKit.h>
@interface UIImage (Ext)
/**
压缩图片至指定大小
@param size <#size description#>
@return <#return value description#>
*/
- (UIImage*)scaleToSize:(CGSize)size;
@end
#import "UIImage+Ext.h"
@implementation UIImage (Ext)
- (UIImage*)scaleToSize:(CGSize)size
{
UIGraphicsBeginImageContext(size);
[self drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
@end
UIImageView的扩展:
#import <UIKit/UIKit.h>
@interface UIImageView (Ext)
/**
旋转图片
@param angle 角度
@param duation 时间
*/
- (void)rotationByAngle:(CGFloat)angle Duation:(CGFloat)duation;
/**
旋转图片
@param angle 角度
@param duation 时间
@param delay 延迟
*/
- (void)rotationByAngle:(CGFloat)angle Duation:(CGFloat)duation Delay:(CGFloat)delay;
@end
#import "UIImageView+Ext.h"
@implementation UIImageView (Ext)
- (void)rotationByAngle:(CGFloat)angle Duation:(CGFloat)duation
{
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: angle * (M_PI / 180.0f) ];
rotationAnimation.duration = duation;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 0;
[self.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
- (void)rotationByAngle:(CGFloat)angle Duation:(CGFloat)duation Delay:(CGFloat)delay
{
CGAffineTransform endAngle = CGAffineTransformMakeRotation(angle * (M_PI / 180.0f));
[UIView animateWithDuration:duation delay:delay options:UIViewAnimationOptionCurveLinear animations:^{
self.transform = endAngle;
} completion:^(BOOL finished) {
}];
}
@end