美文网首页
iOS Object-C 常用到的代码块(持续更新)

iOS Object-C 常用到的代码块(持续更新)

作者: jgyhc | 来源:发表于2019-06-28 15:13 被阅读0次

    YYModel相关的:

    • 声明自定义类参数类型
    + (NSDictionary *)modelContainerPropertyGenericClass {
        return @{@"<#arrayName#>" : [<#ClassName#> class]};
    }
    
    • 黑名单(如果实现了该方法,则处理过程中会忽略该列表内的所有属性)
    + (NSArray *)modelPropertyBlacklist {
      return @[@"<#propertyName#>"];
    }
    
    • 白名单(如果实现了该方法,则处理过程中不会处理该列表外的属性。)
    + (NSArray *)modelPropertyWhitelist {
      return @[@"<#property#>"];
    }
    

    系统功能

    • 拨打电话
    NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"[telprompt://%@](telprompt://%25@)",@"<#phone#>"];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]]
    
    • 手机系统版本:9.1
    NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];
    
    • 手机类型:iPhone 6(方法在下面)
    NSString* phoneModel = [self iphoneType];
    
    - (NSString *)iphoneType {
    需要导入头文件:#import <sys/utsname.h>
    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:@"iPhone8,4"]) return @"iPhone SE";
    if ([platform isEqualToString:@"iPhone9,1"]) return @"iPhone 7";
    if ([platform isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus";
    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;
    }
    
    • 手机系统:iPhone OS
    NSString * iponeM = [[UIDevice currentDevice] systemName];
    
    • 电池电量
    CGFloat batteryLevel=[[UIDevice currentDevice] batteryLevel];
    

    UIKit

    • UIAlertController
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"<#title#>" message:@"<#content#>" preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        [self presentViewController:alertController animated:YES completion:nil];
    
    • UILabel初始化
    UILabel *label = ({
        UILabel *label = [[UILabel alloc] init];
        label.font = [UIFont systemFontOfSize:13];
        label.textColor = [UIColor whiteColor];
        label;
    })
    
    • 修改UITextField中Placeholder的文字颜色
    [textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; 
    

    其他

    • 单例初始化
    + (instancetype)sharedInstance {
        static <#ClassName#> *instance;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            instance = [[<#ClassName#> alloc] init];
        });
        return instance;
    }
    
    • JSON字符串转化为字典
    + (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
    {
        if (jsonString == nil) {
            return nil;
        }
        
        NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
        NSError *err;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                            options:NSJSONReadingMutableContainers
                                                              error:&err];
        if(err)
        {
            NSLog(@"json解析失败:%@",err);
            return nil;
        }
        return dic;
    }
    

    16进制颜色宏定义

    #define HEX_RGBA(rgbValue,a) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:(a)]
    #define HEX_RGB(rgbValue)[UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:(1)]
    #define RGB(R,G,B)      [UIColor colorWithRed:R/255.0f green:G/255.0f blue:B/255.0f alpha:1.0f]
    #define RGBA(R,G,B,A)   [UIColor colorWithRed:R/255.0f green:G/255.0f blue:B/255.0f alpha:A]
    
    • 随机颜色
    #define random(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)/255.0]
    #define randomColor random(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))
    
    • 枚举
    typedef enum NSUInteger {
        <#enumNameType#> ,
        <#enumNameType#>
    } <#enumName#>;
    
    
    • 阿拉伯数字转中文格式
    +(NSString *)translation:(NSString *)arebic
    { 
      NSString *str = arebic;
      NSArray *arabic_numerals = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0"];
      NSArray *chinese_numerals = @[@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"零"];
      NSArray *digits = @[@"个",@"十",@"百",@"千",@"万",@"十",@"百",@"千",@"亿",@"十",@"百",@"千",@"兆"];
      NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:chinese_numerals forKeys:arabic_numerals];
     
      NSMutableArray *sums = [NSMutableArray array];
      for (int i = 0; i < str.length; i ++) {
        NSString *substr = [str substringWithRange:NSMakeRange(i, 1)];
        NSString *a = [dictionary objectForKey:substr];
        NSString *b = digits[str.length -i-1];
        NSString *sum = [a stringByAppendingString:b];
        if ([a isEqualToString:chinese_numerals[9]])
        {
          if([b isEqualToString:digits[4]] || [b isEqualToString:digits[8]])
          {
            sum = b;
            if ([[sums lastObject] isEqualToString:chinese_numerals[9]])
            {
              [sums removeLastObject];
            }
          }else
          {
            sum = chinese_numerals[9];
          }
     
          if ([[sums lastObject] isEqualToString:sum])
          {
            continue;
          }
        }
     
        [sums addObject:sum];
      }
     
      NSString *sumStr = [sums componentsJoinedByString:@""];
      NSString *chinese = [sumStr substringToIndex:sumStr.length-1];
      NSLog(@"%@",str);
      NSLog(@"%@",chinese);
      return chinese;
    }
    
    
    • 颜色转图片
    - (UIImage*)createImageWithColor:(UIColor*)color {
        CGRect rect = CGRectMake(0.0f,0.0f,1.0f,1.0f);
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetFillColorWithColor(context, [color CGColor]);
        CGContextFillRect(context, rect);
        UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return theImage;
    }
    
    • 去掉小数后多余的0
    
    - (NSString *)removeFloatAllZero:(NSString *)string {
        NSString * testNumber = string;
        NSString * outNumber = [NSString stringWithFormat:@"%@",@(testNumber.floatValue)];
        //价格格式化显示
        NSNumberFormatter * formatter = [[NSNumberFormatter alloc]init];
        formatter.numberStyle = kCFNumberFormatterDecimalStyle;
        NSString * formatterString = [formatter stringFromNumber:[NSNumber numberWithFloat:[outNumber doubleValue]]];
        //获取要截取的字符串位置
        NSRange range = [formatterString rangeOfString:@"."];
        if (range.length > 0 ) {
            NSString * result = [formatterString substringFromIndex:range.location];
            if (result.length >= 4) {
                formatterString = [formatterString substringToIndex:formatterString.length - 1];
            }
        }
        return formatterString;
    }
    
    

    相关文章

      网友评论

          本文标题:iOS Object-C 常用到的代码块(持续更新)

          本文链接:https://www.haomeiwen.com/subject/iazscctx.html