Search code examples
macoscocoanstokenfield

Measure string width on NSTokenField


I need to measure a string width to make space (vertically) if more than the horizontal available space is needed. I can do this on a NSTextField, but on a NSTokenField ?


Solution

  • Here is an answer I found elsewhere:

    - (void)resizeTokenField;
    {
        NSRect oldTokenFieldFrame = [self.tokenField frame];
        NSSize cellSize= [[self.tokenField cell] cellSizeForBounds:[self.tokenField
                                                               bounds]];
    
    
            [self.tokenField setFrame:NSMakeRect(
                                            oldTokenFieldFrame.origin.x,
                                            oldTokenFieldFrame.origin.y+ oldTokenFieldFrame.size.height-
                                            cellSize.height,
                                            oldTokenFieldFrame.size.width,
                                            cellSize.height)];
    
    }