Can NSTabViewItem button labels be resized?

2019-08-07 10:05发布

问题:

If I add a number of NSTabViewItems to an NSTabViewController, the tab view item buttons are sized according to the length of the label text. With different texts for each label this can result in NSTabViewItem buttons that have vastly different sizes.

Is there a way to set the button sizes/widths to a specific size in IB? Can it be done dynamically in code?

回答1:

I don't think this can be done in Interface Builder, but you should be able to subclass NSTabViewItem and override - (NSSize)sizeOfLabel:(BOOL)computeMin in order to return desired value. More info here.

For instance, if you want all NSTabViewItem of same 100 pt width:

@interface MyTabViewItem : NSTabViewItem
@end

@implementation MyTabViewItem
- (NSSize)sizeOfLabel:(BOOL)computeMin {

    NSSize size = [super sizeOfLabel:computeMin];
    size.width = 100.0
    return size;

}
@end

and, of course, you'll need to set your NSTabView's items to be instance of MyTabViewItem (either in IB or when dynamically adding them in your code).