I needed to create a NSButton with both image and title but I didn't like any of the standard positioning methods in cocoa.
I decided to subclass the button cell and override the -imageRectForBounds:
and -titleRectForBounds:
to provide my custom positions. The problem is that the -titleRectForBounds:
method gets called normally but the -imageRectForBounds:
is not.
The images in the buttons are shown normally so the cell must have a frame to draw the images I just don't know where it gets it from.
The code is really simple. At the moment the only thing I did is to subclass NSButtonCell and override those two methods. Then in IB I choose an NSButton and change its cell class to my custom button cell.
Here is the code:
#import "JSButtonCell.h"
@implementation JSButtonCell
- (NSRect)titleRectForBounds:(NSRect)theRect
{
NSLog(@"Bounds for title");
NSLog(@"%@",NSStringFromRect(theRect));
NSRect titleRect = [super titleRectForBounds:theRect];
NSLog(@"Title rect");
NSLog(@"%@",NSStringFromRect(titleRect));
return titleRect;
}
- (NSRect)imageRectForBounds:(NSRect)theRect
{
NSLog(@"Bounds for image");
NSLog(@"%@",NSStringFromRect(theRect));
NSRect imageRect = [super imageRectForBounds:theRect];
NSLog(@"Image rect");
NSLog(@"%@",NSStringFromRect(imageRect));
imageRect.origin.y -= 20;
return imageRect;
}
@end