【问题标题】:NSAttributedString in TextField changing/resetting on click单击时 TextField 中的 NSAttributedString 更改/重置
【发布时间】:2017-09-07 12:28:18
【问题描述】:

我正在关注苹果的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 文件(我相信)

感谢您的帮助。我会尝试定期检查以回答任何问题/澄清。编辑:请查看我对 bikram 回答的评论,了解我的情况。

【问题讨论】:

    标签: objective-c macos cocoa


    【解决方案1】:

    您遇到的问题是 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
    

    AppDelegate:

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

    【讨论】:

    • 感谢您的回答 - 但是,它并没有解决所有问题。当我将鼠标悬停在标签上时(Apple Computer,大小 20 helvetica),它仍然没有显示白色悬挂光标(用于单击链接);只是一个 I 酒吧。此外,当我注释掉[self.testLabel setFont ...] 和[attrString addAttribute:NSFontAttributeName ...] 时,单击标签时它仍然会重置文本的字体。也许这提供了一些见解?
    • 您可能还在做其他事情,因为在测试项目中我刚刚添加了此功能,并且我确实显示了手形光标,如果您想要测试项目,它不会切换回字体我可以发在这里。
    • 这将非常有用,请做!
    • 顺便说一句,我在 Mavericks 10.9.3 上使用 XCode 5.1.1。如果您使用的是10.9.2,请升级操作系统,因为我已经看到了一些与此无关的 UI 问题。
    • 我肯定在使用 5.1.1 和 10.9.3。不知何故,使用您的代码后,我仍然遇到问题。这令人困惑。我在这里复制粘贴了我的代码(我相信我完全是从你的帖子中复制的——没有设置头文件之类的东西)。 pastebin.com/gxMAPEk4
    【解决方案2】:

    看来您需要集中文本字段才能显示手。我建议在代码中加入这两行来解决光标问题:

    [self.testLabel selectText:self];
    [[self.testLabel currentEditor] setSelectedRange:NSMakeRange(0, 0)];
    

    在这个答案中,我使用了this link 中的一些代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      • 2012-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多