调整字体大小以填充UITextView?

时间:2010-01-10 22:50:12

标签: iphone cocoa-touch uikit uitextview font-size

如何在UITextView中设置文本的字体大小,使其填满整个UITextView?我希望用户输入他们的文本,然后让文本填满整个UITextView。

感谢任何帮助!

15 个答案:

答案 0 :(得分:17)

我已将dementiazz's answer转换为Swift:

func updateTextFont() {
    if (textView.text.isEmpty || CGSizeEqualToSize(textView.bounds.size, CGSizeZero)) {
        return;
    }

    let textViewSize = textView.frame.size;
    let fixedWidth = textViewSize.width;
    let expectSize = textView.sizeThatFits(CGSizeMake(fixedWidth, CGFloat(MAXFLOAT)));

    var expectFont = textView.font;
    if (expectSize.height > textViewSize.height) {
        while (textView.sizeThatFits(CGSizeMake(fixedWidth, CGFloat(MAXFLOAT))).height > textViewSize.height) {
            expectFont = textView.font!.fontWithSize(textView.font!.pointSize - 1)
            textView.font = expectFont
        }
    }
    else {
        while (textView.sizeThatFits(CGSizeMake(fixedWidth, CGFloat(MAXFLOAT))).height < textViewSize.height) {
            expectFont = textView.font;
            textView.font = textView.font!.fontWithSize(textView.font!.pointSize + 1)
        }
        textView.font = expectFont;
    }
}

答案 1 :(得分:8)

在UITextView的类别中,这应该可以解决问题。为什么需要fudgefactor,请参阅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; 
}

答案 2 :(得分:7)

与Arie Litovsky的答案类似的方法,但没有子类(或使用类别)而不使用contentSize,但没有为我返回正确的渲染文本高度。在iOS 7上测试:

while (((CGSize) [_textView sizeThatFits:_textView.frame.size]).height > _textView.frame.size.height) {
    _textView.font = [_textView.font fontWithSize:_textView.font.pointSize-1];
}

方法是不断缩小字体大小,直到文本恰好放在文本视图的框架内。

如果你打算在生产中使用它,你仍然需要:

  • 处理即使文字尺寸尽可能小也不适合的情况。
  • 如果您还希望缩放文本向上以适合框架,请使用类似的方法增加字体大小。

答案 3 :(得分:5)

试试这个,它更简单:

while (self.contentSize.height > self.frame.size.height)
{
    self.font = [self.font fontWithSize:self.font.pointSize -1];
}

答案 4 :(得分:3)

这是我的示例代码。基本上,我检查期望大小,以了解字体大小是否需要增加或减少。您需要将UITextViewDelegate添加到您的类中以确保其工作

- (void)updateTextFont:(UITextView *)textView
{
    // Only run if has text, otherwise it will make infinity loop
    if (textView.text.length == 0 || CGSizeEqualToSize(textView.bounds.size, CGSizeZero)) return;

    /*
     - Update textView font size
     If expectHeight > textViewHeight => descrease font size n point until it reach textViewHeight
     If expectHeight < textViewHeight => inscrease font size n point until it reach textViewHeight
     */
    CGSize textViewSize = textView.frame.size;
    CGFloat fixedWidth = textViewSize.width;
    CGSize expectSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];

    UIFont *expectFont = textView.font;
    if (expectSize.height > textViewSize.height) {
        while ([textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)].height > textViewSize.height) {
            expectFont = [textView.font fontWithSize:(textView.font.pointSize-1)];
            textView.font = expectFont;
        }
    } else {
        while ([textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)].height < textViewSize.height) {
            expectFont = textView.font;
            textView.font = [textView.font fontWithSize:(textView.font.pointSize+1)];
        }
        textView.font = expectFont;
    }
}

答案 5 :(得分:3)

我已经改变了痴呆症&amp; amp; Matt Frear对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
    }
}

答案 6 :(得分:3)

for swift 3

    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
     }
 }

答案 7 :(得分:2)

此属性仅适用于UITextFields。要在UITextView中执行此操作,您必须不断观看文本并手动调整更改后的字体大小。

答案 8 :(得分:1)

我已将Matt Frear对Swift 4.1的回答转换为UITextView的扩展名:

extension UITextView {
func updateTextFont() {
    if (self.text.isEmpty || self.bounds.size.equalTo(CGSize.zero)) {
        return;
    }

    let textViewSize = self.frame.size;
    let fixedWidth = textViewSize.width;
    let expectSize = self.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat(MAXFLOAT)))


    var expectFont = self.font
    if (expectSize.height > textViewSize.height) {

        while (self.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat(MAXFLOAT))).height > textViewSize.height) {
            expectFont = self.font!.withSize(self.font!.pointSize - 1)
            self.font = expectFont
        }
    }
    else {
        while (self.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat(MAXFLOAT))).height < textViewSize.height) {
            expectFont = self.font
            self.font = self.font!.withSize(self.font!.pointSize + 1)
        }
        self.font = expectFont
    }
}

}

答案 9 :(得分:0)

@Arie Litovsky代码的更新版本。这适用于iOS7及更高版本,并且可以向上和向下拉伸字体大小,以便文本适合。

- (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];
    }
}

答案 10 :(得分:0)

在viewDidLoad中:

textView.textContainer.lineFragmentPadding = 0;
textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);

您需要添加UITextViewDelegate:

- (void)updateTextFont:(UITextView *)textView {

    CGSize textViewSize = textView.frame.size;
    CGSize sizeOfText = [textView.text sizeWithAttributes:@{NSFontAttributeName: textView.font}];

    CGFloat fontOfWidth = floorf(textView.font.pointSize / sizeOfText.height * textViewSize.height);
    CGFloat fontOfHeight = floorf(textView.font.pointSize / sizeOfText.width * textViewSize.width);

    textView.font = [textView.font fontWithSize:MIN(fontOfHeight, fontOfWidth)];
}

答案 11 :(得分:0)

这里是对dementiazz'sMatt Frear's的回答,它们是Swift 5的转换。将此函数放入viewDidLayoutSubviews中,引用要动态调整大小的UITextView的字体。

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;
    }
}

答案 12 :(得分:0)

希望更清楚:

extension UITextView {
    @objc public func fitText() {
        fitText(into: frame.size)
    }

    @objc public func fitText(into originalSize: CGSize) {
        let originalWidth = originalSize.width
        let expectedSize = sizeThatFits(CGSize(width: originalWidth, height: CGFloat(MAXFLOAT)))

        if expectedSize.height > originalSize.height {
            while let font = self.font, sizeThatFits(CGSize(width: originalWidth, height: CGFloat(MAXFLOAT))).height > originalSize.height {
                self.font = font.withSize(font.pointSize - 1)
            }
        } else {
            var previousFont = self.font
            while let font = self.font, sizeThatFits(CGSize(width: originalWidth, height: CGFloat(MAXFLOAT))).height < originalSize.height {
                previousFont = font
                self.font = font.withSize(font.pointSize + 1)
            }
            self.font = previousFont
        }
    }
}

答案 13 :(得分:-1)

你可以使用

轻松完成此操作
  • self.textview.font =
  

self.textview.font = UIFont(name:self.textview.font!.fontName,
  size:self.textview.frame.size.height / 10)!

根据您的要求将textview.frame.size.height除以某个常量..

答案 14 :(得分:-2)

以下是 Swift 4 (基于Arie Litovsky答案)的解决方案,您可以将此代码放入textViewDidChange(_ textView: UITextView)委托方法中:

缩小字体:

    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()
    }

扩展字体:

        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()
    }

您可以更改fontSize < 12fontSize > 15以符合您的最小和最大字体大小。