调整 UITextView 中的字体大小

发布于 2024-08-30 21:08:38 字数 52 浏览 3 评论 0原文

如果文本太多,有没有办法缩小 UITextView 中的字体大小?类似于 UILabel?

Is there a way to shrink the font-size in a UITextView if there is too much text? Similar to the UILabel?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遗心遗梦遗幸福 2024-09-06 21:08:38

接受的答案的问题是您必须猜测填充该字段所需的字符数(字符串的长度),并且每个字体的字符数都不同。像这样的 UITextView 上的类别应该可以工作。

#import "UITextView+Size.h"

#define kMaxFieldHeight 1000

@implementation UITextView (Size)

-(BOOL)sizeFontToFitMinSize:(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 = [self.text sizeWithFont:self.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];

while (stringSize.height >= self.frame.size.height) {

    if (fontSize <= aMinFontSize) // it just won't fit, ever
        return NO;

    fontSize -= 1.0;
    self.font = [self.font fontWithSize:fontSize];
    tallerSize = CGSizeMake(self.frame.size.width-fudgeFactor,kMaxFieldHeight);
    stringSize = [self.text sizeWithFont:self.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];
}

return YES; 
}

@end

The problem with the accepted answer is that you have to guess the number of characters (the string's length) needed to fill the field, and that differs from font to font. Something like this, a category on UITextView, should work.

#import "UITextView+Size.h"

#define kMaxFieldHeight 1000

@implementation UITextView (Size)

-(BOOL)sizeFontToFitMinSize:(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 = [self.text sizeWithFont:self.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];

while (stringSize.height >= self.frame.size.height) {

    if (fontSize <= aMinFontSize) // it just won't fit, ever
        return NO;

    fontSize -= 1.0;
    self.font = [self.font fontWithSize:fontSize];
    tallerSize = CGSizeMake(self.frame.size.width-fudgeFactor,kMaxFieldHeight);
    stringSize = [self.text sizeWithFont:self.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];
}

return YES; 
}

@end
愁杀 2024-09-06 21:08:38

试试这个:

NSInteger lengthThreshold = 200;
if( [ textView.text length ] > lengthThreshold ) {
    NSInteger newSize = ... //calculate new size based on length

    [ textView setFont: [ UIFont systemFontOfSize: newSize ]];
}

Try this:

NSInteger lengthThreshold = 200;
if( [ textView.text length ] > lengthThreshold ) {
    NSInteger newSize = ... //calculate new size based on length

    [ textView setFont: [ UIFont systemFontOfSize: newSize ]];
}
无边思念无边月 2024-09-06 21:08:38

Swift 4 实现的灵感来自 @Jane Sales 的回答。

在计算可用宽度和高度时,我们还必须考虑可能的垂直和水平边距(textContainerInsettextContainer.lineFragmentPadding)。

以下是对 UITextView 上边距如何工作的更好解释:https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/TextUILayer/Tasks/SetTextMargins.html

如果文本视图可以调整大小,那么我们也必须强制布局,以便我们可以根据最大可能的文本视图大小来计算字体大小。在这种情况下,仅考虑高度(仅当所需文本高度大于原始可用高度时才布局)。

import UIKit

extension UITextView {

    func adjustFontToFitText(minimumScale: CGFloat) {
        guard let font = font else {
            return
        }

        let scale = max(0.0, min(1.0, minimumScale))
        let minimumFontSize = font.pointSize * scale
        adjustFontToFitText(minimumFontSize: minimumFontSize)
    }

    func adjustFontToFitText(minimumFontSize: CGFloat) {
        guard let font = font, minimumFontSize > 0.0 else {
            return
        }

        let minimumSize = floor(minimumFontSize)
        var fontSize = font.pointSize

        let availableWidth = bounds.width - (textContainerInset.left + textContainerInset.right) - (2 * textContainer.lineFragmentPadding)
        var availableHeight = bounds.height - (textContainerInset.top + textContainerInset.bottom)

        let boundingSize = CGSize(width: availableWidth, height: CGFloat.greatestFiniteMagnitude)
        var height = text.boundingRect(with: boundingSize, options: .usesLineFragmentOrigin, attributes: [.font: font], context: nil).height

        if height > availableHeight {
            // If text view can vertically resize than we want to get the maximum possible height
            sizeToFit()
            layoutIfNeeded()
            availableHeight = bounds.height - (textContainerInset.top + textContainerInset.bottom)
        }

        while height >= availableHeight {
            guard fontSize > minimumSize else {
                break
            }

            fontSize -= 1.0
            let newFont = font.withSize(fontSize)
            height = text.boundingRect(with: boundingSize, options: .usesLineFragmentOrigin, attributes: [.font: newFont], context: nil).height
        }

        self.font = font.withSize(fontSize)
    }

}

Swift 4 implementation inspired by @Jane Sales's answer.

When calculating available width and height we must also take into consideration possible vertical and horizontal margins (textContainerInset and textContainer.lineFragmentPadding).

Here's a better explanation of how margins work on UITextView: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/TextUILayer/Tasks/SetTextMargins.html

If the text view can resize, then we must also force a layout so we can calculate the font size based on biggest possible text view size. In this case only height is considered (layouts only if required text height is bigger than original available height).

import UIKit

extension UITextView {

    func adjustFontToFitText(minimumScale: CGFloat) {
        guard let font = font else {
            return
        }

        let scale = max(0.0, min(1.0, minimumScale))
        let minimumFontSize = font.pointSize * scale
        adjustFontToFitText(minimumFontSize: minimumFontSize)
    }

    func adjustFontToFitText(minimumFontSize: CGFloat) {
        guard let font = font, minimumFontSize > 0.0 else {
            return
        }

        let minimumSize = floor(minimumFontSize)
        var fontSize = font.pointSize

        let availableWidth = bounds.width - (textContainerInset.left + textContainerInset.right) - (2 * textContainer.lineFragmentPadding)
        var availableHeight = bounds.height - (textContainerInset.top + textContainerInset.bottom)

        let boundingSize = CGSize(width: availableWidth, height: CGFloat.greatestFiniteMagnitude)
        var height = text.boundingRect(with: boundingSize, options: .usesLineFragmentOrigin, attributes: [.font: font], context: nil).height

        if height > availableHeight {
            // If text view can vertically resize than we want to get the maximum possible height
            sizeToFit()
            layoutIfNeeded()
            availableHeight = bounds.height - (textContainerInset.top + textContainerInset.bottom)
        }

        while height >= availableHeight {
            guard fontSize > minimumSize else {
                break
            }

            fontSize -= 1.0
            let newFont = font.withSize(fontSize)
            height = text.boundingRect(with: boundingSize, options: .usesLineFragmentOrigin, attributes: [.font: newFont], context: nil).height
        }

        self.font = font.withSize(fontSize)
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文