NSTextField numbers that can RESCALE


naifz22

New Member
Joined
Jun 4, 2007
Messages
2
Reaction score
0
hello everyone,

i want to type in numbers in the NSTextField and want it to resize the NSView. I want it to change the scaling of X and Y:

[transform scaleXBy: bounds.size.width/10 yBy: bounds.size.height/100];

i'll type in one number for x and another for y, and x will affect 10 and y will affect 100. i can't seem to make it work...


Than you!

the whole code is:



#import "RanWithParameters.h"


@implementation RanWithParameters

- (id)initWithFrame:(NSRect)rect
{
NSLog(@"-------------initWithFrame------------------");
if (self = [super initWithFrame:rect])
{
transform = [[NSAffineTransform alloc] init];
path = [[NSBezierPath alloc] init];
//xmax = [[NSString alloc] init];
}
return self;
}

- (IBAction)generate:(id)sender
{
NSLog(@"generate");
[self drawRandomLine];
[self setNeedsDisplay:YES];
}

- (void)drawRandomLine
{
int i;
NSPoint p;

//Remove previosly drawn random points
[path removeAllPoints];

//Create a path object
[path setLineWidth:2.0];
p = [self randomPoint:0];
[path moveToPoint: p];

for (i = 0; i <= 10; i++)
{
p = [self randomPoint:i];
[path lineToPoint: p];
}
}

// randomPoint returns a random point inside the view
- (NSPoint)randomPoint:(long)index
{
NSPoint result;
result.y = (random() % 100) -50;
result.x = index + 0;

return result;
}

//Setting scales
- (void)setScale:(NSRect)bounds
{
NSAffineTransformStruct I, unitsToPix;

NSLog(@"setScale");

I.m11 = 1;
I.m12 = 0;
I.m21 = 0;
I.m22 = 1;
I.tX = 0;
I.tY = 0;

[transform setTransformStruct:I];
[transform translateXBy: bounds.origin.x yBy: bounds.origin.y + bounds.size.height/2.0];
[transform scaleXBy: bounds.size.width/10 yBy: bounds.size.height/100];

unitsToPix = [transform transformStruct];

NSLog(@"unitsToPix.m11=%f unitsToPix.m12=%f unitsToPix.m21=%f unitsToPix.m22=%f unitsToPix.tX=%f unitsToPix.tY=%f",
unitsToPix.m11, unitsToPix.m12, unitsToPix.m21, unitsToPix.m22, unitsToPix.tX, unitsToPix.tY);
}

- (IBAction)xMax:(id)sender
setScale:(NSRect)bounds
{
float xMax;

NSString *xmax = [randomOutlet stringValue];

xMax = [xmax floatValue];

[transform scaleXBy: bounds.size.width/xMax yBy: bounds.size.height/100];
}

- (void)drawRect:(NSRect)rect
{
NSRect bounds;

NSLog(@"======= drawRect ======");

bounds = [self bounds];

[self setScale:bounds];

//Fill the view with black
[[NSColor blackColor] set];
[NSBezierPath fillRect:bounds];

// Draw the path red
[[NSColor redColor] set];
[path transformUsingAffineTransform: transform]; // transform path in units to path in pixels
[path stroke]; // draw path in pixels
[transform invert]; // invert transform to go from pixels to units
[path transformUsingAffineTransform: transform]; // transform path in pixels back to path in units
[transform invert]; // invert transform back to take us from units to pixels
}

- (void)dealloc
{
[path release];
[transform release];
[super dealloc];
}



@end
 
Ad

Advertisements

Kaveman

New Member
Joined
Nov 26, 2010
Messages
3,558
Reaction score
49
You're over complicating things.

Just calculate the size required and use NSView setBounds:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top