我有一个自定义的NSCell,其中包含各种元素(图像,各种文本片段),其中一个文本块可能有不同的可点击链接.我有我的NSAttributedString正确识别链接并将它们着色为蓝色但是我无法弄清楚如何让光标变成一只手并允许用户实际点击它们.
现在我将我的属性字符串绘制到单元格,这显然是不可点击的,但我不知道如何以任何其他方式添加它,因为NSCell不从NSView继承.通常我只是添加一个NSTextField作为子视图,但在这种情况下我不能这样做.
有什么想法吗?
解决方法
我能想到的唯一解决方案是通过NSCell内的手动命中测试和鼠标跟踪.最难的部分(我没有答案)是如何确定链接文本的矩形…希望有人可以回答这个问题?
一旦知道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]; } }