4

我想在属性字符串的一部分上显示工具提示,并认为在所需范围上使用 NSToolTipAttributeName 属性可以解决问题,但我无法让它工作。我正在尝试使用 MACOS、Xcode (9.2)

NSString *fullString = @"Create a new tooltip";

NSString *compareString =@"tooltip";

NSRange tooltipRange = [fullString rangeOfString: compareString]; 

[senderTextView.textStorage addAttribute:NSToolTipAttributeName
                        value:@"Custom tooltip"
                        range:tooltipRange];

[senderTextView.textStorage addAttribute:NSForegroundColorAttributeName
     value:[NSColor redColor] range:tooltipRange];

单词“工具提示”按预期显示为红色,但是当我将鼠标悬停在它上面时没有工具提示出现。我错过了什么?

4

1 回答 1

0

有几种方法可以做到这一点,将 与NSToolTipAttributeName结合使用NSLinkAttributeName,例如:

NSMutableDictionary* labelAttributes = [NSMutableDictionary new];
labelAttributes[NSToolTipAttributeName] = @"Tooltip";
labelAttributes[NSLinkAttributeName] = @"Link";
NSAttributedString* labelString = [NSAttributedString
    attributedString:@"Display"]
    withAttributes:labelAttributes];

小心别忘displaysLinkToolTips = YES了上NSTextView

或者,您可以使用NSView Tooltips API,它提供:

- (NSToolTipTag)addToolTipRect:(NSRect)rect owner:(id)owner userData:(void *)data;
于 2018-04-07T22:43:16.607 回答