简体   繁体   中英

Tooltip for view based NSTableView

I have a tooltip:

cellView.textField.toolTip = cellView.textField.stringValue;

It shows always, but I need to show tooltip only if text clipped... How I can achieve it?

Set allowsExpansionToolTips property to YES for Table View Cell in xib.

在此处输入图片说明

I think you can do this by implementing the text field delegate method, controlTextDidEndEditing, and checking the size of the text. I found that the size returned by sizeWithAttributes: didn't match the size I would have expected when I filled the text field, so I just determined the value I needed for my if statement empirically (in this example I had the text field's value bound to a property, theText).

-(void)controlTextDidEndEditing:(NSNotification *)obj {
    NSLog(@"%@",NSStringFromRect([obj.object frame]));
    NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSFont systemFontOfSize:13] forKey:@"NSFontAttributeName"];
    NSSize size = [theText sizeWithAttributes:dict];
    NSLog(@"%@",NSStringFromSize(size));
    if (size.width >69) {
        [obj.object setToolTip:theText];
    }
}

Try: allowsExpansionToolTips (in Swift , Obj-C )

Expansion tooltips are shown when the cell cannot show the full content and the user hovers the pointer over the control.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM