Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextField subclass messing adjustFontSizeToWidth

I've created a subclassed textfield view inside my view controller, with the font size to 15, the AdjustFontSizeToWidth enabled and the minimum font size to 10. My textfield has a placeholder text too big to fit the view. The adjust font size to width is activated and the system is reducing the size of the font to 14. I don't exactly know why 14, because the placeholder still doesn't fit (see screenshot) Any idea why is this happening ? Did i missed something ? I subclassed UITextfield and override the following methods (Not sure it is relevant, but this seem to be methods who may cause this bugs) :

- (CGRect)leftViewRectForBounds:(CGRect)bounds {
CGRect leftViewRect = [super leftViewRectForBounds:bounds];
if ([self shouldShowLeftView] == YES) {
    CGFloat newHeight = self.frame.size.height * 0.5f;
    if (self.leftImage) {
        leftViewRect.size.width = newHeight;
    }
    leftViewRect.origin.x = kMargins;
    leftViewRect.origin.y =  (self.frame.size.height - leftViewRect.size.height) / 2.0f;
 }
 return leftViewRect;
}


- (CGRect)textRectForBounds:(CGRect)bounds {
CGRect rectForBounds = [super textRectForBounds:bounds];

if ([self shouldShowLeftView] == YES) {
    rectForBounds.origin.x += kMargins;
    rectForBounds.size.width -= 2.0 * kMargins;
} else {
    rectForBounds.origin.x += kMargins / 2.0;
    rectForBounds.size.width -= 2.0 * (kMargins / 2.0);
}
return rectForBounds;
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
CGRect rectForBounds = [super editingRectForBounds:bounds];

if ([self shouldShowLeftView] == YES) {
    rectForBounds.origin.x += kMargins;
    rectForBounds.size.width -= 2.0 * kMargins;
} else {
    rectForBounds.origin.x +=  kMargins / 2.0;
    rectForBounds.size.width -= 2.0 * (kMargins / 2.0);
}
 return rectForBounds;
}

placeholder not fitting in the screen

EDIT: Update, I tried to use this solution and adapted the code a bit to make it work for the placeholder also. The font resizing is fine, but the UI is messed up when the text is reduced, the text offset itself to the left, behind the left image view. On the screenshot, we can see that the right margin is too big and the text too much on the left. Screenshot fail new try

like image 307
Loadex Avatar asked Nov 10 '22 11:11

Loadex


1 Answers

You aren't missing anything - this is a quirk of iOS. minimumFontSize values less than 14 seem to be completely ignored as per my answer to a similar question.

Set your original font size to 300 and the UITextField will still be 14 pt, not 10 pt.

14 is an odd magic number; undocumented, but probably used to maintain readability. If only it could be lower.

like image 68
Corwin Newall Avatar answered Nov 15 '22 11:11

Corwin Newall