增加NSTextView的字体大小

时间:2013-09-17 14:30:21

标签: cocoa nstextview

我正在尝试创建用户单击的输入文本框(点是用户单击的NSPoint)。

现在我有它工作,但显示的输入框太小了。我想让它填满外部视图(以蓝色显示)。

    // Text Storage
    NSTextStorage *textStorage = [[NSTextStorage alloc] init];
    [textStorage setFont:[NSFont fontWithName:@"Menlo" size:18]];

    // Layout Manager
    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
    [textStorage addLayoutManager:layoutManager];

    NSRect textViewBounds = NSMakeRect(point.x, point.y, NSWidth(self.window.frame), 50);

    // Text Container
    NSTextContainer *textContainer;
    textContainer = [[NSTextContainer alloc]
                     initWithContainerSize:textViewBounds.size];
    [layoutManager addTextContainer:textContainer];

    // Text View
    NSTextView *textView = [[NSTextView alloc] initWithFrame:textViewBounds];

    // Of course text editing should be as undoable as anything else.
    [textView setAllowsUndo:YES];
    [textView setBackgroundColor:[NSColor blueColor]];
    //[textView setDrawsBackground:NO];

    [self addSubview:textView];

    // Make the editing view the first responder so it takes key events and relevant menu item commands.
    //[self.window setContentView:textView];
    [self.window makeFirstResponder:textView];

enter image description here

我在这里缺少什么?

1 个答案:

答案 0 :(得分:4)

NSTextViewNSText的子类,它公开了方法:

- (void)setFont:(NSFont *)aFont

您应该可以通过NSFont设置字体系列和大小,这是各种方法。如果您只想设置大小,可以使用:

NSFont *font = [NSFont userFontOfSize:21.0];

要指定字体系列和大小,您可以使用:

+ (NSFont *)fontWithName:(NSString *)fontName size:(CGFloat)fontSize
相关问题