Recommended books

How to make a clickable link inside a NSTextField and Cocoa

Objective-C posted about 1 year ago by christian

From How do I embed a hyperlink inside an NSTextField or NSTextView?

NSAttributedString+Hyperlink.h

   1  #import <Cocoa/Cocoa.h>
   2  
   3  @interface NSAttributedString (Hyperlink)
   4  +(id)hyperlinkFromString:(NSString*)inString withURL:(NSURL*)aURL;
   5  @end
   6  

NSAttributedString+Hyperlink.m:

   1  #import "NSAttributedString+Hyperlink.h"
   2  
   3  
   4  @implementation NSAttributedString (Hyperlink)
   5  +(id)hyperlinkFromString:(NSString*)inString withURL:(NSURL*)aURL
   6  {
   7      NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString: inString];
   8      NSRange range = NSMakeRange(0, [attrString length]);
   9  	
  10      [attrString beginEditing];
  11      [attrString addAttribute:NSLinkAttributeName value:[aURL absoluteString] range:range];
  12  	
  13      // make the text appear in blue
  14      [attrString addAttribute:NSForegroundColorAttributeName value:[NSColor blueColor] range:range];
  15  	
  16      // next make the text appear with an underline
  17      [attrString addAttribute:
  18  	 NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSSingleUnderlineStyle] range:range];
  19  	
  20      [attrString endEditing];
  21  	
  22      return [attrString autorelease];
  23  }
  24  @end
  25  

This code sets the contents of the NSTextField to a URL:

   1  #import "NSAttributedString+Hyperlink.h"
   2  
   3  [textURL setAllowsEditingTextAttributes: YES];
   4  [textURL setSelectable: YES];
   5  	
   6  NSURL* url = [NSURL URLWithString:@"http://www.apple.com"];
   7  	
   8  NSMutableAttributedString* string = [[NSMutableAttributedString alloc] init];
   9  [string appendAttributedString: [NSAttributedString hyperlinkFromString:@"Apple Computer" withURL:url]];
  10  	
  11  [textURL setAttributedStringValue: string];

Tagged clickable, url, nstextfield, cocoa

Showing and hiding an NSWindow programatically

Objective-C posted about 1 year ago by christian

   1  // Show
   2  [myWindow makeKeyAndOrderFront:self];
   3  [NSApp activateIgnoringOtherApps:YES];
   4  // Hide
   5  [myWindow orderOut:self]; 

Tagged show, hide, window, nswindow

How to scroll images with NSImageView and NSScrollView

Objective-C posted about 1 year ago by christian
  1. In IB (Interface Builder) add an NSImageView to the window.
  2. Select the NSImageView.
  3. From the menu select Select Layout → Embed Objects In → Scroll View.
  4. Select the NSScrollView press Cmd+3 and configure the NSScrollView to expand and fill all available space.
  5. Remove the NSImageView’s border
  6. Use this code to resize the NSImageView’s frame to match the size of the NSImage

   1  NSImageView *imageView = ...;
   2  [imageView setFrameSize:viewSize];
   3  
   4  NSLog(@"imageView's frame after resizing: %@", NSStringFromRect([imageView frame]));

Tagged cocoa, objective-c, autoresize, nsscrollview, nsimageview, scroll

How to store user settings with NSUserDefaults

Objective-C posted about 1 year ago by christian

Storing preferences

   1  NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
   2  [preferences setObject:@"SOME_DATA" forKey:@"KEY"];

Retrieving preferences

   1  [preferences stringForKey:@"KEY"];
   2  [preferences integerForKey:@"KEY"];
   3  [preferences booleanForKey:@"KEY"];

Listing preferences with the defaults command

To list all preferences for a specific app, use this command:

   1  $ defaults read com.aktagon.XXXApp

References

http://developer.apple.com/documentation/Cocoa/Conceptual/UserDefaults/UserDefaults.html

Tagged nsuserdefaults, preferences, settings, objective-c

How to implement callbacks in Objective-C by passing methods as parameters

Objective-C posted about 1 year ago by christian

Example of a method that takes a callback method, defined as theSelector, as a parameter:

   1  - (void) getURL:(NSString *)theURL theSelector:(SEL)theSelector
   2  {
   3  	NSURL *url = [NSURL URLWithString:theURL];
   4  		
   5  	ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
   6  	
   7  	[request setDelegate:self];
   8  	[request setDidFinishSelector:theSelector];
   9  	[request setDidFailSelector:@selector(onError:)];
  10  	
  11         [networkQueue addOperation:request];
  12  	[networkQueue go];
  13  }

   1  // Don't forget the semicolon
   2  SEL sel = @selector(onResponseReceived:);
   3  [self getURL:@"http://www.google.com" selector:sel];

   1  - (void)onResponseReceived:(ASIHTTPRequest *)request {
   2  }

Tagged method, callback, selector