What kind of object is this? Custom textbox in an NSView, as in e.g. Keynote

Discussion in 'Mac Programming' started by MrFusion, Sep 30, 2007.

  1. MrFusion macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #1
    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?
     
  2. Spike099 macrumors regular

    Joined:
    Feb 18, 2007
    Location:
    Canada
    #2
    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.
     
  3. MrFusion thread starter macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #3
    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.
     
  4. Spike099 macrumors regular

    Joined:
    Feb 18, 2007
    Location:
    Canada
    #4
    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.
     
  5. Spike099 macrumors regular

    Joined:
    Feb 18, 2007
    Location:
    Canada
    #5
    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.
     
  6. MrFusion thread starter macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #6
    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.
     
  7. Spike099 macrumors regular

    Joined:
    Feb 18, 2007
    Location:
    Canada
    #7
    I see... I think I'll be in your position soon enough. Haven't attempted too much custom drawing yet.

    Avoid the Overuse of Views

    I suppose if you only have several textfields, your ok to use em.
     
  8. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #8
    You might want to read up on NSTextCell and how it works. It has some nifty tricks it does that might apply here.
     
  9. MrFusion thread starter macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #9
    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!
     

Share This Page