ios _怕忘记的一些备用代码

//删除字符串最后一个字符

NSString *newStr = [str substringToIndex:[str length]-1];


// 把时间的-: 例如 14-05 --> 14:05

NSString *tempTime = [self.timeText.text stringByReplacingOccurrencesOfString:@"-" withString:@":"];

reason.time = [NSString stringWithFormat:@"%@:00",tempTime];


// 设置日期滚动范围

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDate *currentDate = [NSDate date];

NSDateComponents *comps = [[NSDateComponents alloc] init];

[comps setYear:3];

NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];

[comps setYear:0];

NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];

[datePicker setMaximumDate:maxDate];

[datePicker setMinimumDate:minDate];


//限定字符串长度

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    

    if (textField == self.phoneNumField || textField == self.loanTimeLengthField) {

        NSUInteger lengthOfString = string.length;

        for (NSInteger loopIndex = 0; loopIndex < lengthOfString; loopIndex++) {

            unichar character = [string characterAtIndex:loopIndex];

            if (character < 48) return NO;

            if (character > 57) return NO;

        }

        

        NSInteger i = (textField == self.phoneNumField?11:6);

        

        NSUInteger proposedNewLenght = textField.text.length - range.length + string.length;

        if (proposedNewLenght > i) return NO;

        

    }

    return YES;

}


//设置textField位数限制 (如超过位数则不可用)

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    //4857asc0此代码意为只可以输入0-9的数字

    if (textField == self.phoneNumField) {

        NSUInteger lengthStr = string.length;

        for (NSInteger index = 0; index < lengthStr; index++) {

            unichar character = [string characterAtIndex:index];

            if (character < 48) return NO;

            if (character > 57) return NO;

        }

        

        NSInteger i = (textField == self.phoneNumField?11:2);

        

        NSUInteger proposedNewLenght = textField.text.length - range.length + string.length;

        if (proposedNewLenght > i) return NO;

        

    }

  //

        if (textField == self.ageText) {

        NSInteger i = 10;

        NSUInteger lenght = textField.text.length - range.length + string.length;

        if (lenght > i) return NO;

    }


    return YES;

}



------------1. //cell选中效果松开消失

    [tableView  deselectRowAtIndexPath :indexPath  animated : YES ]


------------2. //这只这个防止TextView上边那段空白,Scrllview的原因,因为nav导致的空白

self.automaticallyAdjustsScrollViewInsets = NO;

 

------------3. // 以下两个方法可以保证CZSettingController创建出来肯定是Grouped类型的表格

- (id)init

{

    return [super initWithStyle:UITableViewStyleGrouped];

}

 

- (id)initWithStyle:(UITableViewStyle)style

{

    return [super initWithStyle:UITableViewStyleGrouped];

}

 

------------4. //修改状态栏背景

修改info文件 添加一句: View controller-basedstatus bar appearance = NO

[[UIApplication sharedApplicationsetStatusBarStyle:UIStatusBarStyleLightContent];

 

------------5. // 二进制

   manager.requestSerializer = [AFHTTPRequestSerializer serializer];

   manager.responseSerializer = [AFHTTPResponseSerializer serializer];

   manager.responseSerializer.acceptableContentTypes = [NSSetsetWithObjects:@"application/json", @"text/json",@"text/javascript", @"text/html", nil];

 

------------6. //沙盒目录

NSString *path = NSHomeDirectory();

NSLog(@"NSHomeDirectory:%@",path);

 

------------7. // 获得资源中本地字符串

#define LOCALIZED_STRING(name)NSLocalizedStringFromTable(name, @"infoPlist", nil)


------------8. // 自定义Log

#ifdef DEBUG // 如果是调试阶段, 将DDLog替换为NSLog

#define DDLog(...)NSLog(__VA_ARGS__)

#else // 如果是发布阶段, 将DDLog替换为空白

#define DDLog(...)

#endif


------------9. // 快速创建颜色

#define DDColor(r, g, b, a)[UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0alpha:(a)/255.0];

// 随机颜色

#define DDRandomColorDDColor(arc4random_uniform(255), arc4random_uniform(255),arc4random_uniform(255), arc4random_uniform(255))

 

------------10. //SVPullToRefresh修改  .m文件197行  

 // CGFloat scrollOffsetThreshold =scrollViewContentHeight-self.scrollView.bounds.size.height;

//这句话代替上句可避免首页刷新bug

  CGFloatscrollOffsetThreshold =MAX(scrollViewContentHeight-self.scrollView.bounds.size.height,0);


------------11. //数据间转换

1.NSString相关

    1.NSDictionary转换成NSString:

    NSArray *array = [NSArray arrayWithObjects:@“hello”,@“world”,nil];

    NSString *str = [array componentsJoinedByString@“”];

    2.char* 转换成NSString

    NSString *dataStr = [NSString stringWithCSting:(char *)encoding:NSUTF8StringEncoding];

    3.NSData转换成NSString

    NSString *str = [[NSString alloc] initWithData:dataencoding:NSUTF8StingEncoding];

2.NSDictionary相关

    1.NSDictionary转成josn字符串

    -(NSString *)dictionaryToJson:(NSDictionary *)dic

    {

    NSError *parseError = nil;

    NSData *jsonData = [NSJsonSerialization dataWithJSONObject:dicoptions:NSJSONWritingPrettyPrinted error:&parseError];

    return [[NSSting alloc] initWithData:jsonDataencoding:NSUTF8StringEncoding];

    }

    2.NSString转换成NSDictionary

    NSDictionary *dic = [dicStr JSONValue];

3.NSData相关

    1.Byte转NSData

    NSData *data = [NSData dataWithBytes:byteslength:Strlen(packet->getPayload())];


------------12. 判断当前型号方法


//iPhone 4尺寸
#define iPhone4 ([UIScreeninstancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640,960), [[UIScreen mainScreen] currentMode].size) : NO)


//iPhone 5尺寸
#define iPhone5 ([UIScreeninstancesRespondToSelector:@selector(currentMode)] ?CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen]currentMode].size) : NO)


//iPhone 6 和 6+尺寸
#define iPhone6 ([UIScreeninstancesRespondToSelector:@selector(currentMode)] ?(CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen]currentMode].size) || CGSizeEqualToSize(CGSizeMake(1920,1080), [[UIScreenmainScreen] currentMode].size)) : NO)


你可能感兴趣的:(ios,object)