您的位置:首页 > 编程语言

iphone开发常用代码(不断更…

2014-01-23 17:27 477 查看
好多实用功能
期待慢慢更新
我也会有时间完善一些这样的东西原文地址:iphone开发常用代码(不断更新)作者:暗夜函数名称 : checkStringSize

函数描述 : string 字数限制

输入参数 : string:要判断的string
;minSize:最小字数限制;maxSize:最大字数限制

输出参数 : N/A

返回值 :
BOOL:是否超出限制

备注 : 一个汉字占两个字符,其他字母占一个字符

-(BOOL)checkStringSize:(NSString*)string minSize:(int)minSize
maxSize:(int)maxSize

{

NSString
*regex = @"^[u4e00-u9fa5]";

int
length=[string length];

int
currentStringSize=0;

for
(int i=0;i<length;i++)
{//逐步判断string中的每个字符是否为汉字,是,占两个字符,不是,占一个字符

NSString
*subString = [string
substringWithRange:NSMakeRange( i, 1)] ;

if
([subString isMatchedByRegex:regex]) {

currentStringSize+=2;

}else
{

currentStringSize+=1;

}

}

if
(currentStringSize>=minSize&¤tStringSize<=maxSize)
{

return
YES;

}else
{

return
NO;

}

}

IOS 5
中documents目录下,设置文件不需要上传到ICloud上, 需要设置do not back up属性

#include <sys/xattr.h>

- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL

{

const
char* filePath = [[URL path] fileSystemRepresentation];

const
char* attrName = "com.apple.MobileBackup";

u_int8_t
attrValue = 1;

int
result = setxattr(filePath, attrName, &attrValue,
sizeof(attrValue), 0, 0);

return
result == 0;

}

去除NSString中的空格

[NSString
stringByTrimmingCharactersInSet:[NSCharacterSet
whitespaceCharacterSet]

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];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: