Need help using drawAtPoint

Discussion in 'Mac Programming' started by rand0m3r, Oct 5, 2007.

  1. rand0m3r macrumors regular

    Joined:
    Jun 30, 2006
    #1
    Hi, i'm trying to use this method for an NSAttributedString object. but when i go [object drawAtPoint:p], I get a warning that says there are two definitions of this method, and it is trying to use the one with the signature -(BOOL)drawAtPoint:(NSPoint)aPoint which is not what I want. So how do I get it to use the -(void)drawAtPoint:(NSPoint)aPoint one? thanks.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    What variable type is object?

    If it's id then that may be the problem as both NSString and NSImage define a method with this name. Note that the correct one will actually be chosen at run time, but if you want to get rid of the warning try:

    Code:
    [((NSAttributedString *) object) drawAtPoint:aPoint];
    
    This will allow the compiler to know the type of the object so which version method to call...
     
  3. rand0m3r thread starter macrumors regular

    Joined:
    Jun 30, 2006
    #3
    i made a set of NSAttributedString objects and put it an NSMutableArray and then tried to get them back with objectAtIndex. So they should be (NSAttributedString *) already shouldn't they?
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    The objects will be NSAtteributedString objects so it will work, but the signature of objectAtIndex: is:

    Code:
    -(id) objectAtIndex:(int) index
    
    so objects returned via this are id until you cast them.
     
  5. rand0m3r thread starter macrumors regular

    Joined:
    Jun 30, 2006
    #5
    hey thanks it compiles now. but how come the text doesn't get drawn??!
     
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    Post some of your code for the drawing. Is the NSPoint valid? Is it being drawn within a drawRect: method?
     
  7. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    Check if you are in a flipped view. Drawing text at (0,0) in a flipped view (or is it non-flippd I can never remember) draws the text outside the view as the co-ordinate system starts in the top left and text drawAtPoint: specifies the bottom left corner of the text bounding box so it draws off the top of the view...
     
  8. rand0m3r thread starter macrumors regular

    Joined:
    Jun 30, 2006
    #8
    it is a flipped view but i thought it would be within the bounds.

    NSAttributedString *testString;
    testString = [[NSAttributedString alloc] initWithString:mad:"Testing"];
    [(NSAttributedString *) testString drawAtPoint:NSMakePoint(100, 100)];

    has it got anything to do with putting my view in focus?
     
  9. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #9
    How big is the view?
    Where (in which method) is this code?
    Why are you using an NSAttributedString without attributes? :p
     
  10. rand0m3r thread starter macrumors regular

    Joined:
    Jun 30, 2006
    #10
    put it in the drawRect function and it worked!
     
  11. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #11
    Here's a good tip for future: put ALL code to draw views in drawRect or functions called from drawRect. Otherwise it won't draw :D
     
  12. rand0m3r thread starter macrumors regular

    Joined:
    Jun 30, 2006
    #12
    ah ok. i had NSBezierPath code in initFrame so i thought drawAtPoint would work there too.
     

Share This Page