Ios 在封闭文本视图中使用动态字体自动调整视图大小
所以这里有一个我在这里找不到匹配的例子 我有一个包含UITextView的小UIView,UIView需要围绕文本视图自动调整大小,以便在另一个视图上显示。基本上,TextView需要完全填充UIView,UIView应该只足够大以包含TextView TextView只包含几个句子,这些句子将一直保留在屏幕上,直到外部事件发生,并且某些值发生变化 当我使用固定大小的字体时,一切都很棒 但是嘿。。。我是个老家伙,我的手机上的文字大小有点大了。在我的设备上测试它,可以看出我肯定遗漏了什么 当在textview属性中使用动态字体样式“Title 2”并在textview属性中启用“Automatic adjust font”并使文本大于默认值时,似乎在创建新的边界矩形以在帧上投掷时,我没有正确捕获textview的增长大小(使用较大的文本)。它返回的值看起来很像较小的默认大小的文本值,而不是增加的文本大小 代码在下面,视图的类代码以及调用代码(在这里发布时变得非常明确)。我想我要么错过了一些愚蠢的事情,比如在字体发生变化后捕获大小,但即使将此代码移动到一个新函数并在控件完全绘制后显式调用它,似乎也做不到这一点 我希望这是有意义的 谢谢大家 呼叫代码:Ios 在封闭文本视图中使用动态字体自动调整视图大小,ios,swift,uiview,uitextview,Ios,Swift,Uiview,Uitextview,所以这里有一个我在这里找不到匹配的例子 我有一个包含UITextView的小UIView,UIView需要围绕文本视图自动调整大小,以便在另一个视图上显示。基本上,TextView需要完全填充UIView,UIView应该只足够大以包含TextView TextView只包含几个句子,这些句子将一直保留在屏幕上,直到外部事件发生,并且某些值发生变化 当我使用固定大小的字体时,一切都很棒 但是嘿。。。我是个老家伙,我的手机上的文字大小有点大了。在我的设备上测试它,可以看出我肯定遗漏了什么 当在te
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
}
}
这是一个完美的方法,内容来自:
我很欣赏这篇教程,我把它放在书签里,以便以后有时间看的时候阅读。谢谢现在我想知道我是否错过了一件简单的事情,或者我只是采取了完全错误的方法。我要到今晚晚些时候才能看这段视频。你永远不会后悔:)。他将给出这背后的逻辑。
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// let's create our text view
let textView = UITextView()
textView.frame = CGRect(x: 0, y: 0, width: 200, height: 100)
textView.backgroundColor = .lightGray
textView.text = "Here is some default text that we want to show and it might be a couple of lines that are word wrapped"
view.addSubview(textView)
// use auto layout to set my textview frame...kinda
textView.translatesAutoresizingMaskIntoConstraints = false
[
textView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
textView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
textView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
textView.heightAnchor.constraint(equalToConstant: 50)
].forEach{ $0.isActive = true }
textView.font = UIFont.preferredFont(forTextStyle: .headline)
textView.delegate = self
textView.isScrollEnabled = false
textViewDidChange(textView)
}
}
extension ViewController: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
print(textView.text)
let size = CGSize(width: view.frame.width, height: .infinity)
let estimatedSize = textView.sizeThatFits(size)
textView.constraints.forEach { (constraint) in
if constraint.firstAttribute == .height {
constraint.constant = estimatedSize.height
}
}
}
}