我的iOS - 百科全书(开发常用汇总-持续更新)

总结一些自己常用到,并且比较实用的开发技巧或者工具之类的干货吧。
归纳在一起,平时查阅起来就比较方便了。

(一)字符类

1. NSString与int和float的相互转换

  • 1.1 字符串拼接
    NSString *str1 = @"abc";
    NSString *str2 = @"123";
    NSString *newString = [NSString stringWithFormat:@"%@%@",str1,str2];
  • 1.2 字符转int
    NSString *str3 = @"456";
    int intString = [str3 intValue];
  • 1.3 int转字符
    NSString *stringInt = [NSString stringWithFormat:@"%d",intString];
  • 1.4 字符转float
    float floatString = [stringInt floatValue];
  • 1.5 float转字符
    NSString *stringFloat = [NSString stringWithFormat:@"%f",floatString];

2. 使用 arc4random 生成随机数

  • 2.1获取一个随机整数范围在:[0,100)包括0,不包括100
int o = arc4random() % 100;
  • 2.2 获取一个随机数范围在:[100,200],包括100,包括200
int t =100 + (arc4random() % 101);
  • 2.3 获取一个随机整数,范围在[from,to],包括from,包括to
-(int)getRandomNumber:(int)from to:(int)to{
        return (int)(from + (arc4random() % (to - from) + 1));
}

(二)宏定义

1. 各机型屏幕宽高适配

/** .m文件 */
#import 
//各机型屏幕宽高适配
#define ISIPHONE          [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone
#define ISIPHONE5          ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
#define ISIPHONE4          ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
#define ISIPHONE6          ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
#define ISIPHONE6plus         ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1080, 1920), [[UIScreen mainScreen] currentMode].size) : NO)
//对应屏幕分辨率
/*
 iPhone设备      尺寸 分辨率                   点
 iPhone 3和3s  3.5英寸    (320×480)         320×480
 iPhone 4和4s  3.5英寸    (640×960)         320×480
 iPhone 5和5s  4.0英寸    (640×1136)        320×568
 iPhone 6         4.7英寸    (750×1334)        375×667
 iPhone6 Plus:5.5英寸    (1080×1920)    414x736(打印测试出来的)
 */
#define NeedHightForiPhone5        568
#define NeedWidthForiPhone5        320
#define NeedHightForiPhone4        480
#define NeedWidthForiPhone4        320
#define NeedHightForiPhone6        667
#define NeedWidthForiPhone6        375
#define NeedHightForiPhone6plus       736
#define NeedWidthForiPhone6plus       414

#define SCALE_LOGICALPT_TO_DEVICEPX      ([[UIScreen mainScreen] scale])
#define POINT_TO_PIXEL(x)        (x*SCALE_LOGICALPT_TO_DEVICEPX)
#define PIXEL_TO_POINT(x)        (x/SCALE_LOGICALPT_TO_DEVICEPX)

#define TIMEOUT_VOICECALL_ENTER_STUDYROOM    50

@interface GlobalUtils : NSObject
+ (CGSize)selfBounds;
@end
/** .h文件 */
#import "GlobalUtils.h"
#import 

@implementation GlobalUtils

//不同机型的宽高
+(CGSize)selfBounds
{
    CGSize size;
    if (ISIPHONE4) {
        size=CGSizeMake(NeedWidthForiPhone4, NeedHightForiPhone4);
    }
    else if (ISIPHONE5) {
        size=CGSizeMake(NeedWidthForiPhone5, NeedHightForiPhone5);
    }
    else if (ISIPHONE6)
    {
        size=CGSizeMake(NeedWidthForiPhone6, NeedHightForiPhone6);
    }
    else if (ISIPHONE6plus)
    {
        size=CGSizeMake(NeedWidthForiPhone6plus, NeedHightForiPhone6plus);
    }
    return size;
}

@end

(三)工具类

1. 正则表达式

  • 1.1 验证邮箱格式
+(BOOL)isValidateEmail:(NSString *)email {
    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:email];
}
  • 1.2 判断手机号码
+(BOOL)isMobileNumber:(NSString *)mobileNum{
    NSString *MOBILE  =@"^1+[3578]+\\d{9}";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    if ([regextestmobile evaluateWithObject:mobileNum] == YES) {
        return YES;
    }
    else{
        return NO;
    }
}

2. 验证码倒计时

+ (void)timeDown:(UIButton *)button;
{
    //设置倒计时
    __block int timeout = 10; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束,关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                button.userInteractionEnabled = YES;
                [button setTitle:@"验证码" forState:UIControlStateNormal];
                button.backgroundColor = [UIColor orangeColor];
                button.layer.cornerRadius = button.height/5;
            });
        }else{//倒计时开始
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                [button setTitle:[NSString stringWithFormat:@"%@秒",strTime] forState:UIControlStateNormal];
                [UIView commitAnimations];
                button.userInteractionEnabled = NO;
                button.backgroundColor = [UIColor lightGrayColor];
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}

3. 给输入框(UITextField)的左侧设置图标

/**
 *  给UITextField设置左侧的图片
 *
 *  @param textField UITextField
 *  @param imageName 图片名称
 */
+(void)setLeftViewWithTextField:(UITextField *)textField imageName:(NSString *)imageName{
    UIImageView *leftView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 0, 40, 40)];
    leftView.image = [UIImage imageNamed:imageName];
    leftView.contentMode = UIViewContentModeCenter;
    textField.leftView = leftView;
    textField.leftViewMode = UITextFieldViewModeAlways;
}

4. 设置RGB为image

+ (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

5. Image转换成color

[UIColor colorWithPatternImage:[UIImageimageNamed:@“bg.png"]];

6. 隐藏UITableView(cell)多余的分割线

+ (void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view =[ [UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}

你可能感兴趣的:(我的iOS - 百科全书(开发常用汇总-持续更新))