iOS开发工具类 DFBox全面提升开发效率

Posted iOS的美丽岁月

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS开发工具类 DFBox全面提升开发效率相关的知识,希望对你有一定的参考价值。

引入

在App的开发中,效率占有很重要的地位。作为开发者如何能有在有限的时间内开发出一个优秀的App呢?答案很明显,那就是要求开发者能运用一些现有其他开发者留下的一些功能模块和小工具代码。今天本博客就介绍一个开发者常用的工具类 DFBox。 该类涵有26中方法。该工具类还在一直添加,请尽情关注!!!



一:DFBox.h 文件

//
//  DFBox.h
//  DevelopmentFunctionBox
//
//  Created by MAC on 2017/9/25.
//  Copyright © 2017年 NetworkCode小贱. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface DFBox : NSObject

@property(nonatomic,strong) NSString * dddd;
@property(nonatomic,strong) NSString * f;
@property(nonatomic,strong) NSString * ddwdd;
@property(nonatomic,strong) NSString * ddfdd;

/**
 创建一个单利对象
 */
+(instancetype)developFunctionBox;

/**
 获取作者信息
 */
-(void)getAuthorInfo;

/**
 禁止手机的休眠
 
 @isDormancy  YES:不休眠;NO:处于正常休眠状态
 */
-(void)setPhoneToSleep:(BOOL)isDormancy;

/**
 除去数组中重复的对象
 
 @array 要去除重复的对象
 */
-(NSArray*)removeDuplicateObjectsOfArray:(NSArray*)array;

/**
 给一个View截图
 
 @view 要截图的对象
 */
-(UIImage*)screenshotToView:(UIView*)view ;

/**
 颜色转图像
 
 @color 要转换的颜色
 */
-(UIImage*)colorConvertedToImage:(UIColor*)color ;

/**
 弧度转角度或者角度转弧度
 
 @isRadianToAngles YES为弧度转角度;NO为角度转弧度。
 */
-(CGFloat)radianToAngles:(CGFloat)value with:(BOOL)isRadianToAngles;

/**
 获取随机颜色
 */
-(UIColor*)getRandomColor;

/**
 改变UITextField的placeholder的字体颜色和大小
 
 @color 颜色
 @fontSize 字体大小
 @textField 要改变的对象
 */
-(void)changeTextFieldPlacehoderColor:(UIColor*)color withPlacehoderFont:(CGFloat)fontSize with:(UITextField*)textField;

/**
 收起键盘
 */
-(void)packUpKeyboard;


/**
 获取当前是什么类型的手机
 */
-(NSString*)getPhoneType;

/**
 获取图像的扩展名
 
 @imageData 图像数据
 */
-(NSString*)getImageExtensionName:(NSData*)imageData;

/**
 获取一个类的所有属性
 
 @className 类的名字
 */
-(void)getPropertiesOfClass:(const char*)className;


/**
 绘制圆角图像
 
 @image 要变圆角的图像
 */
-(UIImage*)circleImage:(UIImage*)image;


/**
 图像的拉伸
 
 @image 拉伸的对象
 @edgeInset 拉伸的范围
 */
-(UIImage*)tensileImage:(UIImage*)image withEdgeInset:(UIEdgeInsets)edgeInset;


/**
 JSON字符串转字典
 
 @jsonStr JSON字符串对象
 */
-(NSDictionary*)jsonToDictionary:(NSString*)jsonStr;

/**
 判断身份证号是否合法
 
 @idNumberStr
 */
-(BOOL)judgmentIdNumberOflegitimacy:(NSString*)idNumberStr;

/**
 判断图像是否有透明度通道
 
 @image 判断的对象
 */
-(BOOL)judgmentAlphaOfImage:(UIImage*)image;

/**
 根据屏幕大小获取相应的图像
 
 @imageName 要加载的图像的名字
 */
-(UIImage*)imageWith:(NSString*)imageName;


/**
 移除字符串中的空格
 
 @doString  要处理的字符串
 */
-(NSString*)removeSpaceAndNewline:(NSString*)doString;


/**
 判断一个字符串是否为空
 
 @doString 要判断的字符串
 */
-(BOOL)isEqualToNil:(NSString*)doString;


/**
 删除某个View的所有子视图
 
 @ doView  要处理的View
 */
-(void)removeSonsformView:(UIView*)doView;

/**
 让View 的背景色渐变
 
 @doView  要渐变的View
 */
-(void)gradientViewOfBackGroundColor:(UIView*)doView withColors:(NSArray*)colors;


/**
 给某个View的一个角添加圆角
 
 @doView 要添加的圆角的View
 */
-(void)addRadiiOfView:(UIView*)doView byRoundingCorners:(UIRectCorner)corners withRadii:(CGFloat)radii;

/**
 获取View在Window上的位置
 
 @doView 要获取位置View
 */
-(CGPoint)getPointOfViewInWindow:(UIView*)doView;

/**
 在View上添加虚线
 
 @doView 要添加的虚线的View
 @lineColor 线的颜色
 @lineInterval 虚线间隔数组
 @lineColor 虚线的颜色
 @lineWidth 虚线的宽度
 @point1、point2 虚线的起始和结束位置
 */
-(void)addDottedLineToView:(UIView*)doView byLineInterval:(NSArray*)lineInterval withlineColor:(UIColor*)lineColor withLineWidth:(CGFloat)lineWidth loctionStart:(CGPoint)point1 loctionEnd:(CGPoint)point2;


/**
 获取一个View所在的控制器
 
 @doView 要获取控制器的对象
 */
-(UIViewController*)belongViewController:(UIView*)doView;
@end


二:DFBox类的.m文件内容

//
//  DFBox.m
//  DevelopmentFunctionBox
//
//  Created by MAC on 2017/9/25.
//  Copyright © 2017年 NetworkCode小贱. All rights reserved.
//

#import "DFBox.h"
#import <sys/utsname.h>
#import <objc/runtime.h>
static DFBox * _dFBox = nil ;
@implementation DFBox

/**
 创建一个单利对象
 */
+(instancetype)developFunctionBox
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^
        _dFBox = [[self alloc]init] ;
    );
    return _dFBox ;


/**
 重写init
 */
-(id)init
    if (_dFBox) 
        return self;
    else
        _dFBox = [super init];
    
    return _dFBox ;


/**
 copy
 */
-(id)copy
    if (_dFBox) 
        return self;
    else
        _dFBox = [super copy];
    
    return _dFBox ;



/**
 获取作者信息
 */
-(void)getAuthorInfo
    NSDictionary * authorinfo = @@"AuthorName":@"周双建",@"phoneNumber":@"18801210281",@"QQ":@"1542100658";
    NSLog(@"%@",authorinfo);


#pragma mark 禁止手机的休眠
/**
 @isDormancy  YES:不休眠;NO:处于正常休眠状态
 */
-(void)setPhoneToSleep:(BOOL)isDormancy
    [UIApplication sharedApplication].idleTimerDisabled = isDormancy;



#pragma mark 除去数组中重复的对象
-(NSArray*)removeDuplicateObjectsOfArray:(NSArray*)array
    // 判断给定的对象否为空
    if (array==nil) return nil;
    // 不要忘记 ‘@’ 符号
    return [array valueForKeyPath:@"@distinctUnionOfObjects.self"];



#pragma mark 给一个View截图
/**
 @view 要截图的对象
 */
-(UIImage*)screenshotToView:(UIView*)view 
    if (view) 
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0);
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    
    return nil ;



#pragma mark 颜色转图像
/**
 @color 要转换的颜色
 */
-(UIImage*)colorConvertedToImage:(UIColor*)color 
    if(color)
        CGRect rect = CGRectMake(0, 0, 1.0, 1.0);
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext() ;
        CGContextSetFillColorWithColor(context, color.CGColor);
        CGContextFillRect(context, rect);
        UIImage * image = UIGraphicsGetImageFromCurrentImageContext() ;
        UIGraphicsEndImageContext() ;
        return image ;
    
    return nil;



#pragma mark  弧度转角度或者角度转弧度
/**
 @isRadianToAngles YES为弧度转角度;NO为角度转弧度。
 */
-(CGFloat)radianToAngles:(CGFloat)value with:(BOOL)isRadianToAngles
    if (isRadianToAngles) 
        return (value * 180.0)/M_PI;
    else
        return value * M_PI /180.0;
    



#pragma mark 获取随机颜色
-(UIColor*)getRandomColor
    NSInteger rValue = arc4random() % 255;
    NSInteger gValue = arc4random() % 255;
    NSInteger bValue = arc4random() % 255;
    return [UIColor colorWithRed:rValue /255.0 green:gValue /255.0 blue:bValue /255.0 alpha:1.0f];



#pragma mark  改变UITextField的placeholder的字体颜色和大小
/**
 @color 颜色
 @fontSize 字体大小
 @textField 要改变的对象
 */
-(void)changeTextFieldPlacehoderColor:(UIColor*)color withPlacehoderFont:(CGFloat)fontSize with:(UITextField*)textField
    if (textField==nil) return;
    if (color) 
        [textField setValue:color forKey:@"_placeholderLabel.textColor"];
    
    if (fontSize!=0) 
        [textField setValue:[UIFont systemFontOfSize:fontSize] forKey:@"_placeholderLabel.font"];
    



#pragma mark  收起键盘
-(void)packUpKeyboard
    [[UIApplication sharedApplication].keyWindow endEditing:YES];



#pragma mark  获取当前是什么类型的手机
// 不要忘记引入 ‘sys/utsname.h’头文件
-(NSString*)getPhoneType
    struct utsname systemInfo ;
    uname(&systemInfo);
    NSString * platform = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];
    if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 2G";
    if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
    if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
    if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
    if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
    if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5";
    if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c";
    if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c";
    if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s";
    if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s";
    if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
    if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
    if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6s";
    if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus";
    if ([platform isEqualToString:@"iPhone9,1"])    return @"国行、日版、港行iPhone 7";
    if ([platform isEqualToString:@"iPhone9,2"])    return @"港行、国行iPhone 7 Plus";
    if ([platform isEqualToString:@"iPhone9,3"])    return @"美版、台版iPhone 7";
    if ([platform isEqualToString:@"iPhone9,4"])    return @"美版、台版iPhone 7 Plus";
    if ([platform isEqualToString:@"iPhone8,4"]) return @"iPhone SE";
    if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G";
    if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
    if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G";
    if ([platform isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G";
    if ([platform isEqualToString:@"iPod5,1"]) return @"iPod Touch 5G";
    if ([platform isEqualToString:@"iPad1,1"]) return @"iPad 1G";
    if ([platform isEqualToString:@"iPad2,1"]) return @"iPad 2";
    if ([platform isEqualToString:@"iPad2,2"]) return @"iPad 2";
    if ([platform isEqualToString:@"iPad2,3"]) return @"iPad 2";
    if ([platform isEqualToString:@"iPad2,4"]) return @"iPad 2";
    if ([platform isEqualToString:@"iPad2,5"]) return @"iPad Mini 1G";
    if ([platform isEqualToString:@"iPad2,6"]) return @"iPad Mini 1G";
    if ([platform isEqualToString:@"iPad2,7"]) return @"iPad Mini 1G";
    if ([platform isEqualToString:@"iPad3,1"]) return @"iPad 3";
    if ([platform isEqualToString:@"iPad3,2"]) return @"iPad 3";
    if ([platform isEqualToString:@"iPad3,3"]) return @"iPad 3";
    if ([platform isEqualToString:@"iPad3,4"]) return @"iPad 4";
    if ([platform isEqualToString:@"iPad3,5"]) return @"iPad 4";
    if ([platform isEqualToString:@"iPad3,6"]) return @"iPad 4";
    if ([platform isEqualToString:@"iPad4,1"]) return @"iPad Air";
    if ([platform isEqualToString:@"iPad4,2"]) return @"iPad Air";
    if ([platform isEqualToString:@"iPad4,3"]) return @"iPad Air";
    if ([platform isEqualToString:@"iPad4,4"]) return @"iPad Mini 2G";
    if ([platform isEqualToString:@"iPad4,5"]) return @"iPad Mini 2G";
    if ([platform isEqualToString:@"iPad4,6"]) return @"iPad Mini 2G";
    if ([platform isEqualToString:@"i386"]) return @"iPhone Simulator";
    if ([platform isEqualToString:@"x86_64"]) return @"iPhone Simulator";
    return platform;



#pragma mark  获取图像的扩展名
/**
 @imageData 图像数据
 */
-(NSString*)getImageExtensionName:(NSData*)imageData
    if(imageData==nil) return nil;
    uint8_t name ;
    [imageData getBytes:&name length:1];
    switch (name) 
        case 0xFF:
            return @"jpg";
            break;
        case 0x89:
            return @"png";
            break;
        case 0x47:
            return @"gif";
            break;
        case '%':
            return @"pdf";
            break;
        default:
            return @"webp" ;
            break;
    



#pragma mark  获取一个类的所有属性
/**
 @className 类的名字
 */
-(void)getPropertiesOfClass:(const char*)className
    if (className==nil) return ;
    id class = objc_getClass(className);
    unsigned int count,i;
    objc_property_t * properties = class_copyPropertyList(class, &count);
    for (i=0; i<count; i++) 
        objc_property_t property = properties[i];
        fprintf(stdout, "%s %s\\n", property_getName(property), property_getAttributes(property));
    



#pragma mark  绘制圆角图像
/**
 @image 要变圆角的图像
 */
-(UIImage*)circleImage:(UIImage*)image
    if(image==nil) return nil;
    // 创建上下文
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 1.0);
    // 获取上下文
    CGContextRef contextRef = UIGraphicsGetCurrentContext() ;
    // 添加圆形区域
    CGContextAddEllipseInRect(contextRef, CGRectMake(0, 0, image.size.width, image.size.height));
    // 裁剪
    CGContextClip(contextRef);
    // 绘制图像
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    // 获取图像
    UIImage * imageCircle = UIGraphicsGetImageFromCurrentImageContext();
    // 关闭上下文
    UIGraphicsEndImageContext();
    return imageCircle;



#pragma mark  图像的拉伸
/**
 @image 拉伸的对象
 @edgeInset 拉伸的范围
 */
-(UIImage*)tensileImage:(UIImage*)image withEdgeInset:(UIEdgeInsets)edgeInset
    if (image==nil) 
        return image;
    
    return [image resizableImageWithCapInsets:edgeInset resizingMode:UIImageResizingModeStretch];



#pragma mark  JSON字符串转字典
/**
 @jsonStr JSON字符串对象
 */
-(NSDictionary*)jsonToDictionary:(NSString*)jsonStr
    if (jsonStr==nil) return nil;
    NSData * data = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    return [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];



#pragma mark  判断身份证号是否合法
/**
 @idNumberStr
 */
-(BOOL)judgmentIdNumberOflegitimacy:(NSString*)idNumberStr
    if (idNumberStr==nil) return NO;
    // 创建条件语句
    NSString * regex =  @"^(\\\\d14|\\\\d17)(\\\\d|[xX])$";
    // 谓词查询
    NSPredicate * identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [identityCardPredicate evaluateWithObject:idNumberStr];



#pragma mark   判断图像是否有透明度通道
/**
 @image 判断的对象
 */
-(BOOL)judgmentAlphaOfImage:(UIImage*)image
    if (image==nil) return NO;
    CGImageAlphaInfo imageInfo = CGImageGetAlphaInfo(image.CGImage);
    return (imageInfo == kCGImageAlphaLast || imageInfo == kCGImageAlphaFirst || kCGImageAlphaPremultipliedLast );



#pragma mark  根据屏幕大小获取相应的图像
/**
 @imageName 要加载的图像的名字
 */
-(UIImage*)imageWith:(NSString*)imageName
    if (imageName==nil) return nil;
    NSString * nameStr = [imageName componentsSeparatedByString:@"."].firstObject;
    // 判断屏幕分比率
    if ([UIScreen mainScreen].scale ==2.0) 
        [nameStr stringByAppendingString:@".@2x"];
    else if ([UIScreen mainScreen].scale == 3.0)
        [nameStr stringByAppendingString:@".@3x"];
    
    NSString * imagePath = [[NSBundle mainBundle] pathForResource:nameStr ofType:@"png"];
    if (imageName==nil) 
        return nil ;
    else
        return [UIImage imageWithContentsOfFile:imagePath];
    



#pragma mark  移除字符串中的空格
/**
 @doString  要处理的字符串
 */
-(NSString*)removeSpaceAndNewline:(NSString*)doString
    if (doString==nil) return nil;
    NSString * tempStr = [doString stringByReplacingOccurrencesOfString:@" " withString:@""];
    tempStr = [tempStr stringByReplacingOccurrencesOfString:@"\\r" withString:@""];
    tempStr = [tempStr stringByReplacingOccurrencesOfString:@"\\n" withString:@""];
    return tempStr;



#pragma mark  判断一个字符串是否为空
/**
 @doString 要判断的字符串
 */
-(BOOL)isEqualToNil:(NSString*)doString
    return doString.length <=0 || doString==nil || [doString isEqualToString:@""];



#pragma mark  删除某个View的所有子视图
/**
 @ doView  要处理的View
 */
-(void)removeSonsformView:(UIView*)doView
    if (doView==nil) return;
    [[doView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];



#pragma makr  让View 的背景色渐变
/**
 @doView  要渐变的View
 */
-(void)gradientViewOfBackGroundColor:(UIView*)doView withColors:(NSArray*)colors

    if (doView==nil) return;
    CAGradientLayer * gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = doView.bounds;
    gradientLayer.colors = colors;
    [doView.layer insertSublayer:gradientLayer atIndex:0];



#pragma mark  给某个View的一个角添加圆角
/**
 @doView 要添加的圆角的View
 */
-(void)addRadiiOfView:(UIView*)doView byRoundingCorners:(UIRectCorner)corners withRadii:(CGFloat)radii
    if (doView==nil) return;
    UIBezierPath * bzPath = [UIBezierPath bezierPathWithRoundedRect:doView.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radii,radii)];
    CAShapeLayer * shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = doView.bounds;
    shapeLayer.path = bzPath.CGPath;
    doView.layer.mask = shapeLayer;



#pragma mark  获取View在Window上的位置
/**
 @doView 要获取位置View
 */
-(CGPoint)getPointOfViewInWindow:(UIView*)doView
    if (doView==nil) return CGPointZero;
    return [doView convertPoint:CGPointMake(0, 0) fromView:[UIApplication sharedApplication].windows.lastObject];




#pragma mark  在View上添加虚线
/**
 @doView 要添加的虚线的View
 @lineColor 线的颜色
 */
-(void)addDottedLineToView:(UIView*)doView byLineInterval:(NSArray*)lineInterval withlineColor:(UIColor*)lineColor withLineWidth:(CGFloat)lineWidth loctionStart:(CGPoint)point1 loctionEnd:(CGPoint)point2
    if (doView==nil) return;
    CAShapeLayer * shapeLayer = [CAShapeLayer layer];
    shapeLayer.lineWidth = lineWidth;
    shapeLayer.lineDashPattern = lineInterval ;
    shapeLayer.fillColor = nil;
    shapeLayer.strokeColor = lineColor.CGColor ;
    UIBezierPath * bzPath = [UIBezierPath bezierPath];
    [bzPath moveToPoint:point1];
    [bzPath addLineToPoint:point2];
    shapeLayer.path = bzPath.CGPath ;
    [doView.layer addSublayer:shapeLayer];



#pragma makr  获取一个View所在的控制器
/**
 @doView 要获取控制器的对象
 */
-(UIViewController*)belongViewController:(UIView*)doView
    if (doView==nil) return nil;
    for (UIView * next = [doView superview]; next; next = next.superview) 
        UIResponder * nextRes = [next nextResponder];
        if ([nextRes isKindOfClass:[UIViewController class]]) 
            return (UIViewController*)nextRes ;
        
    
    return nil;


@end

该类持续更新,请尽情关注!!!!!

以上是关于iOS开发工具类 DFBox全面提升开发效率的主要内容,如果未能解决你的问题,请参考以下文章

OPPO广告联盟战略升级,全面提升开发者变现效率

OPPO广告联盟战略升级,全面提升开发者变现效率

让iOS开发变得更有效率-分类工具类

你不知道的Java工具类库,十倍提升开发效率

提升 iOS 开发效率! Xcode 9 内置模拟器的9个技巧

使用工具来提升Android开发效率