UITextView with DatePicker - ios

I have an UITextView and one of my methods requires to change the UITextView inputView to an UIDatePicker, so the user can select a date for a reminder.
Here is the code I used
self.datePicker = [[UIDatePicker alloc] init];
self.noteTextView.inputView = self.datePicker;
[self.noteTextView becomeFirstResponder];
everything works perfectly, but I can't manage to switch back from datePicker to the default keyboard. What I have tried is the following code:
self.noteTextView.inputView = UIKeyboardTypeDefault;

Set the input view to nil
self.noteVite.inputView = nil;

Related

I am getting error like "Assignment to read only property" in UIView

If VIN is an text field this code works for me to Hiding keyboard without breaking UITextField functionality..
UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
VIN.inputView = dummyView;
But in another case i need to do same thing for UIVIEW.. it is possible to do for UIView ?
UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
VIN.inputView = dummyView;
In this case VIN Is an UIView.. i am getting error like "Assignment to read only property".
UITextfield has inputview. Default inputview is keyboard. uiview has not input view that you can set so you can't assign input view to UIView.
And for hiding keyboard you should call resignFirstresponder like,
[myTextField resignFirstResponder];
This will hide keyboard and when you click textfield it will show keyboard again so you should do like this.
Hope this will help :)
If you want to hide or disable keyboard for particular textfield , you can use following textfield delegate
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
// Here You can do additional code or task instead of writing with keyboard
if (textField == YourTextField)
{
return NO;//Keyboar Wont appear for textfield
}
return YES;
}

UIDatePicker keyboard-like interface

I launch a keyboard-like interface for UIDatePicker using this code:
_meetingtimeTF.delegate = self;
datePicker = [[UIDatePicker alloc]init];
[datePicker setDatePickerMode:UIDatePickerModeDateAndTime];
[datePicker addTarget:self action:#selector(onDatePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
_meetingtimeTF.inputView = datePicker;
Is there a way to customize the size? Currently the white panel take up the whole horizontal screen even though the real interface is 1/3 of the screen.
How to dismiss the interface after I am done with date picking and click a button? I tried [datepicker setHidden:YES] but but sometime it just hide the datePicker and the white panel remain there.

How to select programmatically created text fields?

I am adding text fields to a view programmatically like this:
// Add a text field.
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 280, 40)];
textField.returnKeyType = UIReturnKeyNext;
[textField becomeFirstResponder];
[textField addTarget:self action:#selector(nextButtonPressed:) forControlEvents:UIControlEventEditingDidEndOnExit];
UITextField *textFieldTwo = [[UITextField alloc] initWithFrame:CGRectMake(20, 160, 280, 40)];
textFieldTwo.returnKeyType = UIReturnKeyDone;
[textFieldTwo addTarget:self action:#selector(doneButtonPressed:) forControlEvents:UIControlEventEditingDidEndOnExit];
How can I select these fields later on?
I know how to do this when I'm creating things using UI, but how does this work for dynamically added elements?
Example: I want to focus the second field when "Next" button is pressed.
Thanks!
There are at least two ways of doing it:
Give each of your dynamically created fields a distinct tag, and then retrieve the required field using the tag that you gave it by calling viewWithTag: on the view to which you added your fields, or
Make textField and textFieldTwo instance variables of your class, initialize them when you have to, and then refer to these ivars later when you want to send input to them.
The second way is close to what you do when you add the fields through the Interface Builder. The only difference is that in this case the fields are added programmatically.
In this case, I usually set a tag for each textfield present on my viewController, and assign them my viewController as delegate :
// You should use const to identify quickly your tag
textField.tag = 10;
textFieldTwo.tag = 11;
textField.delegate = self;
textFieldTwo.delegate = self;
Then I implements the textFieldShouldReturndelegate method :
#pragma mark - UITextFieldDelegate protocol conformance
-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
NSInteger nextTag = textField.tag + 1;
// Try to find next responder
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder) {
// Found next responder, so set it.
[nextResponder becomeFirstResponder];
} else {
// Not found, so remove keyboard.
[textField resignFirstResponder];
}
return NO; // We do not want UITextField to insert line-breaks.
}
By doing so, you can have multiple UITextField with a focus moved from one to another without having to implements multiple UIControlEventEditingDidEndOnExit event methods.
You can get these by there tag value. For this
1.. set a unique tag value, to each textField at time of creation. (like 45 for first, and 78 for second textfield)
textField.tag = 45;
2.. suppose your you have added these textField as subView on 'myView'.
UITextField *txtField = (UITextField*)[myView viewWithTag:45];
this line will give you textfield having tag 45, which is added on myView.
Note -- Avoid to use '0' as tagValue for any control because '0' is used as byDefault tagValue for controls.
On UIControlEventEditingDidEndOnExit event for the first textField, call [textFieldTwo becomeFirstResponder].

Adding default text to UITextView

I was wondering how to display a default prompt in a UITextView. If I wanted the user to type a description in a text view, the UITextView could have "description" printed in it, and when the user starts to type, it disappears.
For UITextField, there is the placeholderText property, which will display a grayed out text that is removed once the user starts typing.
For UITextView, you can use a custom implementation, such as SZTextView, which implements a similar functionality of a placeholder text.
It wont be a wise idea to use a third party uitextview for placeholder property.
Follow these steps and you will achieve what you need-
set- textview.textcolor=[uicolor greycolor];
textview.text=#"Your initial placeholder text";
Now in -textViewShouldBeginEditing write these lines of codes-
if(textview.color==[uicolor greycolor]){
textview.color=[uicolor blackcolor];
textview.text=#"";
}
Cheers.
The below solution from #svmrajesh isn't complete. You still need to implement an auto-delete functionality so the default text deletes as soon as the user selects the textView.
In my implementation I set the text in the UITextView to lightGrayColor initially so that it looks like default text
[textView setTextColor:[UIColor lightGrayColor]];
Then in the header file I implement the UITextViewDelegate.
#interface YourViewController <UITextViewDelegate>
Then I set the UITextView delegate to self.
[textView setDelegate:self];
Then simply I implement the following delegate method which is fired when the user selects the textView to start typing in their text. The first thing it does is to check if the text color is still set to lightGray.
If it is then the default text is still being displayed, so it is deleted and the textColor is set to black. This simple solution works well for me.
-(void)textViewDidBeginEditing:(UITextView *)textView
{
if(textView.textColor == [UIColor lightGrayColor])
{
textView.textColor = [UIColor blackColor];
textView.text = #"";
}
}
Try this....
For UITextView :
UITextView *myUITextView = [[UITextView alloc] init];
myUITextView.delegate = self;
myUITextView.text = #"placeholder text here...";
For UITextField :
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 200)];
textField.placeholderText = #"Enter your text here";
[self.view addSubview textField];

Show datepicker on textfield tap

I'm very new to iOS so obviously I'm missing a few concepts when trying to follow this solution Display datepicker on tapping on textfield to my problem.
I have the interface and implementation in a class called DatepickerAppDelegate, but I'm lost here:
"In the XIB, Pull out a UIToolbar and a UIDatePicker but don't attach it to the view. Connect the outlets appropriately. dateChanged: responds to changes in the date picker and doneEditing: is called when the Done button in the tool bar is clicked. Connect them too. The methods are implemented as listed below."
I read something about XIB's but I'm using a storyboard, so I think I don't have then. Also, I'm not sure how I'm supposed to pull out elements without attaching it to my view controllers (I'm trying, but when I release the mouse button, the tool flies back to the toolbar), and I don't even understand why I need a UIToolbar.
And finally, how do I connect my textfield to the datepicker delegate?
Can someone help me?
UPDATE:
In fact it is very simple, I just need to:
datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
self.birthday.inputView = datePicker;
UIResponder, from which UITextField inherits, defines a property called inputView. This property (a UIView *) can define a view that will be displayed instead of the keyboard when that UIResponder becomes the first responder.
Thus, if you want a UIDatePicker to appear when you tap in a textField (ie, you make that textField the first responder), then you can say, naïvely:
UIDatePicker *datePicker = [[UIDatePicker alloc] init...];
... configure datePicker ...
[myTextField setInputView:datePicker];
Now, when your UITextField is tapped, a UIDatePicker will be brought up instead.
HOWEVER
You'll want to do more than this, because you'll need to handle different orientations and different idioms.
But that's the basic idea.
(And if you want a toolbar above the UIDatePicker, that's what the inputAccessoryView property is for...)
In viewDidLoad of ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
UIDatePicker *datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(dateTextField:) forControlEvents:UIControlEventValueChanged];
[txtFieldBranchYear setInputView:datePicker];
}
Add one method in your ViewController
-(void) dateTextField:(id)sender
{
UIDatePicker *picker = (UIDatePicker*)txtFieldBranchYear.inputView;
[picker setMaximumDate:[NSDate date]];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSDate *eventDate = picker.date;
[dateFormat setDateFormat:#"dd/MM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:eventDate];
txtFieldBranchYear.text = [NSString stringWithFormat:#"%#",dateString];
}
txtFieldBranchYear is an outlet of UITextField
Add a datepicker view and uitoolbarview to your view.
For example
CGFloat toolbarHeight = 44.f;
CGFloat datePickerHeight = 140.f;
CGFloat containerViewHeight = toolbarHeight+datePickerHeight;
//create container view for datepicker and toolbar
UIView *containerView = [[UIVIew alloc] initWithFrame:CGRectMake(0.f, [self.view bounds].size.height+containerViewHeight, [self.view bounds].size.width, containerViewHeight)];
//after init toolbar set the frame
toolBar.frame = CGRectMake(0.f,0.f,containerView.frame.size.width, toolbarHeight];
//then add on toolbar button save and release it after add
//after init datePicker set the frame
datePicker.frame = CGRectMake(0.f, toolbarHeight, containerView.frame.size.width, datePickerHeight);
//add datePicker and toolBar to containerView
[containerView addSubview: toolBar];
[containerView addSubview: datePicker];
[toolBar release];
[datePicker release];
//add containerView to main view
[self.view addSubview: containerView];
[containerView release];
So when view did load they are will be hidden Y of contener view is outside of the your view frame.
Now you should assign property delegate of UITextFieldDelegate
textfield.delegate = self;
In the method of delegate textFieldDidBeginEditing see UITextFieldDelegate you should fire method that will show your datePicker and toolbar.
If you have many textFields to define which is was selected use tag property, which is integer and you can use switch instead of many if conditions.
To show your datePicker and toolbar you should change the Y of containerView frame.
Here is how to do that
CGRect containerViewFrame = containerView.frame;
containerViewFrame.y -=containerViewHeight;
containerView.frame = containerViewFrame;
To hide just plus the containerViewHeight variable.
You can use UIViewAnimations to show it with some animation. For example UIViewAnimationCurveEaseInOut

Resources