Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextField doesn't scroll to the end after implementing textRectForBounds and editingRectForBounds in a subclass?

Tags:

I have a UITextField subclass where I am implementing the methods below:

- (CGRect)textRectForBounds:(CGRect)bounds {

  return CGRectInset(bounds , 30, 7);
}

-(CGRect) editingRectForBounds:(CGRect)bounds {
  return CGRectInset(bounds , 30, 7);
}

Because of these two methods, the UITextField doesn't scroll to the end when the text entered gets bigger than the width of UITextField. Is there any solution to this?

like image 885
Ashish Awaghad Avatar asked Sep 24 '13 15:09

Ashish Awaghad


2 Answers

Its not working just because of the font size of the text you have declared in UITextField and the CGRectInset you are applying. Actually it should be as per the font size you are setting. For example, for font size of 14.0, just change your method as below:

- (CGRect)textRectForBounds:(CGRect)bounds {
    return CGRectInset(bounds , 30, 6);
}
-(CGRect) editingRectForBounds:(CGRect)bounds {
    return CGRectInset(bounds , 30, 6);
}

The reason is that the drawing rectangle of the text. For example, when we set font size of UITextField as 14.0 then it requires at least a height of 18.0 to adjust the text in the frame. So it needs 4 pixel extra around the text, you can say it overlay frame. Due to which, in your case, this condition is not satisfying because as far as I guess you have adjusted the font size to 14.0 and as by default frame height of UITextField is 30.0 and your edge masking is of 7.0 each side that means total 14.0. So your rect for text returns 16.0 which in turn blocks the OS from updating the text frame because it is outside of the graphics context. I hope you are clear. If it resolves your issue then please accept my answer. More good answer will be as follows:

- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectInset(bounds , 30, (30 - self.font.pointSize - 4)/2);
}

-(CGRect) editingRectForBounds:(CGRect)bounds {
    return CGRectInset(bounds , 30, (30 - self.font.pointSize - 4)/2);
}

Enjoy!

like image 157
if-else-switch Avatar answered Oct 21 '22 02:10

if-else-switch


This is probably a conflict between your custom UITextField CALayer height with insets and it's font size.

You may try one of these adjusts:

  • Decrease font size;
  • Increase the UITextField height on Storyboard or programmatically in your subclass like:

    - (void)drawRect:(CGRect)rect {
        CGRect frameRect = self.frame;
        frameRect.size.height = 35 //specify the height you want;
        self.frame = frameRect;
     }
    
  • Decrease the y-coordinate value on CGRectInset;

  • Or use UIEdgeInsets to have more control in setting values ​​of each position. Something like:

    - (CGRect)textRectForBounds:(CGRect)bounds{
        UIEdgeInsets contentInsets = UIEdgeInsetsMake(5, 30, 5, 30);
        return UIEdgeInsetsInsetRect(bounds, contentInsets);
    }
    
    - (CGRect)editingRectForBounds:(CGRect)bounds{
        UIEdgeInsets contentInsets = UIEdgeInsetsMake(5, 30, 5, 30);
        return UIEdgeInsetsInsetRect(bounds, contentInsets);
    }
    
like image 43
Douglas Ferreira Avatar answered Oct 21 '22 02:10

Douglas Ferreira