ilovetacos
ilovetacos

Reputation: 1

Displaying an NSString on a Custom View

I have an interface that has an NSTextField, NSButton, and an NSView. When I type something in the NSTextfield and press the button, I want the text to be drawn in the NSView. So far I have everything connected and working, except for the view.

How can I connect the text and the view so that every time I press the button, the text is drawn to the view?

Upvotes: 0

Views: 1306

Answers (3)

ilovetacos
ilovetacos

Reputation: 1

For simplicity's sake I didn't mention this before, but the app also has a speech element to speak the string. This aspect of the program works fine, so just ignore any messages involving the SpeakAndDraw class (it's actually misnamed and only includes a speech method, nothing about drawing).

View.m

#import "View.h"


@implementation View

@synthesize stringToDraw;

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setAttributes];
        stringToDraw = @"Hola";
    }
    return self;
}

- (void)drawRect:(NSRect)dirtyRect {
    NSRect bounds = [self bounds];
    [self drawStringInRect:bounds];

}

- (void)setAttributes
{
    attributes = [[NSMutableDictionary alloc] init];
    [attributes setObject:[NSFont fontWithName:@"Helvetica"
                                          size:75]
                   forKey:NSFontAttributeName];

    [attributes setObject:[NSColor blackColor]
                   forKey:NSForegroundColorAttributeName];
}

- (void)drawStringInRect:(NSRect)rect
{
    NSSize strSize = [stringToDraw sizeWithAttributes:attributes];
    NSPoint strOrigin;
    strOrigin.x = rect.origin.x + (rect.size.width - strSize.width)/2;
    strOrigin.y = rect.origin.y + (rect.size.height - strSize.height)/2;
    [stringToDraw drawAtPoint:strOrigin withAttributes:attributes];
}

@end

SpeakerController.m

#import "SpeakerController.h"


@implementation SpeakerController

- (id)init
{
    speakAndDraw = [[SpeakAndDraw alloc] init];
    view = [[View alloc] init];
    [mainWindow setContentView:mainContentView];
    [mainContentView addSubview:view];
    return self;
}

- (IBAction)speakText:(id)sender
{
    [speakAndDraw setStringToSay:[text stringValue]];
    [speakAndDraw speak];
    [view setStringToDraw:[text stringValue]];
    [view setNeedsDisplay:YES];
    NSLog(@"%@", view.stringToDraw);
    NSLog(@"%@", [view window]);

}

@end

Upvotes: 0

Peter Hosey
Peter Hosey

Reputation: 96323

How can I connect the text and the view so that every time I press the button, the text is drawn to the view?

Views do their own drawing.

You need to give the view the string to draw, and then set the view as needing display. You'll do these in the action method that you wire the button up to.

First, your custom view class needs to have a property for the value (string, in this case) that it's going to display. From your action method, which should generally be on a controller object, send the view object a setFoo: message (assuming you named the property foo). That takes care of job one: The view now has the value to display.

Job two is even easier: Send the view a setNeedsDisplay: message, with the value YES.

That's it. The action method is two lines.

Of course, since views draw themselves, you also need your custom view to actually draw, so you need to implement the drawRect: method in that class. It, too, will be short; all you need to do is tell the string to draw itself.

Upvotes: 1

Related Questions