NSTableView内NSTextFieldCell中的可点击网址链接? [英] Clickable url link in NSTextFieldCell inside NSTableView?

查看:208
本文介绍了NSTableView内NSTextFieldCell中的可点击网址链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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

我觉得我缺少某些设置。

解决方案

我认为技术说明无法回答问题,即如何在NSTableView单元格中放置链接。我发现最好的方法是为表格单元格使用一个按钮单元格。假定只有链接将在表的特定列中。



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

p>

在您的表视图委托中,实现tableView:dataCellForTableColumn:row:如下:

 -(NSCell *)tableView:(NSTableView *)tableView 
dataCellForTableColumn:(NSTableColumn *)列
行:(NSInteger)行
{
NSButtonCell * buttonCell = nil ;
NSAttributedString * title =无;
NSString * link = nil;
NSDictionary *属性=无;

//整行的单元格-如果(column == nil)
return(nil);我们不做标题


//除了link以外的其他列都可以正常执行
if(![self isLinkColumn:column])//根据您的表实现此功能
return([column dataCellForRow :行]);

//如果没有链接,没有按钮,则只是一个空白文本字段
if((link = [self linkForRow:row])!= nil)//根据您的需要实施此操作表
return([[[[NSTextFieldCell alloc] initTextCell:@] autorelease]);

//这是一个链接。创建标题
属性= [[NSDictionary分配] initWithObjectsAndKeys:
[NSFont systemFontOfSize:[NSFont systemFontSize]],NSFontAttributeName,
[NSNumber numberWithInt:NSUnderlineStyleSingle],NSUnderlineStyleAttributeName,
NSColor blueColor],NSForegroundColorAttributeName,
[NSURL URLWithString:link],NSLinkAttributeName,nil];
title = [[NSAttributedString alloc] initWithString:link properties:attributes];
[属性发布];

//创建一个按钮单元
buttonCell = [[[NSButtonCell alloc] init] autorelease];
[buttonCell setBezelStyle:NSRoundedBezelStyle];
[buttonCell setButtonType:NSMomentaryPushInButton];
[buttonCell setBordered:NO]; //不需要带边框的按钮
[buttonCell setAttributedTitle:title];
[标题发布];
return(buttonCell);
}

将表的目标/操作设置为委托人并检查是否单击链接列:

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

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

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


I have a NSAttributedString that I'm using in a NSTextFieldCell. It makes several clickable url links and puts a big NSAttributedString inside the NSTextFieldCell. Whenever I am viewing the NSTextFieldCell normally and it's highlighted, I cannot click on the links.

If I set the TableView so I can edit each column or row, when I click twice, go into Edit mode and view the NSTextFieldCell contents, my links show up and are clickable. When I click away from the row, I can no longer see clickable links.

I have to be in "edit" mode to see the links or click on them.

I feel like there's some setting I'm just missing.

解决方案

I don't think the tech note answers the question, which was how to put a link in an NSTableView cell. The best way I've found to do this is to use a button cell for the table cell. This assumes that only links will be in a particular column of the table.

In Interface Builder, drag an NSButton cell onto the table column where you want the links.

In your table view delegate, implement tableView:dataCellForTableColumn:row: as follows:

- (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);
}

Set the target/action for the table to your delegate and check for clicks on the link column:

- (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]];
}

Now the link looks like a link, but a click on it is actually a button press, which you detect in the action method and dispatch using NSWorkspace.

这篇关于NSTableView内NSTextFieldCell中的可点击网址链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆