1

I need to subclass NSSliderCell to customise its appearance. I had no problem with the bar, I overrode

- (void)drawBarInside:(NSRect)aRect flipped:(BOOL)flipped

and it works as expected.

- (void)drawKnob:(NSRect)knobRect

works as well, but I want a smaller knob, and resizing the NSRect isn't an option, because I have an unwanted space either on the right or on the left (or both, if I center my custom knob).

Reading the documentation, I should override

- (void)drawKnob

to calculate the rect in which the knob should be drawn, then call drawKnob: The problem is that, trying to get the cellSize to compute where I should draw the knob, I get an absurd width : 40000

How can I get the right width? NSSliderCell is a subclass of NSCell, so I can't access frame/bounds.

Alessandro
  • 61
  • 1
  • 8

1 Answers1

3

I actually managed to customise NSSlider knob appearance adding a width property to NSSliderCell and overriding - (NSRect)knobRectFlipped:(BOOL)flipped this way:

- (NSRect)knobRectFlipped:(BOOL)flipped {
CGFloat knobCenterPosition = roundf(size.width * self.floatValue / self.maxValue);
// knob should always be entirely visible
knobCenterPosition = MIN(MAX(knobCenterPosition, roundf(KNOB_WIDTH / 2) + KNOB_INSET), size.width - (roundf(KNOB_WIDTH / 2) + KNOB_INSET));
return  NSMakeRect(knobCenterPosition - roundf(KNOB_WIDTH / 2), 0, KNOB_WIDTH, self.cellSize.height);

}

I need to change knobCenterPosition when the knob would overlap the right or left border of the slider (KNOB_WIDTH and KNOB_INSET are self explaining macros).

Alessandro
  • 61
  • 1
  • 8
  • 1
    @rene after reading [this post](http://meta.stackexchange.com/questions/17845/etiquette-for-answering-your-own-question) I decided to post an answer. Was I wrong? – Alessandro Jan 03 '14 at 13:20
  • I couldn't clearly make out if this was an actual answer or a new update to your question. If this is an answer that is perfectly fine. – rene Jan 03 '14 at 13:27