iOS 9 - UITextView缩小字体大小以填充文本视图而不滚动?

时间:2016-08-05 21:34:53

标签: ios autolayout uitextview ios9 uifont

我有一个UITextView,其中包含的文字可能从几个单词到很多单词。 在使用应用程序时,文本视图具有固定的高度,并且启用了滚动功能,因此用户可以查看任何可能很长的文本。

但是,允许用户使用本机共享功能“共享”屏幕。在此过程中,我会截取屏幕截图以用于图像,以及一些其他添加的UI元素。由于您无法滚动屏幕截图,我禁用了UITextView的滚动功能并尝试缩小任何长文本的字体,同时增加任何小文本的字体,以便尽可能地填充UITextView。

这适用于旧版本的iOS,但iOS 9似乎有一些问题,现在它切断了一些文本:

另一方面,此视图内置于XIB文件中并使用自动布局。

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 个答案:

答案 0 :(得分:0)

所以更好的解决方案就是在共享屏幕上使用UILabel,因为我不需要UITextView。

仍然很好奇调整字体大小以适应UITextView文本的正确方法是......