Can't get UITextField to autoshrink text

ios, iphone, objective-c, uitextfield

Solution

I used the answer posted by @Purva as a starting point to come up with this method that gives the required font size starting at the configured font size, and not to drop below the configured minimum font size. Whereas @Purva tested for the height of the text I required the width to fit. This method can be put in either a category or a subclass of UITextField. I have it in a subclass which also captures the `UITextFieldTextDidChangeNotification`. From this notification handler I call the new method and resize the font if required. I also call it when I am assigning new text to the textfield to make sure it will fit by subclassing `- (void)setText: (NSString*)text`.

In each case, when I call it I am using the following code:

    CGFloat requiredFontSize = self.requiredFontSize;
    if( self.font.pointSize != requiredFontSize )
    {
        self.font = [self.font fontWithSize: requiredFontSize];
    }

Here is the new method:

- (CGFloat)requiredFontSize
{
    const CGRect  textBounds = [self textRectForBounds: self.frame];
    const CGFloat maxWidth   = textBounds.size.width;

    if( _originalFontSize == -1 ) _originalFontSize = self.font.pointSize;

    UIFont* font     = self.font;
    CGFloat fontSize = _originalFontSize;

    BOOL found = NO;
    do
    {
        if( font.pointSize != fontSize )
        {
            font = [font fontWithSize: fontSize];
        }

        CGSize size = [self.text sizeWithFont: font];
        if( size.width <= maxWidth )
        {
            found = YES;
            break;
        }

        fontSize -= 1.0;
        if( fontSize < self.minimumFontSize )
        {
            fontSize = self.minimumFontSize;
            break;
        }

    } while( TRUE );

    return( fontSize );
}

Problem

I have a `UITextField` on a table view cell, and when it's text becomes too long I would like the font size to decrease. I want to make it very clear that I am talking about a `UITextField`, not a `UILabel` or a `UITextView`. The reason I say this is because I have seen this question pop up several times and the answers were all based on `UILabel` instead of `UITextField`. For example, someone asked "I can't get my `UITextField` to autoshrink" and the answer was "make sure it's `numberOfLines` is set to `1`". To the best of my knowledge, a `UITextField` does not even have that property and is a single line control. I have tried: - in IB setting the font to system 14.0, `minFontSize` to 7 and checking the "adjust to fit" box - in code in `tableView:cellForRowAtIndexPath:` ``` ptCell.name.font = [UIFont systemFontOfSize: 14.0]; ptCell.name.adjustsFontSizeToFitWidth = YES; ptCell.name.minimumFontSize = 7.0; ``` but neither of these have worked. By that I mean that instead of the text shrinking it truncates the tail. Does anyone know what I am missing? Presumably this should work because I have seen other questions complaining that it is doing that when the user does not want it to.

Original source

Related problems