Drawing Text to an NSView

Discussion in 'Mac Programming' started by RaulAmor, Apr 25, 2010.

  1. RaulAmor macrumors newbie

    Joined:
    Apr 25, 2010
    #1
    How would I implement an NSView so that whenever text is typed into an NSTextField and an NSButton is pressed, the text will be drawn into the NSView?

    I have an Custom View in my GUI in Interface Builder, how would I connect it from there?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    If you have a custom view then you're going to have to have a subclass of NSView in your code and do the drawing in drawRect:. You're going to have to write code: not use IB.
     
  3. RaulAmor thread starter macrumors newbie

    Joined:
    Apr 25, 2010
    #3
    Alright, so in my view class I should have a method to draw (for example) NSString *string to the view, and I call this method from drawRect.

    Now do I create an instance from of that view class in my controller class, set "string" to whatever the NSTextField contains, then simply call setNeedsDisplay on that instance?
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Normally you just do the drawing right there in drawRect:...
     
  5. RaulAmor thread starter macrumors newbie

    Joined:
    Apr 25, 2010
    #5
    Ok, so the extra method for drawing is unnecessary.

    Well I tried what I had described in my previous post and it didn't work. Is it because I'm working through an instance and not the actual object?

    I can't figure out how to connect the NSTextField text to the view class's string variable any other way...
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    I have no idea what this is supposed to mean. An instance is an object.

    That's what your controller layer in a well designed MVC application is for.
     
  7. RaulAmor thread starter macrumors newbie

    Joined:
    Apr 25, 2010
    #7
    Sorry, I mistyped that. By creating an instance of the view class my Custom View is attached to, should I be able to modify the Custom View through that instance?
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    No. That makes no sense at all. Your custom view is an instance. You need to set the class of that to your custom class in Interface Builder. Do not create another instance of it and hope that altering one object will somehow effect another.
     
  9. RaulAmor thread starter macrumors newbie

    Joined:
    Apr 25, 2010
    #9
    Ok, I had done that originally so I guess that's good news.

    So how do I get my controller class to communicate with/modify the Custom View?
     
  10. Littleodie914 macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #10
    You may want to slow down. :)

    Try working through this tutorial. It will really help you both in the short- and long-term.

    http://cocoadevcentral.com/d/learn_cocoa_two/

    If you're still having problems, or wish to investigate further, CocoaDevCentral has a lot of other great tutorials, and this book by Aaron Hillegass is pretty great:

    http://www.amazon.com/Cocoa-Programming-Mac-OS-3rd/dp/0321503619/ref=sr_1_1?ie=UTF8&s=books&qid=1272245838&sr=8-1

    Also, I want to finally note that I actually had to do something like this earlier today. Here's the code I used, I won't tell you what it does or what to change, I'll leave it as a lesson for the reader. Happy coding! :)

    Code:
    - (void)drawRect:(NSRect)dirtyRect {
    	NSGradient *backgroundGradient = [[NSGradient alloc] initWithStartingColor:[NSColor grayColor] endingColor:[NSColor blackColor]];
    	[backgroundGradient drawInRect:dirtyRect angle:90];
    
    	NSMutableDictionary *drawStringAttributes = [[NSMutableDictionary alloc] init];
    	[drawStringAttributes setValue:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
    	[drawStringAttributes setValue:[NSFont fontWithName:@"American Typewriter" size:24] forKey:NSFontAttributeName];
    	NSShadow *stringShadow = [[NSShadow alloc] init];
    	[stringShadow setShadowColor:[NSColor blackColor]];
    	NSSize shadowSize;
    	shadowSize.width = 2;
    	shadowSize.height = -2;
    	[stringShadow setShadowOffset:shadowSize];
    	[stringShadow setShadowBlurRadius:6];
    	[drawStringAttributes setValue:stringShadow forKey:NSShadowAttributeName];	
    	[stringShadow release];
    	
    	NSString *MRString = @"Hello RaulAmor!";
    	NSString *budgetString = [NSString stringWithFormat:@"%@", MRString];
    	NSSize stringSize = [budgetString sizeWithAttributes:drawStringAttributes];
    	NSPoint centerPoint;
    	centerPoint.x = (dirtyRect.size.width / 2) - (stringSize.width / 2);
    	centerPoint.y = dirtyRect.size.height / 2 - (stringSize.height / 2);
    	[budgetString drawAtPoint:centerPoint withAttributes:drawStringAttributes];
    	[drawStringAttributes release];
    }
     

Share This Page