Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7072345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:47:51+00:00 2026-05-28T05:47:51+00:00

I would like to enter text in a NSTextView so that when I hit

  • 0

I would like to enter text in a NSTextView so that when I hit return the NSTextView clears with the cursor going back to the original position. Then the entered text displays on the screen using the drawRect of an NSView.

This what I have currently which doesn’t work. Basically self.outputString is always the current value of [textview string] even though I draw and then clear. The current code below doesn’t draw any string to the screen. If I remove [textview setString:@""] then the current text always shows what I’m writing.

- (void)drawRect:(NSRect)dirtyRect
{
    [[NSColor whiteColor] setFill];

    NSRectFill(dirtyRect);

    // Drawing code here.

    NSPoint point = NSMakePoint(50.0f, 700.0f);
    NSMutableDictionary *font_attributes = [[NSMutableDictionary alloc] init];
    NSFont *font = [NSFont fontWithName:@"Futura-MediumItalic" size:35.0f];
    [font_attributes setObject:font forKey:NSFontAttributeName];
    [self.outputString drawAtPoint:point withAttributes:font_attributes];
    [font_attributes release];
}


- (BOOL)textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector {

    [self enterPressed:[textView string]];
    [textView setString:@""];


    NSRange range = { 0, 0 };
    [self.textView setSelectedRange: range];

    return YES;
}

- (void) enterPressed:(NSString*)stringEntered
{
    NSLog(@"enter pressed with string:  %@", stringEntered);

    self.outputString = stringEntered;
    [self setNeedsDisplay:YES];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:47:52+00:00Added an answer on May 28, 2026 at 5:47 am

    First off, set a delegate on the NSText object so you can take advantage of some of the delegate methods declared by the NSTextDelegate protocol.

    The method you probably want to catch first is the textDidChange: notification, looking for when a enter is pressed.

    Also, does your drawRect method ever get called? If you don’t know, set a breakpoint and find out. What object is that drawRect associated with? Why do drawRect anyways? You could just have a nice centered NSTextField as an IBOutlet and set the font on that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Like in stackoverflow, I would like that if a user begins to enter text
I would like to have a textbox that lets a user enter some text
I have HTML that looks like this: <input custom_attribute=so cool type=text /> I would
I have an html box with which users may enter text. I would like
So I have this shell script: echo Enter text to be classified, hit return
I would like to take a string that the user will enter to a
i would like to add a character to a text-box that separates two characters
I would like to simulate a user pressing shift-enter in a text area. Here
Have a text input box. Would like the user to enter a string in
I would like to create a form where a user can enter an arbitrary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.