0

I have a UITextView that contains text that may range from a few words to a lot of words. While using the app, the text view has a fixed height and scrolling is enabled so the user can view any potentially long text.

However, the user is allowed to "share" the screen using the native share features. During this process, I take a screenshot of the screen to use for the image, along with a few other added UI elements. Since you can't scroll a screenshot, I disabled the UITextView's scrolling ability and attempt to shrink the font of any long text, while increasing the font of any small text so that the UITextView is filled as best as possible.

This use to work on older versions of iOS, but iOS 9 seems to have some issues and now it cuts off some of the text:

One side note, this view is built in a XIB file and uses autolayout.

override func awakeFromNib() {
    super.awakeFromNib()

    self.translatesAutoresizingMaskIntoConstraints = false

    if UIDevice.iPad() || UIDevice.iPadPro() {
        bobbleheadViewWidthConstraint.constant = 300.0
        bobbleheadViewHeightConstraint.constant = 600.0
        self.updateConstraintsIfNeeded()
        self.layoutIfNeeded()
    }

    speechBubbleView.roundCornersWithRadius(5.0)
}

class func shareViewForQuote(quote: Quote) -> BTVShareView? {
    if let shareView = UINib(nibName: "BTVShareView", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as? BTVShareView {
        shareView.bobbleheadView.image = UIImage(named: quote.character!.name!.stringByReplacingOccurrencesOfString(" ", withString: "-"))
        shareView.textView.text = quote.text
        shareView.textView.font = UIFont.systemFontOfSize(60.0)

        // Re-size quote text
        shareView.updateTextFont()
        return shareView
    }
    return nil
}

func updateTextFont() {
    let minFontSize: CGFloat = 1.0
    var currentFontSize: CGFloat = 100.0
    while (currentFontSize > minFontSize && textView.sizeThatFits(CGSizeMake(textView.frame.size.width, textView.frame.size.height)).height >= textView.frame.size.height) {
        currentFontSize -= 1.0
        textView.font = textView.font!.fontWithSize(currentFontSize)
    }
}

1 Answer 1

0

So a better solution was to just use a UILabel for the share screen, since I don't need a UITextView anyways.

Still curious what the proper way to adjust font size to fit text to UITextView would be though...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.