Objective C 从NSDate获取日期名称

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEEE"];
NSString *dayName = [dateFormatter stringFromDate:inDate];
//Optional - get first 3 letters of days name
dayName = [dayName substringToIndex:3];
[dateFormatter release];

Objective C 可能的UIWebView滚动(Objective-C)

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,480)];
[[[webView subviews] lastObject] setScrollEnabled:NO];

Objective C 移至子视图

//Move to child view
    NSInteger row = popOutSlot-1;
	if (self.bookDetailViewController == nil) {
		BookDetailViewController *aBookDetail = [[BookDetailViewController alloc] initWithNibName:@"BookDetailView" bundle:nil];
		self.bookDetailViewController = aBookDetail;
		[aBookDetail release];
	}
	bookDetailViewController.title = [NSString stringWithFormat:@"%@", [booksArray objectAtIndex:row]];
		
	[self.navigationController pushViewController:bookDetailViewController animated:YES];

Objective C 添加到OS X状态托盘

@interface Tray : NSObject <NSApplicationDelegate> {
	NSStatusItem *trayItem;
}
@end

@implementation Tray

- (IBAction)testAction:(id)sender;
{
	NSLog(@"Hello World");
}

- (IBAction)quitAction:(id)sender;
{
	[NSApp terminate:sender];
}

- (void)applicationDidFinishLaunching:(NSNotification *)note;
{
	NSZone *zone = [NSMenu menuZone];
	NSMenu *menu = [[[NSMenu allocWithZone:zone] init] autorelease];
	NSMenuItem *item;
	
	item = [menu addItemWithTitle:@"Testing" action:@selector(testAction:) keyEquivalent:@""];
	[item setTarget:self];
	
	item = [menu addItemWithTitle:@"Quit" action:@selector(quitAction:) keyEquivalent:@""];
	[item setTarget:self];
	
	trayItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
	[trayItem setMenu:menu];
	[trayItem setHighlightMode:YES];
	[trayItem setTitle:@"HERE"];
}

- (void)dealloc;
{
	[trayItem release];
	[super dealloc];
}

@end

Objective C 使用NSMutableArray构建图像数组

//First, define how many images will be in your array
//Just change to adjust to your own number of images
#define IMAGE_COUNT 20


//create an array to hold the images

NSMutableArray *imageArray = [[NSMutableArray alloc] initWithCapacity:IMAGE_COUNT];

//build array of images, cycling through image names
int i;
for(i=1; i<IMAGE_COUNT; i++)
[imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"frame-%d.png", i]]];

//another short example:
for (int c=0;c<300;c++)
{
    NSString *imageName = [NSString stringWithFormat:@"name%d.png", c];
    [UIImage imageNamed: imageName];
}

Objective C 适用于iOS的警报(UIAlertView)

/*
Simple alert for iOS applications
*/

// Alloc and init the alert object
UIAlertView *alert = [[UIAlertView alloc] 

// Alert window title
initWithTitle: NSLocalizedString(@"",nil)

// Message that will be displayed
message: NSLocalizedString(@"",nil)

// UIAlertView delegate (No protocol conformance required)
delegate: self

// Text for the main button
cancelButtonTitle: NSLocalizedString(@"No",nil)

// A second button or just nil for none
otherButtonTitles: NSLocalizedString(@"Yes",nil), nil];

// Display the alert
[alert show];

// Release the object
[alert release];

Objective C 捕获UIImage并编写png

- (NSString *)saveBaseDir {
  NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                       NSUserDomainMask,
                                                       YES);  
  return [array objectAtIndex:0];
}
 
- (BOOL) captureView:(UIView *)aview name:(NSString *)filename {
 
  UIGraphicsBeginImageContext(aview.frame.size);
	[aview.layer renderInContext:UIGraphicsGetCurrentContext()];
	UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
 
  NSString *saveFileName = filename;
  NSString *saveFilePath = [[self saveBaseDir] stringByAppendingPathComponent:saveFileName];
 
  NSData *data = UIImagePNGRepresentation(viewImage);
  NSError *error = nil;
  [data writeToFile:saveFilePath options:NSDataWritingAtomic error:&error];
  if (error) {
    NSLog(@"Error png file:%@ error=%@", saveFileName, [error description]);
    return NO;
  } else {
    NSLog(@"Success png file:%@", saveFileName);
    return YES;
  }
}

Objective C 添加到OS X状态托盘

@interface Tray : NSObject <NSApplicationDelegate> {
	NSStatusItem *trayItem;
}
@end

@implementation Tray

- (IBAction)testAction:(id)sender;
{
	NSLog(@"Hello World");
}

- (IBAction)quitAction:(id)sender;
{
	[NSApp terminate:sender];
}

- (void)applicationDidFinishLaunching:(NSNotification *)note;
{
	NSZone *zone = [NSMenu menuZone];
	NSMenu *menu = [[[NSMenu allocWithZone:zone] init] autorelease];
	NSMenuItem *item;
	
	item = [menu addItemWithTitle:@"Testing" action:@selector(testAction:) keyEquivalent:@""];
	[item setTarget:self];
	
	item = [menu addItemWithTitle:@"Quit" action:@selector(quitAction:) keyEquivalent:@""];
	[item setTarget:self];
	
	trayItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
	[trayItem setMenu:menu];
	[trayItem setHighlightMode:YES];
	[trayItem setTitle:@"HERE"];
}

- (void)dealloc;
{
	[trayItem release];
	[super dealloc];
}

@end

Objective C 将应用程序带到前面

[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];

Objective C 如何制作圆角图像

void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth, float ovalHeight);
{
	float fw, fh;
	if (ovalWidth == 0 || ovalHeight == 0) {
		CGContextAddRect(context, rect);
		return;
	}
	CGContextSaveGState(context);
	CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect));
	CGContextScaleCTM (context, ovalWidth, ovalHeight);
	fw = CGRectGetWidth (rect) / ovalWidth;
	fh = CGRectGetHeight (rect) / ovalHeight;
	CGContextMoveToPoint(context, fw, fh/2);
	CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);
	CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);
	CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);
	CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
	CGContextClosePath(context);
	CGContextRestoreGState(context);
}

- (UIImage *)roundCornersOfImage:(UIImage *)source;
{
	int w = source.size.width;
	int h = source.size.height;
	
	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
	CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
	
	CGContextBeginPath(context);
	CGRect rect = CGRectMake(0, 0, w, h);
	addRoundedRectToPath(context, rect, 5, 5);
	CGContextClosePath(context);
	CGContextClip(context);
	
	CGContextDrawImage(context, CGRectMake(0, 0, w, h), source.CGImage);
	
	CGImageRef imageMasked = CGBitmapContextCreateImage(context);
	CGContextRelease(context);
	CGColorSpaceRelease(colorSpace);
	
	return [UIImage imageWithCGImage:imageMasked];    
}