NSTextFieldCell with Multiple Lines

℡╲_俬逩灬. 提交于 2019-12-09 17:46:33

问题


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?


回答1:


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:

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




回答2:


What's wrong with using an NSTextView?



来源:https://stackoverflow.com/questions/6552682/nstextfieldcell-with-multiple-lines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!