Text in a UITextField moves up after editing (during editing)

I have a strange problem. I have a UITextField in which the user must write the amount of something, so the field is called "amountField". Everything looks great when the user starts editing a text field, the text is in the vertical and horizontal center - that's great.

However, when the user finishes editing, the text rises a little. I tried a lot, nothing helped ...

I am adding screenshots below so you can understand what the problem is.

This is how it looks when editing a field - this is normal.

This is while editing the field - that's ok.

And this is how it looks when editing - this is a problem !

This is the problem.

Please, if anyone knows what might cause this, I will be very grateful! :)

Here are some of my code related to the number of fields.

amountField.keyboardType = UIKeyboardTypeNumberPad; amountField.returnKeyType = UIReturnKeyDone; amountField.delegate = self; [amountField setFont:[UIFont fontWithName:@"Nuptial Script LT Std" size:30]]; amountField.borderStyle = UITextBorderStyleNone; UIImage *amountBg = [UIImage imageNamed:@"skin2_ipad_amountField.png"]; [amountField setBackground:amountBg]; amountField.rightView = nil; //amountField.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.2]; amountField.textAlignment = UITextAlignmentCenter; amountField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; amountField.adjustsFontSizeToFitWidth = YES; amountLabel.textColor = UIColorFromARGB(0x313030); //Using my own macro amountField.frame = CGRectMake(300, 480, 136, 32); amountField.center = CGPointMake(605, 439); 

PS: These white corners are, because I set the background to white with 0.2 alpha, which is good.

+45
ios objective-c iphone uitextfield xcode
Mar 12 '12 at 20:45
source share
12 answers

So...

After many hours of trying a lot - I found a problem. In my case, the problem is the font. I really don't know why, but the font author made the font weird (leading, etc.), It has an empty space at the bottom. I don’t know why, but when you edit the text, all the text properties are ignored, but after the editing is completed they are applied.

So, if you have a similar problem, try changing the font to Arial or something similar.

For a complete explanation, refer to the following links: link 1 and link 2 . The solution recommended in these links can avoid a lot of headaches and can even be applied to fix the problem, like text moving up when you start editing a UITextField (using a system font or other special fonts).

+23
Mar 14 2018-12-12T00:
source share

I had a similar problem that started with iOS 9 . Basically, I have a UITextField in the collection view cell. Sometimes, when the user finishes entering and editing, the text “bounces” up, and then returns to the correct position again. Very weird and annoying glitch. A simple fix for this problem was fixed on iOS 9 and turned out to be safe for iOS 7 and 8:

  - (void)textFieldDidEndEditing:(UITextField *)textField { [textField layoutIfNeeded]; //Fixes iOS 9 text bounce glitch //...other stuff } 
+50
Oct 07 '15 at 15:35
source share

Disabling ClipsToBounds for TextField allowed this for me.

+6
Nov 22 '16 at 3:44
source share

I deal with this problem almost every time I design an application with a custom font. One option is to fix the font (but this is too much work - at least for me :)). The second option I'm using is to subclass UITextField and override the editing methods RectForBounds: and placeholderRectForBounds: and fix the offset. It should also work for your business.

 @implementation MyTextFieldWithFixedFontPosition -(CGRect)editingRectForBounds:(CGRect)bounds{ return CGRectOffset([self textRectForBounds:bounds], 0, 0.5); //0.5 is just example, you can adjust to any offset you like } -(CGRect)placeholderRectForBounds:(CGRect)bounds{ return [self editingRectForBounds:bounds]; } @end 

I have not tested it with leftView or rightView, so be careful when using them :)

NOTE. This "font-dependent" approach, the values ​​used for offset may vary for each font and size

+2
Sep 24 '14 at 15:03
source share

This error occurred to me when I set the text and became the first responder in viewDidLoad or viewWillAppear . When I moved the becomeFirstResponder code to viewDidAppear , the error viewDidAppear away.

+2
Apr 24 '15 at 21:08
source share

I had similar problems with UITextfield built into UITableViewCell . Where exactly is this code located in your project? I believe that after you have finished editing a specific text field, it sends itself -setNeedsDisplay and subsequently calls drawRect: . This may explain the shift in alignment. In my specific scenario, I had to use the table delegate method -willDisplayCell ... to set the content alignment. I would have to learn more about your design in order to offer an offer.

One possible solution would be to use text field delegate methods to set content alignment.

 -(void)textFieldDidEndEditing:(UITextField *)textField{ if (amountField == textField){ amountField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; } } 
0
Mar 14 2018-12-12T00:
source share

I was unable to change the font file, so when I solved this, I saved the original UITextField frame in the property and applied the following code:

 - (void)textFieldDidBeginEditing:(UITextField *)textField { textField.frame = self.usernameFrame; } - (void)textFieldDidEndEditing:(UITextField *)textField { textField.frame = CGRectOffset(self.usernameFrame, 0, 1); } 

It's a bit hacky, but it does its job.

0
Sep 06 '13 at 21:38 on
source share

There is a crash in iOS 8.1 and below, I don’t know if they will fix it later, but at that time there is no unique solution that fixes all cases, because error and solutions are a font type depending on the size.

One of these solutions, or a combination of these solutions below may solve your problem:

  • Change the font size.

  • Change the font type.

  • Resize UITextField.

  • Decompiling the font in question, changing the characteristics of the font and recompiling it (for further clarification, refer to the following links: link 1 and link 2 ).

Otherwise, this other self-sufficient solution below may solve your problem:

Quick version

 import UIKit class CustomTextField: UITextField { ... override func textRectForBounds(bounds: CGRect) -> CGRect { // Possible values. return CGRectInset(bounds, CGFloat(35.0), CGFloat(0.0)) } override func editingRectForBounds(bounds: CGRect) -> CGRect { // Possible values. return CGRectInset(bounds, CGFloat(35.0), CGFloat(0.0)) } override func placeholderRectForBounds(bounds: CGRect) -> CGRect { // Possible values. return CGRectInset(bounds, CGFloat(35.0), CGFloat(0.0)) } } 

This solution has been tested with leftView and works like a charm.

NOTE: this font-dependent approach, the values ​​used for CGRectInset may vary for each font and size.

0
Dec 23 '14 at 9:33
source share

Check the keyboard to change the position of the popup if there is a line of code self.view.layoutIfNeeded() Delete it. Good luck

0
Feb 02 '16 at 4:01
source share

I fixed this by adding height limits to UITextFields.

0
May 18 '16 at 17:31
source share

This is because the BaselineOffset for the text field has changed. In UITextFieldDidEndEditing creating an attribute with NSBaselineOffset: 0 and using this attributedText will fix the problem.

 -(IBAction)txtFieldDidEndEditing:(UITextField *)sender { NSDictionary *style = @{ NSBaselineOffsetAttributeName: @(0) }; self.txtField.attributedText = [[NSAttributedString alloc] initWithString:self.txtField.text attributes:style]; } 
0
Jun 06 '16 at 18:11
source share

This solution above does not work for me. My solution is to subclass UITextField and override setText:

 - (void) setText:(NSString *)text { [super setText:text]; [self layoutIfNeeded]; } 
0
Aug 18 '17 at 6:05
source share



All Articles