11

我正在尝试将文本绘制到黑色背景上,并且发现文本的呈现方式存在一些差异。这是我的代码:

NSTextView * textView = [[NSTextView alloc] initWithFrame:NSMakeRect(0.0f, 0.0f, 500.0f, 50.0f)];
[textView setDrawsBackground:YES];
[textView setBackgroundColor:[NSColor blackColor]];
[textView setString:@"NSTextView: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."];
[textView setFont:[NSFont fontWithName:@"Helvetica" size:18.0f]];
[textView setEditable:NO];
[textView setTextColor:[NSColor whiteColor]];

[flippedView addSubview:textView];

NSTextField * textField = [[NSTextField alloc] initWithFrame:NSMakeRect(0.0f, 50.0f, 500.0f, 50.0f)];
[textField setDrawsBackground:YES];
[textField setBackgroundColor:[NSColor blackColor]];
[textField setBordered:NO];
[textField setBezeled:NO];
[textField setStringValue:@"NSTextField: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."];
[textField setFont:[NSFont fontWithName:@"Helvetica" size:18.0f]];
[textField setEditable:NO];
[textField setTextColor:[NSColor whiteColor]];

[flippedView addSubview:textField];

NSTextField 内的文本看起来很棒,但 NSTextView 内的文本看起来太粗了。我究竟做错了什么?

这是一个屏幕截图:

在此处输入图像描述

4

2 回答 2

0

The NSTextField probably tells its cell about the dark background, which is not what you may want. Tell it to render for light background:

[[textField cell] setBackgroundStyle:NSBackgroundStyleLight];
于 2013-10-15T09:40:44.443 回答
0

我试过这样只是替换你的这一行: -

//replace below this line
    //[textView setFont:[NSFont fontWithName:@"Helvetica" size:18.0f]];
//Modified this below line
NSFontManager *fontManager = [NSFontManager sharedFontManager];
        NSFont *regular = [fontManager fontWithFamily:@"Helvetica"
                                                  traits:NSUnitalicFontMask
                                                  weight:0
                                                    size:18.0f];
        [textView setFont:regular];
于 2013-10-15T09:56:00.267 回答