Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MrFusion

macrumors 6502a
Original poster
Jun 8, 2005
613
0
West-Europe
In Keynote and Pages you have a large working area to which you add text and objects. One such object is a Textbox (in Keynote, located left of Shapes).
It is a piece of text with a black border around it. Changing the text or its attributes changes the box size.

I am trying to replicate this behaviour, with little succes. I have tried modifying NSTextField's and NSTextView's.

My question is what kind of object is this?
 

Spike099

macrumors regular
Feb 18, 2007
143
0
Canada
Not sure what they use in keynote/pages... I created something similar to this using a custom NSTextView.

Upon initialization of the textview, I set some parameters

Code:
[self setDrawsBackground:NO];
[self setSelectable:NO];
[self setEditable:NO];

If the user clicks, or double clicks the textview, then

Code:
[self setSelectable:YES];
[self setEditable:YES];

The user can then edit the text.

Once the user clicks away from the textview, you have to send it a message so that you can set editable and selectable back to NO.

Hope this helps.
 

MrFusion

macrumors 6502a
Original poster
Jun 8, 2005
613
0
West-Europe
Not sure what they use in keynote/pages... I created something similar to this using a custom NSTextView.

Upon initialization of the textview, I set some parameters

Code:
[self setDrawsBackground:NO];
[self setSelectable:NO];
[self setEditable:NO];

If the user clicks, or double clicks the textview, then

Code:
[self setSelectable:YES];
[self setEditable:YES];

The user can then edit the text.

Once the user clicks away from the textview, you have to send it a message so that you can set editable and selectable back to NO.

Hope this helps.

I would also like some more control about how it's drawn (background color, border thickness and color). I have been playing around with NSTextView, I am not sure your suggestion would change size correctly.

In my app, I only want to allow one line of attributed text with arbitrary attributes.
 

Spike099

macrumors regular
Feb 18, 2007
143
0
Canada
Once you have a subclass of NSTextField(in your case). Use the method - (void)drawRect: (NSRect)rect to do some custom drawing.

Something like this.

Code:
- (void)drawRect:(NSRect)rect
{
	[[[NSColor redColor] colorWithAlphaComponent:0.5] set];
	[NSBezierPath fillRect:rect]; // draws the background
	
	// prepare some variables for a border... this could easily be cleaned up a bit
	NSBezierPath* aPath = [NSBezierPath bezierPath];
	
	float width = rect.size.width;
	float height = rect.size.height;
	[aPath setLineWidth:2.0];
	[[NSColor grayColor] set];
	
	// drawing the border.
	[aPath moveToPoint:NSMakePoint(width,0.0)];
	[aPath lineToPoint:NSMakePoint(0.0,0.0)];
	[aPath lineToPoint:NSMakePoint(0.0,height)];
	[aPath lineToPoint:NSMakePoint(width,height)];
	[aPath stroke];
	
	[super drawRect:rect];
}

In your case, [super drawRect:rect]; draws the text for you, so do not omit that.

One thing I need to know, how many text objects are you going to have? You may want to consider having one subclass of NSView which draws it's own text in it's drawRect method. I do believe that this is how keynote, pages and alike have implemented this. The way I have mentioned above, is a fast and easy way out of the problem.
 

Spike099

macrumors regular
Feb 18, 2007
143
0
Canada
Oh, and about size. I'm not to sure how to adjust the width of a textfield automatically. Try using

Code:
- (void)textDidChange:(NSNotification *)aNotification
{
    [self sizeToFit];
    [self setNeedsDisplay:YES];
}

The delegate must be set to self.
Can't promise this will work. Again, look into custom drawing, that may be the ultimate answer.
 

MrFusion

macrumors 6502a
Original poster
Jun 8, 2005
613
0
West-Europe
One thing I need to know, how many text objects are you going to have? You may want to consider having one subclass of NSView which draws it's own text in it's drawRect method. I do believe that this is how keynote, pages and alike have implemented this. The way I have mentioned above, is a fast and easy way out of the problem.

That's what I have been doing so far, until I wanted to edit the text on the view itself. With the keyDown method every key press is captured, unlike with mouse events. I have also no idea how to get the blinking I-cursor to appear.
 

MrFusion

macrumors 6502a
Original poster
Jun 8, 2005
613
0
West-Europe
You might want to read up on NSTextCell and how it works. It has some nifty tricks it does that might apply here.
Could you elaborate on those tricks?
I have tried an NSActionCell and NSTextFieldCell subclass. Although I can display them, I can't edit or even click on them.

I have set target and action in the subclass, selectable and editable are set to YES.

Thanks for the suggestions!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.