【发布时间】:2018-10-24 20:01:19
【问题描述】:
所以这是一个我似乎无法在此处搜索的匹配案例。
我有一个包含 UITextView 的小型 UIView,并且 UIView 需要在 TextView 周围自动调整大小,以便在另一个视图上呈现。基本上TextView需要完全填满UIView,而UIView应该只够容纳TextView。
TextView 仅包含几个句子,这些句子旨在保持在屏幕上,直到发生外部事情并且某些值发生变化。
当我使用固定大小的字体时,一切都很好。
但是,嘿...我是个老家伙,我的手机上的文字大小有点大。在我的设备上测试它显示我必须遗漏一些东西。
在 textview 属性中使用动态字体样式“Title 2”,并在 TextView 属性中打开“自动调整字体”,并且文本大于默认值时,似乎我没有正确捕获创建新的边界矩形以在框架上折腾时,TextView 的增长大小(带有更大的文本)。它返回的值看起来很像较小的默认大小的文本值,而不是增加的文本大小。
代码如下,视图的类代码以及调用代码(在此处发布时非常明确)。我想我要么错过了一些愚蠢的东西,比如在字体发生某些事情后捕获大小,但即使将此代码移动到一个新函数并在控件完全绘制后显式调用它似乎也没有做到这一点。
我希望这是有道理的。
谢谢大家。
调用代码:
let noWView:NoWitnessesYetView = (Bundle.main.loadNibNamed("NoWitnessesYetView", owner: nil, options: nil)!.first as! NoWitnessesYetView)
//if nil != noWView {
let leftGutter:CGFloat = 20.0
let bottomGutter:CGFloat = 24.0
let newWidth = self.view.frame.width - ( leftGutter + leftGutter )
let newTop = (eventMap.frame.minY + eventMap.frame.height) - ( noWView.frame.height + bottomGutter ) // I suspect here is the issue
// I suspect that loading without drawing is maybe not allowing
// the fonts to properly draw and the
// TextView to figure out the size...?
noWView.frame = CGRect(x: 20, y: newTop, width: newWidth, height: noWView.frame.height)
self.view.addSubview(noWView)
//}
类代码:
import UIKit
class NoWitnessesYetView: UIView {
@IBOutlet weak var textView: EyeneedRoundedTextView!
override func draw(_ rect: CGRect) {
let newWidth = self.frame.width
// form up a dummy size just to get the proper height for the popup
let workingSize:CGSize = self.textView.sizeThatFits(CGSize(width: newWidth, height: CGFloat(MAXFLOAT)))
// then build the real newSize value
let newSize = CGSize(width: newWidth, height: workingSize.height)
textView.frame.size = newSize
self.textView.isHidden = false
}
override func awakeFromNib() {
super.awakeFromNib()
self.backgroundColor = UIColor.clear // .blue
self.layer.cornerRadius = 10
}
}
【问题讨论】:
标签: ios swift uiview uitextview