NSAttributedString在TextField更改/重置点击 [英] NSAttributedString in TextField changing/resetting on click

查看:339
本文介绍了NSAttributedString在TextField更改/重置点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关注的是来自苹果的 THIS 指南,但它不是真正工作正常。

I'm following THIS guide from apple, but it's not really working properly.

基本上,我想通过一个自定义的WindowController类添加一个超链接到窗口中的NSTextField。我可以得到超链接工作中的一些问题:

Basically, I'm trying to add a hyperlink to an NSTextField within a Window, via a custom WindowController class. I'm able to get the hyperlink working with a few problems:


  • 当我将鼠标悬停在超链接上时,我得到一个'I bean'指示您可以选择文本的光标)。我想要一个通常出现在超链接上的手

  • 当我点击超链接文本时,它会在浏览器中成功打开链接,但是它会更改文本大小和格式不再居中,回到一些默认值)。

经过一番实验,我发现最初的字符串格式化(例如,大小,字体之前我点击它)是.xib文件我创建的标签。在我点击后,它更改为一些默认字体,我似乎不能以任何方式影响。

After a bit of experimentation, I've discerned that the initial string formatting (e.g. size, font before I click on it) is that of the .xib file I created the label in. After I click, it changes to some default font that I can't seem to affect in any way. The hyperlink persists, though.

以下是一些代码:

AboutWindowController.h

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非常简单: (我相信)

The .xib is very simple: it's a window with a label in it, and I linked the label to the .h file properly (I believe)

感谢您的帮助。我会尽量回来定期回来回答任何问题/澄清。编辑:请检查我对bikram的回答更新我的情况。

Thanks for the help. I'll try to check back regularly to answer any questions/clarifications. Please check my comment on bikram's answer for an update of my situation.

推荐答案

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

The problem you are hitting is that NSMutableAttributedString is trying to force its formatting and NSTextField is forcing its own.

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

My main XIB is having only menu and my windowController XIB has one Label NSTextField.

windowController: / p>

windowController:

@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:

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

这篇关于NSAttributedString在TextField更改/重置点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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