"] baseURL:[NSURL fileURLWithPath:boundle]];
从网页加载图片并让图片在规定长宽中缩小
[cell.img loadHTMLString:[NSString stringWithFormat:@"",goodsInfo.GoodsImg] baseURL:nil];
将网页加载到webview上通过javascript获取里面的数据,如果只是发送了一个连接请求获取到源码以后可以用正则表达式进行获取数据
NSString *javaScript1 = @"document.getElementsByName(‘.u‘).item(0).value";
NSString *javaScript2 = @"document.getElementsByName(‘.challenge‘).item(0).value";
NSString *strResult1 = [NSString stringWithString:[theWebView stringByEvaluatingJavaScriptFromString:javaScript1]];
NSString *strResult2 = [NSString stringWithString:[theWebView stringByEvaluatingJavaScriptFromString:javaScript2]];
用NSString怎么把UTF8转换成unicode
utf8Str //
NSString *unicodeStr = [NSString stringWithCString:[utf8Str UTF8String] encoding:NSUnicodeStringEncoding];
View自己调用自己的方法:
[self performSelector:@selector(loginToNext) withObject:nil afterDelay:2];//黄色段为方法名,和延迟几秒执行.
显示图像:
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"myImage.png"]];
myImage.opaque = YES; //opaque是否透明
[self.view addSubview:myImage];
[myImage release];
WebView:
CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0);
UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];
[webView setBackgroundColor:[UIColor whiteColor]];
NSString *urlAddress = @"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self addSubview:webView];
[webView release];
显示网络活动状态指示符
这是在iPhone左上部的状态栏显示的转动的图标指示有背景发生网络的活动。
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
动画:一个接一个地显示一系列的图象
NSArray *myImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"myImage1.png"], [UIImage imageNamed:@"myImage2.png"], [UIImage imageNamed:@"myImage3.png"], [UIImage imageNamed:@"myImage4.gif"], nil];
UIImageView *myAnimatedView = [UIImageView alloc];
[myAnimatedView initWithFrame:[self bounds]];
myAnimatedView.animationImages = myImages; //animationImages属性返回一个存放动画图片的数组
myAnimatedView.animationDuration = 0.25; //浏览整个图片一次所用的时间
myAnimatedView.animationRepeatCount = 0; // 0 = loops forever 动画重复次数
[myAnimatedView startAnimating];
[self addSubview:myAnimatedView];
[myAnimatedView release];
动画:显示了something在屏幕上移动。注:这种类型的动画是“开始后不处理” -你不能获取任何有关物体在动画中的信息(如当前的位置) 。如果您需要此信息,您会手动使用定时器去调整动画的X和Y坐标
这个需要导入QuartzCore.framework
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
//Creates and returns an CAPropertyAnimation instance for the specified key path.
//parameter:the key path of the property to be animated
theAnimation.duration=1;
theAnimation.repeatCount=2;
theAnimation.autoreverses=YES;
theAnimation.fromValue=[NSNumber numberWithFloat:0];
theAnimation.toValue=[NSNumber numberWithFloat:-60];
[view.layer addAnimation:theAnimation forKey:@"animateLayer"];
Draggable items//拖动项目
Here‘s how to create a simple draggable image.//这是如何生成一个简单的拖动图象
1. Create a new class that inherits from UIImageView
@interface myDraggableImage : UIImageView { }
2. In the implementation for this new class, add the 2 methods:
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
// Retrieve the touch point 检索接触点
CGPoint pt = [[touches anyObject] locationInView:self];
startLocation = pt;
[[self superview] bringSubviewToFront:self];
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
// Move relative to the original touch point 相对以前的触摸点进行移动
CGPoint pt = [[touches anyObject] locationInView:self];
CGRect frame = [self frame];
frame.origin.x += pt.x - startLocation.x;
frame.origin.y += pt.y - startLocation.y;
[self setFrame:frame];
}
3. Now instantiate the new class as you would any other new image and add it to your view
//实例这个新的类,放到你需要新的图片放到你的视图上
dragger = [[myDraggableImage alloc] initWithFrame:myDragRect];
[dragger setImage:[UIImage imageNamed:@"myImage.png"]];
[dragger setUserInteractionEnabled:YES];
线程:
1. Create the new thread:
[NSThread detachNewThreadSelector:@selector(myMethod) toTarget:self withObject:nil];
2. Create the method that is called by the new thread:
- (void)myMethod
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
*** code that should be run in the new thread goes here ***
[pool release];
}
//What if you need to do something to the main thread from inside your new thread (for example, show a loading //symbol)? Use performSelectorOnMainThread.
[self performSelectorOnMainThread:@selector(myMethod) withObject:nil waitUntilDone:false];
Plist files
Application-specific plist files can be stored in the Resources folder of the app bundle. When the app first launches, it should check if there is an existing plist in the user‘s Documents folder, and if not it should copy the plist from the app bundle.
// Look in Documents for an existing plist file
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
myPlistPath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat: @"%@.plist", plistName] ];
[myPlistPath retain];
// If it‘s not there, copy it from the bundle
NSFileManager *fileManger = [NSFileManager defaultManager];
if ( ![fileManger fileExistsAtPath:myPlistPath] )
{
NSString *pathToSettingsInBundle = [[NSBundle mainBundle] pathForResource:plistName ofType:@"plist"];
}
//Now read the plist file from Documents
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *path = [documentsDirectoryPath stringByAppendingPathComponent:@"myApp.plist"];
NSMutableDictionary *plist = [NSDictionary dictionaryWithContentsOfFile: path];
//Now read and set key/values
myKey = (int)[[plist valueForKey:@"myKey"] intValue];
myKey2 = (bool)[[plist valueForKey:@"myKey2"] boolValue];
[plist setValue:myKey forKey:@"myKey"];
[plist writeToFile:path atomically:YES];
Alerts
Show a simple alert with OK button.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:
@"An Alert!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
Info button
Increase the touchable area on the Info button, so it‘s easier to press.
CGRect newInfoButtonRect = CGRectMake(infoButton.frame.origin.x-25, infoButton.frame.origin.y-25, infoButton.frame.size.width+50, infoButton.frame.size.height+50);
[infoButton setFrame:newInfoButtonRect];
Detecting Subviews
You can loop through subviews of an existing view. This works especially well if you use the "tag" property on your views.
for (UIImageView *anImage in [self.view subviews])
{
if (anImage.tag == 1)
{ // do something }
}
16.
//UILabel 和字体大小的匹配,可以用到制作电子书的时候。
复制代码
复制代码
//UILabel 和字体大小的匹配,可以用到制作电子书的时候。
NSString *str = @"UILabel 和字体大小的匹配.亲爱的,在一起的日子很平淡,似乎波澜不惊,只是,这种平凡的日子是最浪漫的,对吗?遇到你之前,世界像一片荒草原;遇到你之后,世界像一个游乐园。过去的许多岁月,对我来说像一缕轻烟,未来的无限生涯,因你而幸福无边。爱一个人是在夜里等待另一个人的呼吸,虽然隔着千里万里,但我知道你在手机的那一端,于是我便会夜夜等待.守株待兔是人间最大的幸福,因为我有目标,你就是我要逮那只兔子,是我一生要好好照顾保护那个人.没有你的时候,你就是我的世界;和你在一起时,世界就是你的。亲爱的,情人节快乐。";
UIFont *font = [UIFont fontWithName:@"Arial" size:36.0f];
CGSize size = CGSizeMake(320, 2000);
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];//先设为“0”的大小状态
label.numberOfLines = 0;//当设为0的时候,label显示多行。
CGSize labelSize = [str sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];//得到size的宽和高
NSLog(@"%f",labelSize.height);
label.frame = CGRectMake(0, 0, labelSize.width, labelSize.height);
label.text = str;
label.font = font;
label.textColor = [UIColor blueColor];
[self.view addSubview:label];
[label release];
复制代码
复制代码
17.选中某一行的时候,该行颜色变化
[tableView deselectRowAtIndexPath:indexPath animated:NO];//选择当前行,使得改行颜色变化,选中后的反显颜色即刻消失。
18.ios 音频后台播放
在按Home的情况下,后台如何播放音乐?
在Info.plist增加一项 Required backgroound modes默然是数组,在其下增加一个元素App plays audio
之后在代码中添加:
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
[[AVAudioSession sharedInstance] setActive:YES error: nil];
或者是下面的代码:
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];11:47:36
放到初始化的中或在播放音乐前的即可。
19。数组排序
复制代码
复制代码
for (int j=0;j<[arr count];j++)
{
for (int i=0; i<[arr count]-1-j; i++) {
if ([[arr objectAtIndex:i] intValue]>[[arr objectAtIndex:i+1] intValue]) {
aa = [arr objectAtIndex:i+1];
[arr replaceObjectAtIndex:i+1 withObject:[arr objectAtIndex:i]];
[arr replaceObjectAtIndex:i withObject:aa];
}
}
}
或者这样。。。。。。。。
//对数组进行排序
-(void)paixuForArray:(NSMutableArray *)_array
{
NSString *aa;
for (int j=0;j<[_array count];j++)
{
for (int i=0; i<[_array count]-1-j; i++) {
if ([[[_array objectAtIndex:i] substringWithRange:NSMakeRange(3, 4)] intValue]<[[[_array objectAtIndex:i+1] substringWithRange:NSMakeRange(3, 4)] intValue]) {
aa = [_array objectAtIndex:i+1];
[_array replaceObjectAtIndex:i+1 withObject:[_array objectAtIndex:i]];
[_array replaceObjectAtIndex:i withObject:aa];
}
}
}
}
复制代码
复制代码
20。获取当前的日期,时间,星期几
复制代码
复制代码
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps;
// 年月日获得
comps = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
fromDate:date];
NSInteger year = [comps year];
NSInteger month = [comps month];
NSInteger day = [comps day];
NSLog(@"year: %d month: %d, day: %d", year, month, day);
//当前的时分秒获得
comps = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit)
fromDate:date];
NSInteger hour = [comps hour];
NSInteger minute = [comps minute];
NSInteger second = [comps second];
NSLog(@"hour: %d minute: %d second: %d", hour, minute, second);
// 周几和星期几获得
comps = [calendar components:(NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit)
fromDate:date];
NSInteger week = [comps week]; // 今年的第几周
NSInteger weekday = [comps weekday]; // 星期几(注意,周日是“1”,周一是“2”。。。。)
NSInteger weekdayOrdinal = [comps weekdayOrdinal]; // 这个月的第几周
NSLog(@"week: %d weekday: %d weekday ordinal: %d", week, weekday, weekdayOrdinal);
参考相关文章介绍,下面两篇文章介绍的很像细。
http://www.2cto.com/kf/201202/118985.html
http://www.cppblog.com/walkklookk/archive/2011/09/15/155852.aspx
复制代码
复制代码
21。
iPhone随机数
求随机数的三种方法:
1. srand((unsigned)time(0));
int i = rand() % 5;
2. srandom(time(0));
int i = random() % 5;
3. int i = arc4random() % 5 (常用) ;
参考:
http://www.cocoachina.com/bbs/read.php?tid=70719&keyword=%CB%E6%BB%FA%CA%FD
http://www.cocoachina.com/bbs/read.php?tid-2977-fpage-2-toread--page-1.html
http://www.codeios.com/thread-310-1-1.html
ios编程:iPhone How-to:给导航栏贴图
时间:2011-04-22 csdn博客 林家男孩
通过tintColor属性可以定制UINavigationBar的背景颜色,但如果需要设定渐变色、甚至纹理来说,就需要贴图了。 比较“暴力”的一种做法就是通过Category来重新实现- (void) drawRect:(CGRect)rect的实现,“暴力”是因为这种杀伤面很广,所有项目内的UINavigationBar都会因此改变。这点在应 用中应该格外小心。
@interface UINavigationBar (ImageBackground)
@end
@implementation UINavigationBar (ImageBackground)
- (void) drawRect:(CGRect)rect {
[[UIImage imageNamed:@"bkimage.png"] drawInRect:rect];
}
@endiOS常用代码
原文:http://www.cnblogs.com/QQ765286788/p/4766352.html