具有多行的 NSTextFieldCell

发布于 2024-11-18 12:05:42 字数 178 浏览 4 评论 0原文

我需要显示一个包含多行且每行格式不同的 NSTextFieldCell 。

像这样的:

第 1 行:标题
第 2 行:描述

我对 NSTextFieldCell 进行了子类化,但我不知道如何继续使用它。

有什么想法吗?

I need to show an NSTextFieldCell with multiple lines with different format on each line.

Something like this:

Line 1: Title
Line 2: Description

I subclassed NSTextFieldCell but I don't know how to go on with it.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如果没有 2024-11-25 12:05:42

首先,您不必通过子类化 NSTextFieldCell 来实现此目的,因为作为 NSCell 的子类,NSTextFieldCell code> 继承 -setAttributedStringValue:。您提供的字符串可以表示为 NSAttributedString。以下代码说明了如何使用普通的 NSTextField 获得所需的文本。

MDAppController.h:

@interface MDAppController : NSObject <NSApplicationDelegate> {
    IBOutlet NSWindow *window;
    IBOutlet NSTextField *textField;
}

@end

MDAppController.m:

@implementation MDAppController

static NSDictionary *regularAttributes = nil;
static NSDictionary *boldAttributes = nil;
static NSDictionary *italicAttributes = nil;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

    if (regularAttributes == nil) {
        regularAttributes = [[NSDictionary dictionaryWithObjectsAndKeys:
        [NSFont systemFontOfSize:[NSFont systemFontSize]],NSFontAttributeName,
                       nil] retain];

        boldAttributes = [[NSDictionary dictionaryWithObjectsAndKeys:
        [NSFont boldSystemFontOfSize:[NSFont systemFontSize]],NSFontAttributeName,
                  nil] retain];

        NSFont *regFont = [NSFont userFontOfSize:[NSFont systemFontSize]];
        NSFontManager *fontManager = [NSFontManager sharedFontManager];
        NSFont *oblique = [fontManager convertFont:regFont
                                       toHaveTrait:NSItalicFontMask];
        italicAttributes = [[NSDictionary dictionaryWithObjectsAndKeys:
              oblique,NSFontAttributeName, nil] retain];
    }


    NSString *string = @"Line 1: Title\nLine 2: Description";
    NSMutableAttributedString *rString =
    [[[NSMutableAttributedString alloc] initWithString:string] autorelease];

    [rString addAttributes:regularAttributes
                     range:[string rangeOfString:@"Line 1: "]];

    [rString addAttributes:regularAttributes
                     range:[string rangeOfString:@"Line 2: "]];
    [rString addAttributes:boldAttributes
                     range:[string rangeOfString:@"Title"]];

    [rString addAttributes:italicAttributes
                     range:[string rangeOfString:@"Description"]];

    [textField setAttributedStringValue:rString];
}
@end

这会产生以下结果:

在此处输入图像描述

现在,取决于您的意图使用此文本后,您可以通过几种不同的方式实现设计。您可能想了解 NSTextView 是否适合您而不是 NSTextField...

First off, you don't have to subclass NSTextFieldCell to achieve this, since, as a subclass of NSCell, NSTextFieldCell inherits -setAttributedStringValue:. The string you provided can be represented as a NSAttributedString. The following code illustrates how you could achieve the desired text with an ordinary NSTextField.

MDAppController.h:

@interface MDAppController : NSObject <NSApplicationDelegate> {
    IBOutlet NSWindow *window;
    IBOutlet NSTextField *textField;
}

@end

MDAppController.m:

@implementation MDAppController

static NSDictionary *regularAttributes = nil;
static NSDictionary *boldAttributes = nil;
static NSDictionary *italicAttributes = nil;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

    if (regularAttributes == nil) {
        regularAttributes = [[NSDictionary dictionaryWithObjectsAndKeys:
        [NSFont systemFontOfSize:[NSFont systemFontSize]],NSFontAttributeName,
                       nil] retain];

        boldAttributes = [[NSDictionary dictionaryWithObjectsAndKeys:
        [NSFont boldSystemFontOfSize:[NSFont systemFontSize]],NSFontAttributeName,
                  nil] retain];

        NSFont *regFont = [NSFont userFontOfSize:[NSFont systemFontSize]];
        NSFontManager *fontManager = [NSFontManager sharedFontManager];
        NSFont *oblique = [fontManager convertFont:regFont
                                       toHaveTrait:NSItalicFontMask];
        italicAttributes = [[NSDictionary dictionaryWithObjectsAndKeys:
              oblique,NSFontAttributeName, nil] retain];
    }


    NSString *string = @"Line 1: Title\nLine 2: Description";
    NSMutableAttributedString *rString =
    [[[NSMutableAttributedString alloc] initWithString:string] autorelease];

    [rString addAttributes:regularAttributes
                     range:[string rangeOfString:@"Line 1: "]];

    [rString addAttributes:regularAttributes
                     range:[string rangeOfString:@"Line 2: "]];
    [rString addAttributes:boldAttributes
                     range:[string rangeOfString:@"Title"]];

    [rString addAttributes:italicAttributes
                     range:[string rangeOfString:@"Description"]];

    [textField setAttributedStringValue:rString];
}
@end

This results in the following:

enter image description here

Now, depending on how you intend for this text to be used, you could implement the design in several different ways. You may want to look into whether an NSTextView might work for you rather than an NSTextField...

弃爱 2024-11-25 12:05:42

What's wrong with using an NSTextView?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文