Category Archives: Cocoa Application

Cocoa OSX NSTextField Select All Text On Focus

Hello,

Recently I was working on MAC application where there was a requirement to select text inside NSTextField as soon as user focus it. In this blog I am going to explain how to do this.

First add a class to your project and name is CustomTextField. It will create two files CustomTextField.h and CustomTextField.m

Open CustomTextField.h file and add following code to it.

#import
@interface CustomTextField : NSTextField

@end

Now open CustomTextField.m file and add following code to it.

#import “CustomTextField.h”
@implementation CustomTextField
– (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    
    // Drawing code here.
}
– (BOOL)becomeFirstResponder
{
    BOOL result = [super becomeFirstResponder];
    if(result){
        [self performSelector:@selector(selectText:) withObject:self afterDelay:0];
    }
    return result;
}
@end

As you can see we have added event handler for becomeFirstResponder event. This event is fired as soon as user focus the text field. In that function we are selecting text by calling selector selectText with zero second delay. This will immediately select the text as soon as user focus on it. Now go to your Storyboard or xib file and select the TextField in which you want this behavior. 

Go to identity inspector and select the class CustomTextField for your textfield.
That’s it.You can also do additional stuff like changing background color on focus.
[self setBackgroundColor:[NSColor clearColor]];
[self setBackgroundColor:[NSColor redColor]];

Hope this helps you. 

Cocoa OSX NSTextField Allow Only Integer Value

Hello,

Recently I was working on MAC OSX application where we have a view with some textfields. Where in few textfields where only numeric values are allowed. In this blog I will explain how to do this.

I used NSNumberFormatter for that. First you have to create a class which extends NSNumberFormatter.

Go to your .m file and add new interface.

@interface OnlyIntegerValueFormatter : NSNumberFormatter

@end

Now implement this interface in same file.

@implementation OnlyIntegerValueFormatter
– (BOOL)isPartialStringValid:(NSString*)partialString newEditingString:(NSString**)newString errorDescription:(NSString**)error
{
    if([partialString length] == 0) {
        return YES;
    }
    
    NSScanner* scanner = [NSScanner scannerWithString:partialString];
    
    if(!([scanner scanInt:0] && [scanner isAtEnd])) {
        NSBeep();
        return NO;
    }
    
    return YES;
}
@end

That’s it. Now create instance of OnlyIntegerValueFormatter and assign it to NSTextField. 
OnlyIntegerValueFormatter *formatter = [[OnlyIntegerValueFormatter alloc] init];
[self.onlyIntegerTextField setFormatter:formatter];

That’s it. Now if you try to type characters in the textfield, it won’t allow it. 

Create Simple MAC OSX application with WebView and Load URL in it

Hello,

In this blog post we will see how to create a simple MAC OSX application with webview, which loads a URL in webview. This types of application are very useful when you have nicely designed web application and you want to convert it to MAC OSX app. Using this approach, you don’t have to create separate OSX application. To create this application , you will need Xcode. Open the Xcode and select “Create New xCode project”. Select OS X -> Application – > Cocoa Application. It will show following window.

Enter product name, Organization Name, Company Identifier, Class Prefix of your choice and click on Next. It will ask for the location to save the project. Select the location and create project. Once the project is created, you will see following type of file stricture in Xcode.
It may be different for in you based on entered class prefix by you. After this open AppDelegate.h file. Add following line after line #import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
After this add following line after @property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet WebView *webView;

Once done open MainMenu.xib file. Select window as displayed in following image and add webview as show in the following image.

After that we have to add IBOutlet for that Select App delegate from the right pane and Control + Drag from it to Webview. It will show context menu with Webview. Select the webview and it will add IBOutlet. See the following image.
Now we have to load URL in Webview. Open your AppDelegate.m file and find the applicationDidFinishLaunching function and add following code to it.
NSRect frame=[NSScreen mainScreen].frame ;
[self.window setFrame:frame display:YES animate:YES];

Above lines will open the window in fullscreen mode. 

NSURL *url = [NSURL URLWithString:@”http://davehiren.blogspot.com”];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[[[self webView] mainFrame] loadRequest:urlRequest];
[self.window setContentView:self.webView];

Above lines will load the URL in webview. You can also set title of window with

[self.window setTitle:@”title”];

When you run the app you should see following screen.
That’s it, your application is ready. You can change the URL the way you want.