Tuesday, September 15, 2009

How do you rotate an NSButton, NSTextField, or NSView?

How do you rotate an NSButton, NSTextField, or NSView?

First, you can do it and it is fairly easy to do! Here is what it will look like!



There are two NSView methods available to do this.

In OS 10.0 you have:

- (void)setFrameRotation:(CGFloat)angle

And in OS 10.5 you have:

- (void)setFrameCenterRotation:(CGFloat)angle

For some the above information will be sufficient. For those desiring more information please continue to read this posting.

Create a new "Cocoa Application". From the File menu choose "New Project" and then select "Cocoa Application".
















Name and save the project whatever you want. I named my project "RotatedControl".

In XCode add to the "Classes" folder a "New File". Choose "Objective-C class".

I named my class "Controller"















Add your IBOutlets to your new class. Here is my code:

//
// Controller.h
// RotatedControl
//
// Created by Geoffrey_Slinker on 9/13/09.
// Copyright 2009 Area 51. All rights reserved.
//

#import


@interface Controller : NSObject {
IBOutlet NSTextField *textField;
IBOutlet NSButton *button;
}

@property (retain) NSTextField *textField;
@property (retain) NSButton *button;

- (void) awakeFromNib;

@end

//
// Controller.m
// RotatedControl
//
// Created by Geoffrey_Slinker on 9/13/09.
// Copyright 2009 Area 51. All rights reserved.
//

#import "Controller.h"


@implementation Controller

@synthesize textField;
@synthesize button;

- (void) awakeFromNib
{
//[textField setFrameRotation:45.0];
//[button setFrameRotation:30.0];

[textField setFrameCenterRotation:45.0];
[button setFrameCenterRotation:-45.0];


}

@end


Now open the NIB file. In XCode double click "MainMenu.xib" found in the folder "NIB Files". This should launch Interface Builder (IB).

Add an "Object Controller" and set its class to be the class type you created above. In my case I set it to "Controller". See "Where is the Classes Tab" for detailed information.

Still in IB add a Button and a Text Field to your Window. Just select them from the Library of Objects and drag them onto the Window.



Now "hook up" the IBOutlets of the Controller to the controls on the window.


You can see I have "wired" or "connected" the button outlet to the button on the window. The same was done for the text field.

Also wire the the "Referencing Outlets" delegate to the "Application". The "Application is above the "Controller" in the MainMenu.xib view.



If I haven't missed any steps then from XCode just build and run the application.



2 comments:

Anonymous said...

Nice post and this fill someone in on helped me alot in my college assignement. Thank you as your information.

ThE uSeFuL said...

I have the same problem as this guy here,

http://www.xydo.com/articles/29474456-animated_rotation_of_nsview_contents


Any solution?