我有一个自定义的NSCell,里面有各种元素(图像,各种文本片段),其中一个文本块可能有各种可点击的链接。我的NSAttributedString可以正确地识别链接并将它们涂成蓝色,但是我不知道如何让光标变成手形,并允许用户真正点击它们。
现在,我已经将我的属性字符串直接绘制到单元格中,而这个单元格显然是不可点击的,但我不确定如何添加它,因为NSCell不是从NSView继承的。通常我只会添加一个NSTextField作为子视图,但在这种情况下我不能这样做。
有什么想法吗?
发布于 2009-09-09 08:24:41
我能想到的唯一解决方案是在你的NSCell中通过手动点击测试和鼠标跟踪。最难的部分(我没有答案)是如何确定链接文本的rect……希望有人能回答这个问题?
一旦知道了url文本的rect,就可以通过实现hitTestForEvent来实现单击操作。我想你会这样做的;
// If the event is a mouse down event and the point is inside the rect trigger the url
- (NSUInteger)hitTestForEvent:(NSEvent *)event inRect:(NSRect)frame ofView:(NSView *)controlView {
NSPoint point = [controlView convertPoint:[event locationInWindow] fromView:nil];
// Check that the point is over the url region
if (NSPointInRect(point, urlFrame)) {
// If event is mousedown activate url
// Insert code here to activate url
return NSCellHitTrackableArea;
} else {
return [super hitTestForEvent:event inRect:frame ofView:controlView];
}
}
发布于 2009-09-10 01:36:10
基于与Ira Cooke和其他人的讨论,我决定采用以下解决方案:
如果一切都按计划进行,那么我一次应该只有一个NSView连接到NSTableView,而且大多数时候根本就没有。一旦我让它正常工作,我会回来报告我的结果。
https://stackoverflow.com/questions/1397258
复制相似问题