I gave up on trying to find the magic formula of settings which would make my 
custom subclassed NSTextFieldCell return the correct expansion frame, as the 
cell which is returned by -[NSTableColumn dataCell] does.  Instead, I overrode 
expansionFrameWithFrame:inView:, replacing the multi-line frame returned by 
super with a single-line frame which I calculate myself.

It solves the same problem when used in NSTokenFieldCell subclasses too.

Thanks again for the tip, Corbin.

/*!
 @brief    Calculates the expansion frame which is used to display the
 entire text in a special expansion frame tool tip, if text is truncated.

 @details  This is a workaround for the fact that -[NSTableColumn dataCell],
 even though it claims to be an NSTextFieldCell, must have some magic built
 into it which allows the expansion frame tool tip to be calculated
 correctly regardless of -wraps, -isScrollable, and -truncatesLastVisibleLine.
 The [[NSTextFieldCell alloc] init] used to draw the text in this class
 does not have that magic, so we calculate it from scratch, based on the
 current attributed string value.
*/
- (NSRect)expansionFrameWithFrame:(NSRect)frame
                           inView:(NSView*)view {
    NSRect expansionFrame = [super expansionFrameWithFrame:frame
                                           inView:view] ;
    // Since we are not using a magic NSTextFieldCell, the
    // expansionFrame at this point may be no wider than the
    // cell, and will be high enough to display the text if
    // it were wrapped to multiple lines, which it does not.
    // (You can get the correct rect if you setWraps:NO, but
    // then the ellipsis does not show when drawing normally.)
    // So we discard the size and use only the origin.

    // Super does correctly return NSZeroRect if the text
    // is not currently truncated, so we use that and do same.
    if (
        (expansionFrame.size.width == 0.0)
        &&
        (expansionFrame.size.height == 0.0)
        ) {
        return NSZeroRect ;
    }
    
    NSAttributedString* as = [self attributedStringValue] ;
    CGFloat height = frame.size.height ;
    CGFloat width = [as widthForHeight:height] ;
    expansionFrame.size.width = width ;
    expansionFrame.size.height = height ;
    return expansionFrame ;
}



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to