我正在尝试创建一个用户单击的输入文本框(点是用户单击的 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];
Run Code Online (Sandbox Code Playgroud)

我在这里缺少什么?
NSTextView是NSText其公开方法的子类:
- (void)setFont:(NSFont *)aFont
Run Code Online (Sandbox Code Playgroud)
您应该能够通过NSFont各种方法设置字体系列和大小。如果只想设置大小,可以使用:
NSFont *font = [NSFont userFontOfSize:21.0];
Run Code Online (Sandbox Code Playgroud)
要指定字体系列和大小,您可以使用:
+ (NSFont *)fontWithName:(NSString *)fontName size:(CGFloat)fontSize
Run Code Online (Sandbox Code Playgroud)