4

I'm trying to create a NSTextField with a button on its right side (similar to a NSSearchView), but I can't figure out how to have the NSTextField's text scroll without going under the NSButton.

Here is my code thus far:

rightButton = [[NSButton alloc] initWithFrame:rect];
[rightButton setTarget:self];
[[rightButton cell] setHighlightsBy:NSNullCellType];
[rightButton setImage:[NSImage imageNamed:@"rightButton"]];
[rightButton setAction:@selector(action:)];
[rightButton setBordered:NO];

Any thoughts?

EDIT: Here is a screenshot of the problem. Screenshot of the bug.

Eimantas
  • 48,927
  • 17
  • 132
  • 168
individualtermite
  • 3,615
  • 16
  • 49
  • 78
  • A bit more detail. DO you need the button to stay stationary while the text field is being entered into? – gnasher Mar 28 '11 at 00:02
  • Hi Kenny: At the moment, the button stays in the same place, simply obscures the text when it reaches the button. – individualtermite Mar 28 '11 at 00:14
  • Are you trying to make the text wrap, or just have the text not be in the "column" where the button is? – drewag Apr 05 '11 at 02:18
  • Consider editing your question to show a screenshot of the current behaviour in your program. –  Apr 06 '11 at 02:33
  • @dreway - Just trying to have the text not be in the column. @Bavarious - Good idea, I've edited the question to add the photo. – individualtermite Apr 06 '11 at 14:37

1 Answers1

2

I think this is what you're looking for. Basically what you're doing is adjusting the frame to compensate for the button you put inside the field.

Community
  • 1
  • 1
sudo rm -rf
  • 29,408
  • 19
  • 102
  • 161
  • Thanks! Just one question - When that sample code calls [self countAreaRectForBounds:bounds], Xcode says that it is an invalid initializer... Is this a custom function? – individualtermite Apr 08 '11 at 02:12
  • `countAreaRectForBounds` would be your custom view you're putting inside the textField, I believe. So maybe `[rightButton bounds]` – sudo rm -rf Apr 08 '11 at 02:23
  • Also, if I want to programmatically change the NSTextField's cell to a subclass, is there a recommended way to do that? – individualtermite Apr 11 '11 at 00:06
  • Is it created in Interface Builder? Does it have to be programmatically? If you don't care, it's easier if you use Interface Builder. Just change this field here (assuming you have Xcode 4): http://bit.ly/g2DGue – sudo rm -rf Apr 11 '11 at 02:18
  • Yeah, unfortunately it has to be programmatically. – individualtermite Apr 11 '11 at 02:52
  • @PF1: Did you create the textField programmatically or in IB? – sudo rm -rf Apr 11 '11 at 03:48
  • @sudo I created it programmatically with "NSTextField *field = [[NSTextField alloc] initWithFrame:textFieldFrame]". – individualtermite Apr 11 '11 at 15:35
  • Okay, then suppose your custom textField class is named `customField`, then just do `customField *field = [[customField alloc] initWithFrame:textFieldFrame]` – sudo rm -rf Apr 11 '11 at 16:06
  • @sudo I apologize if I'm missing something, but NSTextFieldCell doesn't seem to respond to initWithFrame (it crashes the application)... Doesn't it need to be contained in a NSTextField? – individualtermite Apr 11 '11 at 18:28
  • Ahh, you created a custom cell. My bad, I misunderstood your question. Maybe ask that in a different question, as I honestly don't know how to assign a custom cell to a NSTextField programmatically. It's easy enough in IB, but if you created the field in code it's not going to help. :) – sudo rm -rf Apr 11 '11 at 18:35