我已经建立了一个子类,UITextView
它会随着用户键入而扩展和收缩以适应文本,称为 FlexibleText。我在这个文本视图上还有一个捏手势识别器,允许用户放大和缩小它。
但是,当我捏住以缩小文本视图时,然后尝试再次编辑它,在键入任何字符后UITextContainerView
,文本视图内的比例会缩小,切断一些文本。
我真的很感激任何帮助解决这个问题!这是我的代码:
import UIKit
class ViewController: UIViewController, UITextViewDelegate, UIGestureRecognizerDelegate {
class FlexibleText: UITextView {
var horizontalConstraint: NSLayoutConstraint?
var verticalConstraint: NSLayoutConstraint?
var widthConstraint: NSLayoutConstraint?
var heightConstraint: NSLayoutConstraint?
}
var currentTextView = FlexibleText()
var screenWidth = UIScreen.main.bounds.width
var screenHeight = UIScreen.main.bounds.height
override func viewDidLoad() {
super.viewDidLoad()
//add close keyboard recognizer
let closeKeyboardRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
view.addGestureRecognizer(closeKeyboardRecognizer)
let text1 = FlexibleText()
text1.text = "your text"
text1.font = UIFont.systemFont(ofSize: 40)
text1.backgroundColor = UIColor.yellow
text1.frame = CGRect(x: 50, y: 100, width: 250, height: 100)
text1.isScrollEnabled = false
text1.delegate = self
text1.textAlignment = .center
view.addSubview(text1)
// layout
text1.translatesAutoresizingMaskIntoConstraints = false
text1.horizontalConstraint = text1.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0)
text1.horizontalConstraint?.isActive = true
text1.verticalConstraint = text1.topAnchor.constraint(equalTo: view.topAnchor, constant: 100)
text1.verticalConstraint?.isActive = true
text1.widthConstraint = text1.widthAnchor.constraint(equalToConstant: 250)
text1.widthConstraint?.isActive = true
text1.heightConstraint = text1.heightAnchor.constraint(equalToConstant: 100)
text1.heightConstraint?.isActive = true
adjustTextViewHeight(text1)
currentTextView = text1
//add pinch gesture
let pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(pinchRecognized))
pinchGesture.delegate = self
text1.addGestureRecognizer(pinchGesture)
}
// flexible functions
func textViewDidChange(_ textView: UITextView) {
adjustTextViewHeight(textView)
}
func adjustTextViewHeight(_ textView: UITextView) {
let newSize = textView.sizeThatFits(CGSize(width: screenWidth, height: CGFloat.greatestFiniteMagnitude))
if let currentView = textView as? FlexibleText {
if newSize.width <= screenWidth {
currentView.widthConstraint?.constant = newSize.width
} else if newSize.width > screenWidth {
currentView.widthConstraint?.constant = screenWidth
}
currentView.heightConstraint?.constant = newSize.height
}
self.view.layoutIfNeeded()
}
@objc func dismissKeyboard() {
view.endEditing(true)
}
//pinch gesture
@objc func pinchRecognized(recognizer: UIPinchGestureRecognizer) {
if let view = recognizer.view as? FlexibleText {
if recognizer.state == .changed {
view.transform = view.transform.scaledBy(x: recognizer.scale, y: recognizer.scale)
} else if recognizer.state == .ended {
view.font = UIFont.systemFont(ofSize: 40 * recognizer.scale)
}
adjustTextViewHeight(view)
recognizer.scale = 1.0
}
}
}
这是缩小之前的图像:
之后:
以及视图层次结构的屏幕截图:
谢谢!