Изменить размер шрифта в UITextView - PullRequest
5 голосов
/ 04 мая 2010

Есть ли способ уменьшить размер шрифта в UITextView, если текста слишком много? Похоже на UILabel?

Ответы [ 3 ]

12 голосов
/ 20 января 2012

Проблема с принятым ответом состоит в том, что вам нужно угадать количество символов (длину строки), необходимое для заполнения поля, и оно отличается от шрифта к шрифту. Примерно так, категория в 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
8 голосов
/ 04 мая 2010

Попробуйте это:

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

    [ textView setFont: [ UIFont systemFontOfSize: newSize ]];
}
2 голосов
/ 21 сентября 2017

Реализация Swift 4, вдохновленная ответом @Jane Sales.

При расчете доступной ширины и высоты мы также должны учитывать возможные вертикальные и горизонтальные поля (textContainerInset и textContainer.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)
    }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...