iOS 扩展方法
来源:互联网 发布:ubuntu 卸载adb命令 编辑:程序博客网 时间:2024/05/22 15:59
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; } /** * 手机号码: * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[0, 1, 6, 7, 8], 18[0-9] * 移动号段: 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188 * 联通号段: 130,131,132,145,155,156,170,171,175,176,185,186 * 电信号段: 133,149,153,170,173,177,180,181,189 */ NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|7[0135678]|8[0-9])\\d{8}$"; /** * 中国移动:China Mobile * 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188 */ NSString *CM = @"^1(3[4-9]|4[7]|5[0-27-9]|7[08]|8[2-478])\\d{8}$"; /** * 中国联通:China Unicom * 130,131,132,145,155,156,170,171,175,176,185,186 */ NSString *CU = @"^1(3[0-2]|4[5]|5[56]|7[0156]|8[56])\\d{8}$"; /** * 中国电信:China Telecom * 133,149,153,170,173,177,180,181,189 */ 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{ // 创建一个bitmap的context // 并把它设置成为当前正在使用的context UIGraphicsBeginImageContext(size); // 绘制改变大小的图片 [self drawInRect:CGRectMake(0, 0, size.width, size.height)]; // 从当前context中创建一个改变大小后的图片 UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); // 使当前的context出堆栈 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
0 0
- iOS 扩展方法
- iOS 图片压缩UIImage方法扩展
- iOS 图片压缩UIImage方法扩展
- iOS 图片压缩UIImage方法扩展
- iOS 图片压缩UIImage方法扩展
- iOS 图片压缩UIImage方法扩展
- iOS类别扩展无法识别添加方法
- iOS 图片压缩UIImage方法扩展
- iOS类别扩展无法识别添加方法
- ios扩展
- iOS 扩展类添加属性,需要实现的set方法
- iOS 中对类的扩展的几种方法
- iOS gif 图片播放实现方法 (image 扩展方案)
- iOS 与日历、日期相关的一些扩展方法
- 扩展方法
- 扩展方法
- 扩展方法
- 扩展方法
- JPEG算法解密(五)
- The user specified as a definer ('root'@'%') does not exist
- 易企秀上传图片不成功!提示非法图片,问题解决!
- AT24Cxx芯片读写--IIC总线
- Http状态码大全(404、500、505等)
- iOS 扩展方法
- WinForm-ListBox控件美化
- 面向切面编程(AOP)的理解
- JAVA密码校验
- ubuntu下面编译libuv
- 无法附加到进程。Visual Studio 2015 远程调试器(MSVSMON.EXE)似乎没有在远程计算机上运行。
- 怎么在CorelDRAW中将矩形变圆角
- 最简单的Ogre系列之三——添加鼠标(不使用任何GUI)
- java读取邮件