【发布时间】: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