Question

I'm trying to create an input text box where the user clicks (point is an NSPoint where the user clicked).

Right now I have it working, but the input box that's being displayed is too small. I'd like to have it fill up the outer view (showed in blue).

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

What am I missing here?

Was it helpful?

Solution

NSTextView is a subclass of NSText which exposes the method:

- (void)setFont:(NSFont *)aFont

You should be able to set the font family and size via NSFont and it's various methods. If you just want to set the size, you can use:

NSFont *font = [NSFont userFontOfSize:21.0];

To specify a font family and size, you can use:

+ (NSFont *)fontWithName:(NSString *)fontName size:(CGFloat)fontSize
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top