views:

65

answers:

1

Hello,

Im attempting to make a little app that lets you add text boxes to you canvas (window). I have an NSTextField that needs to let you drag it around the window. When you drop it it needs to stay in the spot you mouse left it. Heres my code to make the fist text field:

NSTextField *myTextField=[[NSTextField alloc] initWithFrame:NSMakeRect(200.0, 200.0, 200.0, 25.0)];
[myTextField setBordered:NO];
[myTextField setStringValue:@"Double Click to edit"];
[[window contentView] addSubview:myTextField];
//Some sort of dragging code for myTextField

If anyone has ever done something like this any help is really appreciated.

A: 

My first instinct would be to create a subclass of NSTextField and override some or all of mouseDown:, mouseDragged:, and mouseUp: to create the dragging behavior you want. The mouse events section in Apple's Event-Handling Guide might be helpful.

Isaac
Thanks Im using the mouseDragged but Im trying to figure out how to update the tex fields position.
happyCoding25
Ahh, okay. You might look for open-source examples of other draggable controls. The one I've got in mind-- http://code.google.com/p/igresizablecombobox/ --isn't exactly the kind of dragging you're doing, but if you look at the code there, you can see how it looks at changes in the mouse position and uses those changes to alter the height of the combo box popup.
Isaac