UITextField subclass messing adjustFontSizeToWidth

I created a subclassed text box inside my view controller with a font size of 15 , enabled with the AdjustFontSizeToWidth parameter and a minimum size of up to 10 . My text box has placeholder text too large to fit the view. The font size is changed to width, and the system reduces the font size to 14 . I don't know why 14 because the placeholder still doesn't fit (see screenshot) Any idea why this is happening? Did I miss something? I subclassed UITextfield and redefined the following methods (not sure if this is relevant, but it looks like methods that might cause such errors):

- (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 too. Changing the font size is fine, but the user interface is ruined when the text is reduced, the text is shifted to the left, behind the left image. In the screenshot, we see that the right edge is too large, and the text is too much on the left. Screenshot fail new try

+5
source share
1 answer

You are missing nothing - this is the quirk of iOS. minimumFontSize values ​​less than 14 seem to be completely ignored in my answer to a similar question.

Set your initial 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 ensure readability. If only it could be lower.

+1
source

All Articles