Resize font size to fill UITextView?

后端 未结 15 933
春和景丽
春和景丽 2020-12-01 07:03

How would I set the font size of text in a UITextView such that it fills the entire UITextView? I\'d like the user to type in their text, then have the text fill the entire

相关标签:
15条回答
  • 2020-12-01 07:30

    Here is a conversion of dementiazz's and Matt Frear's answer to Swift 5. Put this function into viewDidLayoutSubviews, referencing the UITextView's who's font you want to dynamically resize.

    func updateTextFont(textView: UITextView) {
        if (textView.text.isEmpty || textView.bounds.size.equalTo(CGSize.zero)) {
            return;
        }
    
        let textViewSize = textView.frame.size;
        let fixedWidth = textViewSize.width;
        let expectSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat(MAXFLOAT)))
    
        var expectFont = textView.font;
        if (expectSize.height > textViewSize.height) {
            while (textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat(MAXFLOAT))).height > textViewSize.height) {
                expectFont = textView.font!.withSize(textView.font!.pointSize - 1)
                textView.font = expectFont
            }
        }
        else {
            while (textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat(MAXFLOAT))).height < textViewSize.height) {
                expectFont = textView.font;
                textView.font = textView.font!.withSize(textView.font!.pointSize + 1)
            }
            textView.font = expectFont;
        }
    }
    
    0 讨论(0)
  • 2020-12-01 07:30

    Here is a solution for Swift 4 (based on Arie Litovsky answer), you can put this code into textViewDidChange(_ textView: UITextView) delegate method :

    Scaling down the font :

        while (textView.contentSize.height > textView.frame.size.height) {
            guard let fontName = textView.font?.fontName, let fontSize = textView.font?.pointSize else {return}
            if fontSize < 12 {
                return
            }
            textView.font = UIFont(name: fontName, size: fontSize - 1)
            textView.layoutIfNeeded()
        }
    

    And scaling up the font:

            while (textView.contentSize.height < textView.frame.size.height) {
            guard let fontName = textView.font?.fontName, let fontSize = textView.font?.pointSize else {return}
            if fontSize > 15 {
                return
            }
            textView.font = UIFont(name: fontName, size: fontSize + 1)
            textView.layoutIfNeeded()
        }
    

    You can change the fontSize < 12 and fontSize > 15 to fit your needs in minimum and maximum font size.

    0 讨论(0)
  • 2020-12-01 07:31

    This, in a category on UITextView, should do the trick. For why you need the fudgefactor, see this post

    #define kMaxFieldHeight 9999 
    
    -(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-01 07:31

    This property is only available on UITextFields. To do this in a UITextView, you'd have to constantly watch the text and manually adjust the font size as that changed.

    0 讨论(0)
  • 2020-12-01 07:31

    An updated version of @Arie Litovsky's code. This works in iOS7 and later and stretches the font size both up and down so the text fits.

    - (void) stretchFontToFit:(UITextView*)textView
    {
    
        while( textView.contentSize.height < textView.frame.size.height )
        {
            textView.font = [textView.font fontWithSize:textView.font.pointSize +1];
            [textView layoutIfNeeded];
        }
    
        while( textView.contentSize.height > textView.frame.size.height )
        {
            textView.font = [textView.font fontWithSize:textView.font.pointSize -1];
            [textView layoutIfNeeded];
        }
    }
    
    0 讨论(0)
  • 2020-12-01 07:34

    I have converted dementiazz's & Matt Frear's answer to Swift 3:

    func updateTextFont() {
            if (textView.text.isEmpty || textView.bounds.size.equalTo(CGSize.zero)) {
                return
            }
    
        let textViewSize = textView.frame.size
        let fixedWidth = textViewSize.width
        let expectSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat(MAXFLOAT)))
    
        var expectFont = textView.font
        if (expectSize.height > textViewSize.height) {
            while (textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat(MAXFLOAT))).height > textViewSize.height) {
                expectFont = textView.font!.withSize(textView.font!.pointSize - 1)
                textView.font = expectFont
            }
        }
        else {
            while (textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat(MAXFLOAT))).height < textViewSize.height) {
                expectFont = textView.font
                textView.font = textView.font!.withSize(textView.font!.pointSize + 1)
            }
            textView.font = expectFont
        }
    }
    
    0 讨论(0)
提交回复
热议问题