Can't get UITextField to autoshrink text

后端 未结 11 792
逝去的感伤
逝去的感伤 2020-12-05 14:25

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 t

相关标签:
11条回答
  • 2020-12-05 14:53

    Swift 3.0 way to resize the font to fit:

    let startFontSize = 14.0
    let minFontSize = 7.0
    
    func resizeFont() {
        guard let font = self.font, let text = self.text else {
            return
        }
    
        let textBounds = self.textRect(forBounds: self.bounds)
        let maxWidth = textBounds.size.width
    
        for fontSize in stride(from: startFontSize, through: minFontSize, by: -0.5) {
            let size = (text as NSString).size(attributes: [NSFontAttributeName: font.withSize(CGFloat(fontSize))])
            self.font = font.withSize(CGFloat(fontSize))
            if size.width <= maxWidth {
                break
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-05 14:54

    Try this its working for me (swift 3.0)

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
            // bla bla ... anything inside method you want to do
            // reset font size of textfield
            textField.font = UIFont.systemFont(ofSize: CGFloat(15.0))
            var widthOfText: CGFloat = textField.text!.size(attributes: [NSFontAttributeName: textField.font!]).width
            var widthOfFrame: CGFloat = textField.frame.size.width
            // decrease font size until it fits (25 is constant that works for me)
            while (widthOfFrame - 25) < widthOfText {
                let fontSize: CGFloat = textField.font!.pointSize
                textField.font = textField.font?.withSize(CGFloat(fontSize - 0.5))
                widthOfText = (textField.text?.size(attributes: [NSFontAttributeName: textField.font!]).width)!
                widthOfFrame = textField.frame.size.width
            }
            return true
        }
    
    0 讨论(0)
  • 2020-12-05 14:56

    I had the same problem. The UITextField stoped shrinking the text when it was too long, but instead it resized itself and grew outside its 'bounds'.

    The solution that helped me was to set width constraint on given UITextField. After that it did not grew anymore, instead the text inside got smaller as intended. (Of course you have to set minFontSize and check the "adjust to fit" box in storyboard.)

    I know it's kind of a late, but if anyone else will find this question via google as I did...it might just help.

    0 讨论(0)
  • 2020-12-05 14:58
    -(BOOL)sizeFontToFit:(NSString*)aString minSize:(float)aMinFontSize maxSize:(float)aMaxFontSize 
    {   
    float fudgeFactor = 16.0;
    float fontSize = aMaxFontSize;
    
    self.font = [self.font fontWithSize:fontSize];
    
    CGSize tallerSize = CGSizeMake(self.frame.size.width-fudgeFactor,kMaxFieldHeight);
    CGSize stringSize = [aString sizeWithFont:self.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];
    
    while (stringSize.height >= self.frame.size.height)
           {
           if (fontSize <= aMinFontSize) // it just won't fit
               return NO;
    
           fontSize -= 1.0;
           self.font = [self.font fontWithSize:fontSize];
           tallerSize = CGSizeMake(self.frame.size.width-fudgeFactor,kMaxFieldHeight);
           stringSize = [aString sizeWithFont:self.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];
           }
    
    return YES; 
    }
    
    0 讨论(0)
  • 2020-12-05 14:58

    My humble solution for this problem was this.... (find the proper size of font on my own - to fit the size of frame) I made it inside delegate method of shouldChangeCharactersInRange:replacementString:

    -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    
     {
    
        // bla bla ... anything inside method you want to do
    
        // reset font size of textfield
        textField.font = [FONT_PROXY fontNormalOfSizeNormal];
    
        CGFloat widthOfText = [textField.text sizeWithAttributes:@{NSFontAttributeName:textField.font}].width;
        CGFloat widthOfFrame = textField.frame.size.width;
    
        // decrease font size until it fits (25 is constant that works for me)
        while((widthOfFrame - 25) < widthOfText){
            CGFloat fontSize = textField.font.pointSize;
            textField.font = [textField.font fontWithSize:fontSize - 0.5f];
             widthOfText = [textField.text sizeWithAttributes:@{NSFontAttributeName:textField.font}].width;
             widthOfFrame = textField.frame.size.width;
        }
    
    
      }
    
    0 讨论(0)
  • 2020-12-05 15:05

    Can be solved by selecting Line Breaks = Clip in IB

    0 讨论(0)
提交回复
热议问题