Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextField text not scrolling off on the left side

I have a UITextField subclass where I have overridden some methods (see code below). The problem is that when I type in it and the text reaches the rigth margin it will no longer show what I am typing. In view debug mode I see that the UIFieldEditor is much wider that the text filed.

Here is the UITextFiled subclass code:

- (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 where 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 do I get the text to scroll off on the left side when the user is typing a large string in this text field?

like image 306
user426132 Avatar asked Sep 04 '15 08:09

user426132


3 Answers

I was facing the same issue, but I didn't have any UITextField subclass. The problem is that textfield has certain ratio between textfield height and font size. Increasing the font size above certain value w.r.t textfield height caused this issue. What I did is decreased the font size a bit and textfield started working again.

like image 137
manish_kumar Avatar answered Nov 06 '22 21:11

manish_kumar


Your code is completely right. The problem came from the UITextField class. When you dropped in a UITextField in your storyboard, and set it's fontSize and minFontSize to 50. This system textField will have the same problem as your custom textField.

It's just because UITextField don't know how to draw big font in a narrow space.

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

like image 11
wj2061 Avatar answered Nov 06 '22 19:11

wj2061


I have another solution. It's apparently will work if you ticked "Adjust to fit" option below minimum font size (at Storyboard). Somehow the text will shrunk if typed more than right margin.

Or with code:

self.textField.minimumFontSize = 12;
self.textField.adjustsFontSizeToFitWidth = YES;
like image 2
Ryde Avatar answered Nov 06 '22 20:11

Ryde