On the iPhone a UITextField can have a clearButtonMode set to show a small clear button (X) at the end of a text input. Similarly, on the Mac the NSSearchField has a nice clear button on it at the end. My question is - is there a way to enable this on a normal NSTextField?
There's nothing built-in; just use a search field and turn off the magnifying glass:
[[button cell] setSearchButtonCell:nil];
I was looking for the same thing for a multi-lined NSTextField the other day, with no luck so ended up using a button set with no border and to toggle with a small image. set very close to the NSTextField (not on it)
and:
- (IBAction)clearTextViewTex:(id)sender{
[textField performSelector:#selector(selectAll:)];
[textField performSelector:#selector(delete:)];
}
Doing it this way also retained the undo functions, without me having to write any NSUndoManager stuff.
Related
I have a simple app with a Tool Bar at the bottom. Depending upon the context of what the user is doing, I may want various buttons visible or not in the Tool Bar. So, in my code, I remove the weak modifier for the IBOutlet reference to those buttons, and I remove the non-visible buttons in viewDidLoad. Great.
Now, when the context changes, I add/remove buttons as desired using
toolBar.items?.removeAtIndex(0)
toolBar.items?.insert(playButton, atIndex: 0)
Great, works well. But for aesthetics, I would like to have the button removal/insert animated. Apparently the animated:YES argument from Objective-C days is gone. Do you know, does the API support animation of the button shuffling? Or, if not, how best to accomplish this?
Thanks!
There are a number of posts on Stack about this subject but I can't get any to work.
I have a Button whose text literal is supplied by a variable which can change in length. I want the button to be sufficiently long so the text is readable rather than concatenated in the middle with ".....".
If I use something like
CGSize stringsize = [myString sizeWithFont:[UIFont systemFontOfSize:15]];
NSLog(#"Width = %f", stringsize.width);
[myButton setFrame:CGRectMake(20,0,stringsize.width, stringsize.height)];
Similarly I have been unable to get commands like to work either.
[self myButton sizeToFit];
Does anyone have a solution that does work?
I am trying to implement these from viewDidLoad.
Are you using a storyboard? If so, go to your file inspector in the storyboard and see if you have "use autolayout" checked. From what I can tell, uibuttons won't respond to frame changes while this option is checked.
Answer for Xcode 7.2
Click on the view that needs the fit
(Not from the list of views from the left panel, but in the stage itself)
Now click on Editor menu, and then Size to Fit Content
Voila!!
You can use the shortcut command + =
I put a textfield in a window, and I want the textfield draw background only when focused.
I know that all the controls in the window share one field editor.
I tried subclass nstextfield and implement becomeFirstResponder and resignFirstResponder.
And tried use custom singleton editor for the window .
Any one know how to achieve this?
In the NSWindow ,every textfield or button share one instance of field editor(a singleton NSTextView instance),so when you click the textfield, textfield become firstResponser first,and then quickly pass it to the shared field editor. So when the textfield lost focus ,the resignFirstResponder of the textfield will never be called(because the field editor is the FirstResponder now).
You can look at fieldEditor:forObject: in NSWindow API.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSWindow_Class/Reference/Reference.html#//apple_ref/occ/instm/NSWindow/fieldEditor:forObject:
SOLUTION:
(Thanks , Michael Gorbach)
In my window controller
- (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject
{
NSText *text = [sender fieldEditor:YES forObject:self];
if(text&&[anObject isKindOfClass:[MyCustomTextField class]])
{
[text setBackgroundColor:[NSColor whiteColor]];
[text setDrawsBackground:YES];
}
return text;
}
I just did this recently, in a tableView. You need to use a custom cell and fieldEditor. Specifically, you need to call setDrawsbackground:YES on the NSText/NSTextView object that is the field editor, and setBackground: to configure your color of choice. There are two places to set up a custom field editor.
One is to implement setUpFieldEditorAttributes: on a custom NSTextFieldCell subclass that you have configured your NSTextField to use, and another is to use the window or window delegate method windowWillReturnFieldEditor:toObject:.
Note that if the first method doesn't work for a particular setting, sometimes you need to use the second, because it gets in earlier in the codepath.
I've created a custom subclass of NSCell with an NSImageCell, some NSTextFieldCells, and an NSPopUpButtonCell.
I'm initializing the pop up cell using:
myPopUpCell = [[NSPopUpButtonCell alloc] init];
[myPopUpCell setBordered:NO];
[myPopUpCell setAutoenablesItems:NO];
[myPopUpCell addItemsWithTitles:[NSArray arrayWithObjects:#"Item1", #"Item2", #"Item3"]];
And drawing it in drawInteriorWithFrame:inView:
Everything seems to work great, except that when clicking on the pop up cell while running my app the cell does not pop up. Any suggestions about what might be wrong?
Drawing the pop-up button cell in drawInteriorWithFrame:inView: is going to do just that; draw it, but nothing else. Handling click events is unrelated to drawing, so you're going to have to do some work in your custom cell to interpret mouse events, and if they're inside the frame you're using for the pop-up button, pass them on to the button cell. Start by subclassing the methods listed in the NSCell docs under tracking the mouse, such as –trackMouse:inRect:ofView:untilMouseUp:, and you should be be able to figure out what's needed to make the button cell act correctly.
Depending on what you're doing you may actually find it easier to draw the title string yourself, and just use NSMenu's +popUpContextMenu:withEvent:forView:.
In my application I have some controls like NSButton and NSTextfield I want to turn invisible.
I know I can do it n the Interface Builder but I need to do it in the code.
I still haven't found the right message I need to send to the controls.
[myView setHidden:YES];
Actually, this will also disable the control. If you want the control to still work, such as for an NSButton, [button setTransparent:YES] is the one to use.