gpt4 book ai didi

objective-c - NSTextField 在浏览器中打开本地html

转载 作者:行者123 更新时间:2023-12-03 17:11:24 25 4
gpt4 key购买 nike

我需要在 cocoa 中创建一个可点击的文本(超链接文本)。单击后,它应该在浏览器中打开内容,而不是在 Finder 中打开。我怎样才能完成它?

这是我正在尝试的代码

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",@"file:///Users/abc/index.html"]];

NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString: @"Help"];

NSRange range = NSMakeRange(0, [attrString length]);
[attrString beginEditing];

[attrString addAttribute:NSLinkAttributeName value:[url absoluteString] range:range];

// make the text appear in blue
[attrString addAttribute:NSForegroundColorAttributeName value:[NSColor blueColor] range:range];

// make the text appear with an underline
[attrString addAttribute:
NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSSingleUnderlineStyle] range:range];

[attrString endEditing];

提前致谢......

最佳答案

您可以子类 NSTextField 并重写 mouseDown 方法。

-(void)mouseDown:(NSEvent *)theEvent
{
[super mouseDown:theEvent];
NSLog(@"Here"); // Delegate action back to View Controller.
}

然后就可以监听文本上的点击事件了。您甚至可以在 resetCursorRects 中更改光标以获取 pointingHand。

-(void)resetCursorRects
{
[self addCursorRect:[self bounds] cursor:[NSCursor pointingHandCursor]];
}

关于objective-c - NSTextField 在浏览器中打开本地html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25261459/

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