我正在跟进THIS https://developer.apple.com/library/mac/qa/qa1487/_index.html来自苹果的指南,但它并没有真正正常工作。
基本上,我试图通过自定义 WindowController 类向窗口内的 NSTextField 添加超链接。我可以让超链接解决一些问题:
- 当我将鼠标悬停在超链接上时,我会看到一个“I bean”(指示您可以选择文本的光标)。我想要一只通常出现在超链接上方的手
- 当我单击超链接文本时,它会成功在浏览器中打开链接,但随后它会更改文本大小和格式(例如,它不再居中,回到某些默认值)。现在,当我将鼠标悬停在它上面时,我得到了手。
经过一番实验,我发现初始字符串格式(例如,单击之前的大小、字体)是我在其中创建标签的 .xib 文件的格式。单击后,它会更改为某种默认字体我似乎无法以任何方式影响。不过,超链接仍然存在。
这是一些代码:
关于WindowController.h
#import "AboutWindowController.h"
@interface AboutWindowController ()
@end
@implementation AboutWindowController
- (id)initWithWindow:(NSWindow *)window
{
self = [super initWithWindow:window];
if (self) {
// Initialization code here.
}
return self;
}
- (void)windowDidLoad
{
[super windowDidLoad];
[self.testLabel setAllowsEditingTextAttributes:YES];
[self.testLabel setSelectable:YES];
NSMutableAttributedString* string1 = [[NSMutableAttributedString alloc] init];
NSString* inString = @"Apple Computer";
NSURL* aURL = [NSURL URLWithString:@"www.google.com"];
NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString: inString];
NSRange range = NSMakeRange(0, [attrString length]);
[attrString beginEditing];
[attrString addAttribute:NSLinkAttributeName value:[aURL absoluteString] range:range];
// make the text appear in blue
[attrString addAttribute:NSForegroundColorAttributeName value:[NSColor blueColor] range:range];
// next make the text appear with an underline
[attrString addAttribute:
NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSSingleUnderlineStyle] range:range];
[attrString endEditing];
[string1 appendAttributedString: attrString];
[self.testLabel setAttributedStringValue:string1];
[self.testLabel setFont:[NSFont fontWithName:@"Helvetica" size:20]];
}
@end
关于WindowController.h
#import <Cocoa/Cocoa.h>
@interface AboutWindowController : NSWindowController
@property (weak) IBOutlet NSTextField *testLabel;
@end
.xib 非常简单:它是一个带有标签的窗口,我将标签正确链接到 .h 文件(我相信)
谢谢您的帮助。我会尝试定期回来回答任何问题/澄清。编辑:请检查我对 bikram 答案的评论以了解我的情况的更新。
你遇到的问题是NSMutableAttributedString
正在尝试强制其格式化并且NSTextField
是在逼迫自己。
我的主 XIB 只有菜单,而我的 windowController XIB 有一个Label NSTextField
.
窗口控制器:
@interface TFTWindowController : NSWindowController
@property (weak) IBOutlet NSTextField *testLabel;
@end
@implementation TFTWindowController
- (id)initWithWindow:(NSWindow *)window
{
self = [super initWithWindow:window];
if (self) {
// Initialization code here.
}
return self;
}
- (void)awakeFromNib {
}
- (void)windowDidLoad
{
[super windowDidLoad];
[self.testLabel setAllowsEditingTextAttributes:YES];
[self.testLabel setSelectable:YES];
NSMutableAttributedString* string1 = [[NSMutableAttributedString alloc] init];
NSString* inString = @"Apple Computer";
NSURL* aURL = [NSURL URLWithString:@"www.google.com"];
NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:inString];
NSRange range = NSMakeRange(0, [attrString length]);
[attrString beginEditing];
[attrString addAttribute:NSLinkAttributeName value:[aURL absoluteString] range:range];
// make the text appear in blue
[attrString addAttribute:NSForegroundColorAttributeName value:[NSColor blueColor] range:range];
// next make the text appear with an underline
[attrString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSSingleUnderlineStyle] range:range];
[attrString addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Helvetica" size:20] range:range];
[attrString endEditing];
[string1 appendAttributedString: attrString];
[self.testLabel setAttributedStringValue:string1];
[self.testLabel setFont:[NSFont fontWithName:@"Helvetica" size:20]];
}
@end
应用程序委托:
@interface TFTAppDelegate : NSObject <NSApplicationDelegate>
@property(nonatomic, strong)TFTWindowController *windowController;
@end
@implementation TFTAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
self.windowController = [[TFTWindowController alloc] initWithWindowNibName:@"TFTWindowController"];
[_windowController showWindow:nil];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)