UITextView alignment is changing on its own - iphone

I have a UITextView with RTL alignment.
If the user enters text, and dismisses keyboard, the alignment remains - RTL.
But if the user chooses empty string, I change the string to the place holder, and the alignment flips to be LTR.
I tried to explicitly ask for RTL, but it didn't help.
-(void)cancelPad{
[userTextView resignFirstResponder];
userTextView.textAlignment = UITextAlignmentRight;
userTextView.text = #"place holder text";
}
-(void)doneWithNumberPad{
if ([userTextView.text isEqualToString:#""]) {
[self cancelPad];
}
[userTextView resignFirstResponder];
userTextView.textAlignment = UITextAlignmentRight;
}
Somebody have any ideas?

Instead of:
userTextView.text = ""
you should put:
userTextView.text = [NSString stringWithFormat:#"\u202b"];
This is the UTF-8 character for "starting RTL"

Eventually I just added a label with the place holder text.
When the keyboard is shown, the placeholderLabel is hidden.
If the keyboard is dismissed with empty text, the placeholderLabel is shown again.
If there is a more proper solution, do not hesitate to post it here.

first:
in the .h file add this
UIViewController<UITextFieldDelegate>
assign your text field to this
userTextView.delegate =self;
then use this methods and put your code in
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField{
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
}
Example :
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
if ([userTextView.text isEqualToString:#""]) {
[self cancelPad];
}
[userTextView resignFirstResponder];
userTextView.textAlignment = UITextAlignmentRight;
}

Related

Make second UITextField firstresponder after the first UITextField did finish editing and press Done

I have created two UITextFields dynamically, when tapped on a certain CA Layer. I have made the first textfield the first responder, what I want is, when I enter text in first Textfield and press done, I want the second textfield to be the first responder.
n thats what I did for this in textFieldShouldReturn.
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (titleField.returnKeyType== UIReturnKeyDone) {
NSString *title = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (title.length>0 )
{
[self changeLayersTitle:title];
}
[dateField becomeFirstResponder];
if (dateField.returnKeyType==UIReturnKeyDone)
{
NSString *date = [dateField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (date.length>0)
{
[self changeLayersDate:date];
}
}
[dateField resignFirstResponder];
[bgLayer removeFromSuperlayer];
[titleField removeFromSuperview];
[dateField removeFromSuperview];
}
return NO;
}
Now, when I enter press Done after entring text in first one, it dismissed both of the textfields, bt shows me the keyboard.
So, what am I missing or did wrong or do I have to put it somewhere else. please help me out.
I'm not sure why you were comparing the returnKeyType property of the text fields. You should compare the text fields for equality, and if the return key was pressed on the first text field, then make the second text field the new responder.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == titleField) {
[dateField becomeFirstResponder];
}
else if (textField == dateField) {
// Return button pressed on 2nd field. Do something
}
return YES;
}
In your done button callback method, can't you just set your second text field as the first responder?
You don't need to resignFirstResponder on your first text field.

simple question about UITextField and not working secureTextEntry

I have a password field with text "Password" and when user clicks, it gets cleared. Moreover I want to set the type of this textfield as secure but this doesn´t work.
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if ([textField.text isEqualToString:#"Password"] || [textField.text isEqualToString:#"Email"])
{
textField.text = #"";
textField.secureTextEntry = YES;
}
}
I've just had the same problem. I was changing the property from within the textFieldDidBeginEditing: call.
Moving the property change to the textFieldShouldBeginEditing: call fixed the problem. I believe the trick is to change it while the text field isn't the becomeFirstResponder.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if ([textField isEqual:_passwordField]) {
textField.secureTextEntry = YES;
}
return YES;
}
Simply put your text in a placeholder and make your password textfield secure. Your problem will be solved :).
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if ([textField.text isEqualToString:#"Password"] || [textField.text isEqualToString:#"Email"])
{
textField.text = #"";
[textField resignFirstResponder];
textField.secureTextEntry = YES;
[textField becomeFirstResponder];
}
}
it can solve your problem , but it has a big bug. the shit UITextField .
set the property in interface builder or when you initialize textfield remove the line from
-(void)textFieldDidBeginEditing:(UITextField *)textField {
}
I think you should set placeholder text as email or password and pre select the secure text entry from interface builder.That's it...
I realize this is a little old, but in iOS 6 the UITextField "text" is now by default "Attributed" in Interface Builder. Switching this to be "Plain", which is how it was in iOS 5, fixes this problem.
If your keyboard is present, secureTextEntry won't work. Because the UITextInputTraits Protocol doesn't change the textfield if the keyboard shows.
You can dismiss the keyboard, and it will work
[myTextField resignFirstResponder]

UITextfield clear button

How can I clear the field which currently has the cursor placed inside it?
I have tried:
if(textfield.isEditing)
textfield.text = #"";
This works for me, but if I select all 3 fields, and press the 'clear field button' all the fields clear Also instead of isEditing, I have tried to use tags on the UITextfields and do :
if(textfield.tag == 1)
textfield.text = #"";
but this has the same effect.
How can I solve this problem?
try one or both of these...
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.clearsOnBeginEditing = YES;
Then add delegate...
- (BOOL)textFieldShouldClear:(UITextField *)textField {
return YES;
}
Try out this in TextField's Delegate method didBeginEditing:
if ([textfield isFirstResponder]) {
textfield.text = #"";
}
Hope this helps you.

Add 'Done' and 'New line' buttons on keyboard in iPhone application

I have created a window based application with a UITabbarController as the RootViewController.
In one of the tabs, i have provided UITextField and UITextView.
I want to provide two buttons on the keyboard itself:
Done - which will hide the keyboard.
Enter - for new line.
Please post your answer if anybody has some idea how to do it.
For the UITextField you can change the return key to a done key by setting the following:
targetTextField.returnKeyType = UIReturnKeyDone;
However, you won't be able to have a Enter and Done key at the same time without custom addition of views to the keyboard.
Also, to control the done behavior of the keyboard you have to implement a UITextFieldDelegate method:
targetTextField.delegate = self;
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
return YES; //dismisses the keyboard
}
I know you can set the returnKeyType for a UITextView but I'm not sure if you can manipulate the return key behavior.
You have a tutorial on how add subviews to the iPhone keyboard here :
http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/7350-adding-subviews-custimize-keyboard.html
Hope this helps,
Vincent
For some reason return YES; didn't work on its own. that worked for me :
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField.returnKeyType == UIReturnKeyNext) {
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];
}
}
if (textField.returnKeyType == UIReturnKeyDone) {
[textField resignFirstResponder];
}
return YES; //dismisses the keyboard
}

set UITextField as non editable - Objective C

[Number.editable = NO];
[Number resignFirstResponder];
[Password.editable = NO];
[Password resignFirstResponder];
I am getting the error
Request for member 'editable' in something not a structure or union
:S
Thanks
Firstly, the [...] aren't needed if you're not sending a message.
Number.editable = NO;
[Number resignFirstResponder];
Password.editable = NO;
[Password resignFirstResponder];
But this is not the cause of error. The .editable property is only defined for UITextView, not UITextField. You should set the .enabled property for a UITextField (note that a UITextField is a UIControl).
Number.enabled = NO;
...
Also, you can use the delegate methods.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return NO;
}
That would do the trick, I prefer this method over setting textField.enabled = YES when it's likely that the ability to edit will change during the lifecycle of the app.
textField.userInteractionEnabled = NO;
Hope this helps..
Returning NO from shouldChangeCharactersInRange would be better choice because if text is longer than the textfield width, then above solution will give problem, because user won't be able to see all text(i.e. text hidden beyond the text field width)