gpt4 book ai didi

cocoa - NSTableView 内 NSTextFieldCell 中的可点击 url 链接?

转载 作者:行者123 更新时间:2023-12-03 16:09:46 24 4
gpt4 key购买 nike

我有一个在 NSTextFieldCell 中使用的 NSAttributedString。它创建了几个可点击的 url 链接,并将一个大的 NSAttributedString 放入 NSTextFieldCell 中。每当我正常查看 NSTextFieldCell 并且它突出显示时,我都无法单击链接。

如果我设置 TableView 以便可以编辑每一列或行,则当我单击两次时,进入编辑模式并查看 NSTextFieldCell 内容,我的链接将显示并且可单击。当我点击离开该行时,我再也看不到可点击的链接。

我必须处于“编辑”模式才能查看链接或单击它们。

我觉得我缺少一些设置。

最佳答案

我认为技术说明没有回答这个问题,即如何在 NSTableView 单元格中放置链接。我发现执行此操作的最佳方法是使用按钮单元格作为表格单元格。这假设只有链接位于表的特定列中。

在 Interface Builder 中,将 NSButton 单元格拖到您想要链接的表格列上。

在 TableView 委托(delegate)中,实现 tableView:dataCellForTableColumn:row: 如下:

- (NSCell *) tableView: (NSTableView *) tableView
dataCellForTableColumn: (NSTableColumn *) column
row: (NSInteger) row
{
NSButtonCell *buttonCell = nil;
NSAttributedString *title = nil;
NSString *link = nil;
NSDictionary *attributes = nil;

// Cell for entire row -- we don't do headers
if (column == nil)
return(nil);

// Columns other than link do the normal thing
if (![self isLinkColumn:column]) // Implement this as appropriate for your table
return([column dataCellForRow:row]);

// If no link, no button, just a blank text field
if ((link = [self linkForRow:row]) != nil) // Implement this as appropriate for your table
return([[[NSTextFieldCell alloc] initTextCell:@""] autorelease]);

// It's a link. Create the title
attributes = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSFont systemFontOfSize:[NSFont systemFontSize]], NSFontAttributeName,
[NSNumber numberWithInt:NSUnderlineStyleSingle], NSUnderlineStyleAttributeName,
[NSColor blueColor], NSForegroundColorAttributeName,
[NSURL URLWithString:link], NSLinkAttributeName, nil];
title = [[NSAttributedString alloc] initWithString:link attributes:attributes];
[attributes release];

// Create a button cell
buttonCell = [[[NSButtonCell alloc] init] autorelease];
[buttonCell setBezelStyle:NSRoundedBezelStyle];
[buttonCell setButtonType:NSMomentaryPushInButton];
[buttonCell setBordered:NO]; // Don't want a bordered button
[buttonCell setAttributedTitle:title];
[title release];
return(buttonCell);
}

将表的目标/操作设置为您的委托(delegate)并检查链接列上的点击:

- (void) clickTable: (NSTableView *) sender
{
NSTableColumn *column = [[sender tableColumns] objectAtIndex:[sender clickedColumn]];
NSInteger row = [sender clickedRow];
NSString *link = nil;

if ([self isLinkColumn:column] && (link = [self linkForRow:row]) != nil)
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:link]];
}

现在,链接看起来像一个链接,但单击它实际上是按下按钮,您可以在操作方法中检测到该按钮并使用 NSWorkspace 进行调度。

关于cocoa - NSTableView 内 NSTextFieldCell 中的可点击 url 链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/765047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com