UIEdgeInsets 设置包围tableView的坐标
- typedef struct UIEdgeInsets {
- CGFloat top, left, bottom, right; // specify amount to inset (positive) for each of the edges. values can be negative to 'outset'
- } UIEdgeInsets;
里面分别是上,左,下,右的包围长度,往下拖动时,如果top 》 0, 就会显示出来,如果小于0就会隐藏。
计算字符串的显示长度
- CGSize detailSize = [@"你的字符串" sizeWithFont:[UIFont systemFontOfSize:15]
- constrainedToSize:CGSizeMake(200, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
navigationbar的back键触发其他事件
- UIButton *back =[[UIButton alloc] initWithFrame:CGRectMake(200, 25, 63, 30)];
- [back addTarget:self act
- ion:@selector(reloadRowData:) forControlEvents:UIControlEventTouchUpInside];
- [back setImage:[UIImage imageNamed:@"返回按钮.png"] forState:UIControlStateNormal];
- UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:back];
- self.navigationItem.leftBarButtonItem = loginButtonItem
- [back release];
- [backButtonItem release];
防止屏幕暗掉锁屏
- [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
显示网络活动状态指示符
这是在iPhone左上部的状态栏显示的转动的图标指示有背景发生网络的活动。
- UIApplication* app = [UIApplication sharedApplication];
- app.networkActivityIndicatorVisible = YES;
获取UUID
- [[UIDevice currentDevice] uniqueIdentifier]
- UIDevice *myDevice = [UIDevice currentDevice];
- NSString *deviceID = [myDevice uniqueIdentifier];
截取屏幕图片
- UIGraphicsBeginImageContext(CGSizeMake(200,400)); //创建一个基于位图的图形上下文并指定大小为CGSizeMake(200,400)
- [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; //renderInContext 呈现接受者及其子范围到指定的上下文
- UIImage *aImage = UIGraphicsGetImageFromCurrentImageContext(); //返回一个基于当前图形上下文的图片
- UIGraphicsEndImageContext(); //移除栈顶的基于当前位图的图形上下文
- imageData = UIImagePNGRepresentation(aImage); //以png格式返回指定图片的数据
应用程序边框大小
我们应该使用"bounds"来获得应用程序边框。不是用"applicationFrame"。"applicationFrame"还包含了一个20像素的status bar。除非我们需要那额外的20像素的status bar。
震动和声音播放
- Sound will work in the Simulator, however some sound (such as looped) has been
- reported as not working in Simulator or even altogether depending on the audio format.
- Note there are specific filetypes that must be used (.wav in this example).
- AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
- SystemSoundID pmph;
- id sndpath = [NSBundle mainBundle] pathForResource:@"mySound" ofType:@"wav" inDirectory:@"/"];
- CFURLRef baseURL = (CFURLRef) [NSURL alloc] initFileURLWithPath:sndpath];
- AudioServicesCreateSystemSoundID (baseURL, &pmph);
- AudioServicesPlaySystemSound(pmph);
- [baseURL release];
Iphone获取本机IP
- -(NSString *)getAddress {
- char iphone_ip[255];
- strcpy(iphone_ip,"127.0.0.1"); // if everything fails
- NSHost* myhost =[NSHost currentHost];
- if (myhost)
- {
- NSString *ad = [myhost address];
- if (ad)
- strcpy(iphone_ip,[ad cStringUsingEncoding:NSASCIIStringEncoding]);
- }
- return [NSString stringWithFormat:@"%s",iphone_ip];
- }
NSString转化为UNICODE String:
(NSString*)fname = @“Test”; char fnameStr[10]; memcpy(fnameStr, [fname cStringUsingEncoding:NSUnicodeStringEncoding], 2*([fname length]));
NSString转化为char
(NSString*)fname = @“Test”;
char fnameStr[10];
fnameStr =[fname UTF8String];
char -> NSData:
char * postData = "TEST";
NSData *data = [NSData dataWithBytes:postData length:strlen(postData)];
转换为NSString: - (id)initWithUTF8String:(const char *)bytes
然后用NSString的 - (NSData *)dataUsingEncoding:(NSStringEncoding)encoding
NSData ->char
NSData returnData;
char* bu=[returnData bytes];
NSData->NSString
NSString* aStr;
aStr = [NSString alloc] initWithData:aData encoding:NSASCIIStringEncoding];
NSString->NSData
NSData* aData;
aData = [aStr dataUsingEncoding: NSASCIIStringEncoding];