1

So, this is my case :

  • One NSBox
  • Inside this, there is an NSTextField

What I want to do is :

  • Programmatically manage to position the textField in the box (e.g. top left, top right, center, etc).

I've tried doing it with NSTextField's bounds or frame, but I don't think I can make it...

Any ideas?

4

2 回答 2

1

If I understood your question correctly, do you just want to make sure the NSTextField is always in the top left, center, or whatever, when the view is resized? If so, then you can use struts and springs for that. If you just want to move the text field, then changing the frame should work. Ex, if you have a text field called foo, you can set the whole frame:

[foo setFrame: CGRectMake(desiredX,desiredY,foo.bounds.size.width,foo.bounds.size.height)];

or just its origin (effectively repositioning it):

[foo setFrameOrigin: CGPointMake(desiredX, desiredY)];
于 2012-09-09T17:27:47.067 回答
0

For interest's sake, there is also a minilanguage for positioning in Cocoa called Visual Format Language. It lets you specify relationships such as:

|-50-[orchidBox]-50-|

Which means the child element should be indented 50px from it's superview on each side.

于 2012-09-09T19:52:56.400 回答