UITextField Text Does Not Scroll Left

I have a subclass of UITextField where I redefined some methods (see code below). The problem is that when I enter it into the text and the text reaches the limit, it will no longer show what I am typing. In debug mode, I see that the UIFieldEditor much wider than the text.

Here is the subclass code of UITextFiled:

 - (instancetype)init { self = [super init]; if (self) { self.edgeInsets = UIEdgeInsetsMake(7, 0, 10, 15); } return self; } - (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self.edgeInsets = UIEdgeInsetsMake(7, 0, 10, 15); } return self; } -(id)initWithCoder:(NSCoder *)aDecoder{ self = [super initWithCoder:aDecoder]; if(self){ self.edgeInsets = UIEdgeInsetsMake(7, 0, 10, 15); } return self; } - (CGRect)textRectForBounds:(CGRect)bounds { return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, self.edgeInsets)]; } - (CGRect)editingRectForBounds:(CGRect)bounds { return [super editingRectForBounds:UIEdgeInsetsInsetRect(bounds, self.edgeInsets)]; } 

Here is the code in which I use this class:

 // alloc / init _myTextField.delegate = self; _myTextField.backgroundColor = [UIColor whiteColor]; _myTextField.layer.masksToBounds = YES; _myTextField.layer.cornerRadius = 3.0; _myTextField.returnKeyType = UIReturnKeyDone; _myTextField.clearButtonMode = UITextFieldViewModeWhileEditing; _myTextField.autocapitalizationType = UITextAutocapitalizationTypeWords; _myTextField.autocorrectionType = UITextAutocorrectionTypeNo; _myTextField.hidden = YES; _tfButton = [UIButton buttonWithType:UIButtonTypeSystem]; [_tfButton setImage:[UIImage imageNamed:@"ic_edit"] forState:UIControlStateNormal]; [self fs_addSubview:_tfButton]; [_tfButton constrainWidth:22.]; [_tfButton setTintColor:[UIColor greenColor]]; [_tfButton constrainHeight:22.]; [_tfButton constrainToRightOfView:_myTextField margin:-25.0]; [_tfButton constrainEqualCenterYWithView:_myTextField offset:0.0]; [_tfButton setUserInteractionEnabled:NO]; UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)]; _myTextField.leftView = leftView; _myTextField.leftViewMode = UITextFieldViewModeAlways; 

How to make the text scroll on the left side when the user enters a large string in this text box?

+7
ios uitextfield
source share
5 answers

Your code is completely right. The problem arose from the UITextField class. When you fell into a UITextField in your storyboard and set its fontSize and minFontSize to 50. This system textField will have the same problem as your custom text filter.

This is simply because UITextField does not know how to draw a large font in narrow space.

I used your subclass of textField (with a default height of 30), after I changed the font size to 7, everything works fine.

+6
source share

I ran into the same problem, but I did not have a subclass of UITextField. The problem is that the text box has a certain relationship between the height of the text and the font size. Increasing the font size above a certain wrt height of the text field caused this problem. I slightly reduced the font size and the text box worked again.

+2
source share

I have another solution. It will apparently work if you check the "Adjust to Fit" option below the minimum font size (on the storyboard). Somehow the text will be shortened if you type more than the right margin.

Or with the code:

 self.textField.minimumFontSize = 12; self.textField.adjustsFontSizeToFitWidth = YES; 
+2
source share

Reducing the font size from 18 to 16 fixed it for me. I do not know why and do not care: D

 searchTF = [UITextField new]; searchTF.frame = CGRectMake(10, 10, w-140, 20); searchTF.backgroundColor = [UIColor redColor]; searchTF.font = [UIFont fontWithName:@"HelveticaNeue" size:18.0f]; 

to

 searchTF.font = [UIFont fontWithName:@"HelveticaNeue" size:16.0f]; 
0
source share

With a little coloring, this is what I get after trying to reproduce your code, and it looks fine to me. Could you clarify the expected behavior?

enter image description here

-one
source share

All Articles