【问题标题】:iOS 9 - UITextView shrink font size to fill text view without scrolling?iOS 9 - UITextView 缩小字体大小以填充文本视图而不滚动?
【发布时间】:2016-12-12 09:22:04
【问题描述】:

我有一个 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)
    }
}

【问题讨论】:

    标签: ios autolayout uitextview ios9 uifont


    【解决方案1】:

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

    仍然好奇调整字体大小以使文本适合 UITextView 的正确方法是什么...

    【讨论】:

      猜你喜欢
      • 2011-01-03
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      • 2015-04-05
      • 1970-01-01
      • 2016-07-04
      • 2012-06-28
      • 2018-08-23
      相关资源
      最近更新 更多