objective-c - 单击时更改/重置 TextField 中的 NSAttributedString

标签 objective-c macos cocoa

我正在关注 THIS来自苹果的指南,但它并没有真正正常工作。

基本上,我正在尝试通过自定义 WindowController 类向 Window 中的 NSTextField 添加超链接。我能够让超链接解决一些问题:

  • 当我将鼠标悬停在超链接上时,我得到一个“I bean”(指示您可以选择文本的光标)。我想要一只通常出现在超链接上的手
  • 当我点击超链接文本时,它会在我的浏览器中成功打开链接,但随后会更改文本大小和格式(例如,它不再居中,恢复为默认值)。不过,现在当我将鼠标悬停在它上面时,我得到了手。

经过一些实验后,我发现最初的字符串格式(例如大小、单击之前的字体)是我在其中创建标签的 .xib 文件的格式。单击后,它会变为一些我似乎无法以任何方式影响的默认字体。不过,超链接仍然存在。

部分代码如下:

AboutWindowController.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

AboutWindowController.h

#import <Cocoa/Cocoa.h>

@interface AboutWindowController : NSWindowController
@property (weak) IBOutlet NSTextField *testLabel;

@end

.xib 非常简单:它是一个带有标签的窗口,我正确地将标签链接到 .h 文件(我相信)

感谢您的帮助。我会尝试定期回来查看以回答任何问题/说明。编辑:请查看我对比克拉姆回答的评论,了解我的最新情况。

最佳答案

您遇到的问题是 NSMutableAttributedString 试图强制其格式化,而 NSTextField 正在强制其自己的格式。

我的主 XIB 只有菜单,我的 windowController XIB 有一个 Label NSTextField

窗口 Controller :

@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

应用委托(delegate):

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

关于objective-c - 单击时更改/重置 TextField 中的 NSAttributedString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24318032/

相关文章:

ios - 如何重试基于 block 的 URL 请求

ios - XCode 4.5 中 ARC 的 Phantom 内存泄漏,其中肯定调用了 dealloc 或 Instruments 问题?

objective-c - cocoa 真正慢 NSTextView。有没有更好的方法将文本附加到 NSTextView 的内容?

macos - Applescript iTunes 词典解释

iphone - NSSortDescriptor 用于比较 Cocoa/iPhone 中的 CLLocation 对象

database - 使用 Spotlight 作为应用程序的 "database"

ios - SQLite 数据库在主 UI 线程以外的线程上获取

objective-c - 字符串范围 : high CPU usage

ios - UITabBarController 内 UINavigationController 的 UINavigationBar 中缺少按钮的插图

c++ - OSX Xcode 6 上的 OpenGL 4.1 设置困难