// 是否高清屏 #define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO) // 是否iPad #define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) // 是否模拟器 #define isSimulator (NSNotFound != [[[UIDevice currentDevice] model] rangeOfString:@"Simulator"].location) // 是否iphone5 #define IS_IPHONE_5 (fabs((double)[[UIScreen mainScreen] bounds].size.height - (double)568)<DBL_EPSILON)
//计算点到线段的最短距离 double x1, y1, x2, y2, x3, y3; double px = x2 - x1; double py = y2 - y1; double som = px * px + py * py; double u = ((x3 - x1) * px + (y3 - y1) * py) / som; if (u > 1) { u = 1; } if (u < 0) { u = 0; } //the closest point double x = x1 + u * px; double y = y1 + u * py; double dx = x - x3; double dy = y - y3; double dist = sqrt(dx*dx + dy*dy);
//怎么样判断两线段是否相交? 知道两线段的起始坐标和终止坐标 bool IsLineSegmentCross(POINT pFirst1, POINT pFirst2, POINT pSecond1, POINT pSecond2) { //每个线段的两点都在另一个线段的左右不同侧,则能断定线段相交 //公式对于向量(x1,y1)->(x2,y2),判断点(x3,y3)在向量的左边,右边,还是线上. //p=x1(y3-y2)+x2(y1-y3)+x3(y2-y1).p<0 左侧, p=0 线上, p>0 右侧 long Linep1,Linep2; //判断pSecond1和pSecond2是否在pFirst1->pFirst2两侧 Linep1 = pFirst1.x * (pSecond1.y - pFirst2.y) + pFirst2.x * (pFirst1.y - pSecond1.y) + pSecond1.x * (pFirst2.y - pFirst1.y); Linep2 = pFirst1.x * (pSecond2.y - pFirst2.y) + pFirst2.x * (pFirst1.y - pSecond2.y) + pSecond2.x * (pFirst2.y - pFirst1.y); if ( ((Linep1 ^ Linep2) >= 0 ) && !(Linep1==0 && Linep2==0))//符号位异或为0:pSecond1和pSecond2在pFirst1->pFirst2同侧 { return false; } //判断pFirst1和pFirst2是否在pSecond1->pSecond2两侧 Linep1 = pSecond1.x * (pFirst1.y - pSecond2.y) + pSecond2.x * (pSecond1.y - pFirst1.y) + pFirst1.x * (pSecond2.y - pSecond1.y); Linep2 = pSecond1.x * (pFirst2.y - pSecond2.y) + pSecond2.x * (pSecond1.y - pFirst2.y) + pFirst2.x * (pSecond2.y - pSecond1.y); if ( ((Linep1 ^ Linep2) >= 0 ) && !(Linep1==0 && Linep2==0))//符号位异或为0:pFirst1和pFirst2在pSecond1->pSecond2同侧 { return false; } //否则判为相交 return true; }
实现下面的委托 #define NUMBERS @"0123456789\n" - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSCharacterSet *cs; if(textField == phoneNumberField) { cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet]; NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""]; BOOL basicTest = [string isEqualToString:filtered]; if(!basicTest) { UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入数字" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alert show]; [alert release]; return NO; } } //其他的类型不需要检测,直接写入 return YES; } 如果输入的不是数字,进行提示。
string 转成 char: char mychar[100]; NSString * mystring; strcpy(mychar,(char *)[mystring UTF8String]); char 转成 string: NSString *TempString; char mychar[100]; mystring=[NSString stringWithFormat:@"%s",mychar];
/* Erica Sadun, [url]http://ericasadun.com[/url] iPhone Developer's Cookbook, 3.0 Edition BSD License, Use at your own risk */ #import <foundation foundation.h=""> @interface NSString (UtilityExtensions) - (NSString *) stringByAppendingRandomStringOfRandomLength; - (int) occurrencesOfString: (NSString *) aString; - (NSDate *) date; + (NSString *) commasForNumber: (long long) num; @property (readonly) NSString *trimmedString; @property (readonly) NSDate *date; @end @interface NSString (UTF8String) @property (readonly) char *UTF8String; // a favorite! @end /* Erica Sadun, [url]http://ericasadun.com[/url] iPhone Developer's Cookbook, 3.0 Edition BSD License, Use at your own risk */ #import "NSString-Utilities.h" @implementation NSString (UtilityExtensions) - (NSString *) trimmedString { return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; } - (int) occurrencesOfString: (NSString *) aString { return [self componentsSeparatedByString:aString].count -1; } // run srandom() somewhere in your app // [url]http://tinypaste.com/5f1c9[/url] // Requested by BleuLlama - (NSString *) stringByAppendingRandomStringOfRandomLength { int len = random() % 32; NSMutableArray *chars = [NSMutableArray array]; NSMutableString *s = [NSMutableString stringWithString:self]; NSMutableCharacterSet *cs = [[NSMutableCharacterSet alloc] init]; [cs formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]]; [cs formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]]; // [cs formUnionWithCharacterSet:[NSCharacterSet symbolCharacterSet]]; // [cs formUnionWithCharacterSet:[NSCharacterSet punctuationCharacterSet]]; // init char array from charset for (int c = 0; c < 128; c++) // 7 bit only if ([cs characterIsMember:(unichar)c]) [chars addObject:[NSString stringWithFormat:@"%c", c]]; for (int i = 0; i < len; i++) [s appendString:[chars objectAtIndex:(random() % chars.count)]]; [cs release]; return s; } - (NSDate *) date { // Return a date from a string NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; formatter.dateFormat = @"MM-dd-yyyy"; NSDate *date = [formatter dateFromString:self]; return date; } // return a comma delimited string + (NSString *) commasForNumber: (long long) num { if (num < 1000) return [NSString stringWithFormat:@"%d", num]; return [[self commasForNumber:num/1000] stringByAppendingFormat:@",%03d", (num % 1000)]; } @end
//保存当前view到相册 UIGraphicsBeginImageContext(currentView.bounds.size); //currentView 当前的view [currentView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
//iPhone 处理大文件(MD5) +(NSString*)fileMD5:(NSString*)path { NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:path]; if( handle== nil ) return @"ERROR GETTING FILE MD5"; // file didnt exist CC_MD5_CTX md5; CC_MD5_Init(&md5); BOOL done = NO; while(!done) { NSData* fileData = [handle readDataOfLength: CHUNK_SIZE ]; CC_MD5_Update(&md5, [fileData bytes], [fileData length]); if( [fileData length] == 0 ) done = YES; } unsigned char digest[CC_MD5_DIGEST_LENGTH]; CC_MD5_Final(digest, &md5); NSString* s = [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", digest[0], digest[1], digest[2], digest[3], digest[4], digest[5], digest[6], digest[7], digest[8], digest[9], digest[10], digest[11], digest[12], digest[13], digest[14], digest[15]]; return s; }