ios - 在封闭的 TextView 中使用动态字体自动调整 View 大小

标签 ios swift uiview uitextview

所以我在这里搜索时似乎找不到匹配的案例。

我有一个包含 UITextView 的小型 UIView,并且 UIView 需要围绕 TextView 自动调整大小,以便在另一个 View 上呈现。基本上,TextView 需要完全填充 UIView,并且 UIView 应该仅足够大以包含 TextView。

TextView 仅包含几个句子,这些句子将保留在屏幕上,直到发生外部事件并且某些值发生变化。

当我使用固定大小的字体时,一切都很棒。

但是嘿...我是个老家伙了,我 Handlebars 机上的文字大小调大了一点。在我的设备上测试它表明我一定遗漏了一些东西。

当在textview属性中使用动态字体样式“Title 2”,并在TextView属性中打开“自动调整字体”,并且文本大于默认值时,似乎我没有正确捕获创建新的边界矩形以在框架上抛掷时 TextView 增长的大小(具有更大的文本)。它返回的值看起来很像较小的默认大小的文本值,而不是增加的文本大小。

代码如下, View 的类代码以及调用代码(在此处发布时非常明确)。我想我要么错过了一些愚蠢的事情,比如在字体发生问题后捕获大小,但即使将此代码移至新函数并在控件完全绘制后显式调用它似乎也无法做到这一点。

我希望这是有道理的。

谢谢大家。

调用代码:

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
    }
}

最佳答案

这种完美的方式内容来自:https://www.youtube.com/watch?v=0Jb29c22xu8 .

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
            }
        }
    }

}

关于ios - 在封闭的 TextView 中使用动态字体自动调整 View 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52977104/

相关文章:

ios - Swift NSMutableArray 增加一个数组

ios - Xcode SpriteKit - 删除 Sprite 并停止 Action - repeatActionForever

iOS:根据 View 位置动态更改 UILabel 对齐方式

较小屏幕上的 ios 自动布局问题

ios - 使用 Swift 2.0 从 Parse 检索数据

ios - 从嵌套 NSDictionary 读取值

swift - 如何设置标签背景图像比例以适应Swift?

ios - 如何检查 HealthKit 功能何时完成(Swift)

iphone - 触摸屏幕上的任意位置即可激活方法

ios - 在完成之前取消 UIView animateWithDuration