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
Hope that's clearer:
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
}
}
}