UITextField change placeholder color when it become first Responder - uitextfield

Is there any way to change the first responder UITextField's placeholder color? Thanks a lot!

You can change the UITextField placeholder color on first responder event as like in its delegate method
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[textField setValue:[UIColor yellowColor] forKeyPath:#"_placeholderLabel.textColor"];
return YES;
}

Related

Intercept Keydown Actions in an NSTextFieldCell

I have a cell-based NSOutlineView which displays NSTextFieldCell objects.
I'd like to respond to keydown or keyup events so as to make the text contained in the NSTextFieldCell bold when the text contains certain preset keywords. What is the most elegant way to achieve this - should I:
Subclass NSOutlineView and override the keydown method
Subclass NSTextFieldCell
Utilize a delegate of some kind
Utilize some other approach
Thanks very much in advance to all for any info!
Found it.
In awakeFromNib:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(actionToTakeOnKeyPress:) name:NSControlTextDidChangeNotification object:theNSOutlineViewThatContainsTheNSTextFieldCell];
Then add a method like this:
- (void) actionToTakeOnKeyPress: (id) sender
{
//will be called whenever contents of NSTextFieldCell change
}
To intercept key presses in a way that they can still be filtered out, various NSResponder messages may be overwritten, such as keyDown: or interpretKeyEvents:.
To be able to do that, a subclass of a NSTextView needs to be used as the field editor. For that, one subclasses NSTextFieldCell and overrides fieldEditorForView:, returning the subclass (see Custom field editor for NSTextFieldCell in an NSTableView).
Here's the relevant code excerpts:
In a subclassed NSTextFieldCell (which then has to be assigned in Interface Builder for the editable column, or returned by the NSTableViewDelegate's dataCellForTableColumn message):
- (NSTextView *)fieldEditorForView:(NSView *)aControlView
{
if (!self.myFieldEditor) {
self.myFieldEditor = [[MyTextView alloc] init];
self.myFieldEditor.fieldEditor = YES;
}
return self.myFieldEditor;
}
It also requires the declaration of a property in the #interface section:
#property (strong) MyTextView *myFieldEditor;
And then in MyTextView, which is a subclass of NSTextView:
-(void)keyDown:(NSEvent *)theEvent
{
NSLog(#"MyTextView keyDown: %#", theEvent.characters);
static bool b = true;
if (b) { // this silly example only lets every other keypress through.
[super keyDown:theEvent];
}
b = !b;
}

Make a NSTableCellView editable

I created a View-Based NSTableView with a single column. This column is populated with a standard NSTableCellView from Interface Builder (I chose the version with image and textfield).
Now I want to make the textfield in the column editable.
My first attempt was to modify the NSTextField from Interface builder and set its behaviour as Editable. It works, indeed when I select a row and I push the enter key the field becomes editable and I can change its value. I thought I would be able to intercept this change thanks to some NSTableViewDataSource method like tableView:setObjectValue:forTableColumn:row: but this method never gets called in response of a textfield edit action.
Which is the right way to deal with editable field in a view-based NSTableView system? I suppose that the NSTableViewDataSource has something to do with it but I don't know how to get its methods called.
Create a subclass of NSTableCellView. (The appropriate .h and .m files) Make the class respond to the NSTextFieldDelegate protocol. Implement the control:textShouldEndEditing: method. Make this subclass the delegate of your label control.
Here is some example code.
CategoryListCell.h
#interface CategoryListCell : NSTableCellView
#end
CategoryListCell.m
#interface CategoryListCell()<NSTextFieldDelegate>
#property (weak) IBOutlet NSTextField *categoryLabel;
#property (assign) BOOL editing;
#property (copy) NSString* category;
#end
#implementation CategoryListCell
- (BOOL)control:(NSControl*)control textShouldBeginEditing:(NSText *)fieldEditor {
self.editing = YES;
return YES;
}
- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor; {
if (self.editing) {
self.editing = NO;
[self mergeFromSource:self.category toDestination:self.categoryLabel.stringValue];
}
return YES;
}
- (void)mergeFromSource:(NSString*)source toDestination:(NSString*) destination {
// your work here
}
#end
Sounds like you need to subclass the NSView that's in the NSTableView cell and make the subclassed view a delegate of the textfield. Your view will then get text change notifications via the NSTextField delegate method:
- (void)textDidChange:(NSNotification *)notification;

How to select a row in an NSTableView when clicking on an NSTextView inside an NSTableCellView?

I have a view-based single-column NSTableView. Inside my NSTableCellView subclass I have an NSTextView which is selectable, but not editable.
When the user clicks on the NSTableCellView directly, the row highlights properly. But when the user clicks on the NSTextView inside that NSTableCellView, the row does not highlight.
How do I get the click on the NSTextView to pass to the NSTableCellView so that the row highlights?
Class hierarchy looks like:
NSScrollView > NSTableView > NSTableColumn > NSTableCellView > NSTextView
Here's what I ended up doing. I made a subclass of NSTextView and overrode mouseDown: as follows...
- (void)mouseDown:(NSEvent *)theEvent
{
// Notify delegate that this text view was clicked and then
// handled the click natively as well.
[[self myTextViewDelegate] didClickMyTextView:self];
[super mouseDown:theEvent];
}
I'm reusing NSTextView's standard delegate...
- (id<MyTextViewDelegate>)myTextViewDelegate
{
// See the following for info on formal protocols:
// stackoverflow.com/questions/4635845/how-to-add-a-method-to-an-existing-protocol-in-cocoa
if ([self.delegate conformsToProtocol:#protocol(MyTextViewDelegate)]) {
return (id<MyTextViewDelegate>)self.delegate;
}
return nil;
}
And in the header...
#protocol MyTextViewDelegate <NSTextViewDelegate>
- (void)didClickMyTextView:(id)sender;
#end
In the delegate, I implement didClickMyTextView: to select the row.
- (void)didClickMyTextView:(id)sender
{
// User clicked a text view. Select its underlying row.
[self.tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:[self.tableView rowForView:sender]] byExtendingSelection:NO];
}
or, use a NSTextField, and then,
textfield.bezeled = NO;
textfield.drawsBackground = NO;
textfield.editable = NO;
textfield.selectable = YES;
[textfield setRefusesFirstResponder: YES];
I think you have essentially the same problem I had here: pass event on.
See the accepted answer.
Following the same pattern, you would subclass NSTextView and override - (void)mouseUp:(NSEvent *)theEvent to pass the event on to the superView, which I'm assuming is the tableView:
- (void)mouseUp:(NSEvent *)theEvent {
[superView mouseUp:theEvent];
}

Xcode/iOS5: Show UIDatePicker when UITextField is pressed (+ hide default keyboard)

I'd like to call UIDatePicker only when UITextField is active. When the date is picked, it should be shown in the same UITextField.
I even don't know how to call the Picker.
Can anyone help me?
there is a delegate method called
-(void)textFieldDidBeginEditing:(UITextField*)textField
{
[textField resignFirstResponder];
in this you have to do this
pick = [[UIDatePicker alloc] init];
[pick setFrame:CGRectMake(0,200,320,120)];
[pick addTarget:self action:#selector(done) forControlEvents:UIControlEventValueChanged]
[self.view addSubview:pick];
}
in
-(void)done
{
[textfield setText:pick.date];
}
release the pick in dealloc
To add to Hitman's answer, I think you should implement the
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
instead and return NO. Then show the date picker and fill the UITextField text according to the picker selection.

Close the keyboard on UITextField

I'm a newbie developing for iOS devices. I inserted an UITextField on InterfaceBuilder, and I assigned with the code:
#interface ComposeViewController : UIViewController {
id <ComposeViewControllerDelegate> delegate;
IBOutlet UITextField *notificationTitle;
}
How I could allow to close the keyboard when the user press the "Return" key?
Set the Delegate of the UITextField to your ViewController, add a referencing outlet between the File's Owner and the UITextField, then implement this method:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == yourTextField) {
[textField resignFirstResponder];
}
return NO;
}
Inherit UITextFieldDelegate protocol
In viewDidLoad method set:
yourTextField.delegate = self
Implement the delegate method below:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[yourTextField resignFirstResponder];
return NO;
}
Inherit UITextFieldDelegate protocol and implement textFieldShouldReturn:, that's you will catch "return" event.
Inside textFieldShouldReturn write [notificationTitle resignFirstResponder];
Add a action target to the event Did End on Exit(UIControlEventEditingDidEndOnExit), in the target function remove the first responder from the text filed using resignFirstResponder.
Adding action target
Note:
1. Nib --- give action to even Did End on Exit
2. In code add target action to the event UIControlEventEditingDidEndOnExit.

Resources