Objective c 如何在NSTableView中创建可单击的url

Objective c 如何在NSTableView中创建可单击的url,objective-c,nstableview,Objective C,Nstableview,我正在尝试构建基于OS-X核心数据的应用程序。在其中一个实体中,我存储了一个URL(www.somesite.com/somepage/someindex.php) 使用绑定,我成功地在NSTableView中显示URL。然而,我希望URL是可点击的,当点击时,浏览器会启动并打开页面。我做了一些研究,找到了一些解决方案,例如: 此外: 但它们看起来都过时了,第一个已经6年了,而第二个最后更新是在2005年1月 任何人都可以提供更简单更快的方法如何实现这一点?我没想到我会写一堆代码,只是为了让

我正在尝试构建基于OS-X核心数据的应用程序。在其中一个实体中,我存储了一个URL(www.somesite.com/somepage/someindex.php)

使用绑定,我成功地在
NSTableView
中显示URL。然而,我希望URL是可点击的,当点击时,浏览器会启动并打开页面。我做了一些研究,找到了一些解决方案,例如:

此外:

但它们看起来都过时了,第一个已经6年了,而第二个最后更新是在2005年1月

任何人都可以提供更简单更快的方法如何实现这一点?我没想到我会写一堆代码,只是为了让简单的链接工作,说实话。。。我来自web开发世界,在那里,这些事情可以在几秒钟内解决,而这里似乎是完全不同的故事

任何帮助都将不胜感激


John

您可以在tableviewcell中使用。它支持强大的链接检测。

您可以在tableviewcell中使用。它支持强大的链接检测。

您可以使用
NSTextView
并实现其委托。有一个演示:

// MyCellView.h
@interface MyCellView : NSView

@property (nonatomic, strong) IBOutlet NSTextView *textView;

@end

// ViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    NSNib *nib = [[NSNib alloc] initWithNibNamed:@"MyCellView" bundle:[NSBundle mainBundle]];
    [self.tableView registerNib:nib forIdentifier:@"MyCell"];
}

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    MyCellView *cell = (MyCellView *)[tableView makeViewWithIdentifier:@"MyCell" owner:self];

    cell.textView.delegate = self;
    [cell.textView.textStorage setAttributedString:[self makeLinkAttributedString:@"This is a test: www.somesite.com/somepage/someindex.php"]];

    return cell;
}

- (NSAttributedString *)makeLinkAttributedString:(NSString *)string {
    NSMutableAttributedString *linkedString = [[NSMutableAttributedString alloc] initWithString:string];

    NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
    [detector enumerateMatchesInString:string options:0 range:NSMakeRange(0, string.length) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
        if (match.URL) {
            NSDictionary *attributes = @{ NSLinkAttributeName: match.URL };
            [linkedString addAttributes:attributes range:match.range];
        }
    }];

    return [linkedString copy];
}

#pragma mark - NSTextViewDelegate methods
- (BOOL)textView:(NSTextView *)textView clickedOnLink:(id)link atIndex:(NSUInteger)charIndex {
    // The click will be handled by you or the next responder.
    return NO;
}

您可以使用
NSTextView
并实现其委托。有一个演示:

// MyCellView.h
@interface MyCellView : NSView

@property (nonatomic, strong) IBOutlet NSTextView *textView;

@end

// ViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    NSNib *nib = [[NSNib alloc] initWithNibNamed:@"MyCellView" bundle:[NSBundle mainBundle]];
    [self.tableView registerNib:nib forIdentifier:@"MyCell"];
}

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    MyCellView *cell = (MyCellView *)[tableView makeViewWithIdentifier:@"MyCell" owner:self];

    cell.textView.delegate = self;
    [cell.textView.textStorage setAttributedString:[self makeLinkAttributedString:@"This is a test: www.somesite.com/somepage/someindex.php"]];

    return cell;
}

- (NSAttributedString *)makeLinkAttributedString:(NSString *)string {
    NSMutableAttributedString *linkedString = [[NSMutableAttributedString alloc] initWithString:string];

    NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
    [detector enumerateMatchesInString:string options:0 range:NSMakeRange(0, string.length) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
        if (match.URL) {
            NSDictionary *attributes = @{ NSLinkAttributeName: match.URL };
            [linkedString addAttributes:attributes range:match.range];
        }
    }];

    return [linkedString copy];
}

#pragma mark - NSTextViewDelegate methods
- (BOOL)textView:(NSTextView *)textView clickedOnLink:(id)link atIndex:(NSUInteger)charIndex {
    // The click will be handled by you or the next responder.
    return NO;
}

我一定会看一看,即使我更喜欢本机解决方案(如果有)…无论如何谢谢。我一定会看一看,即使我更喜欢本机解决方案(如果有)…无论如何谢谢。
UITextView
已经在
dataDetectorTypes
属性中为链接提供了本机数据检测器,您的代码中不需要使用
makeLinkAttributedString
方法手动检测。您是对的,但是
setAutomaticLinkDetectionEnabled:
仅在NSTextView可编辑时工作。嗯,我想它是在只读模式下工作,而不是在可编辑模式下工作?你确定吗?是的,我想是的,我看到了:这家伙绝对错了,当然。我在IB中打开它一千次,它在不可编辑时工作良好,在可编辑时不工作。在SO中搜索表明人们经历了相同的行为。
UITextView
已经在
dataDetectorTypes
属性中为链接设置了本机数据检测器,不需要在代码中使用
makeLinkAttributedString
方法手动检测。您是对的,但是
setAutomaticClinkDetectionEnabled:
仅在NSTextView可编辑时工作。嗯,我以为它是在只读模式下工作,而不是在可编辑模式下工作?你确定吗?是的,我想是的,我看到了:这家伙绝对错了,当然。我在IB中打开它一千次,它在不可编辑时工作良好,在可编辑时不工作。搜索SO表明人们经历了相同的行为。