使用捏合手势将文本按比例缩小后键入文本字段时出现奇怪的行为?

时间:2019-01-28 05:17:43

标签: ios swift uitextview

我已经建立了UITextView的子类,该子类会根据用户输入的类型进行扩展和缩小以适合文本。我还在此文本视图上有一个Pinch Gesture Recognizer,可以让用户上下缩放它。

但是,当我捏​​缩小文本视图的比例时,然后尝试再次编辑它,在键入任何字符后,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

    }

}



}

这是缩小之前的图像:

example

及之后:

example

以及视图层次结构的屏幕截图:

example

谢谢!

2 个答案:

答案 0 :(得分:0)

我设法通过以下答案解决了这个问题:iOS: Scaling UITextView with pinching?

它仅涉及按比例更改字体大小,然后将文本视图大小调整为该大小。这是我的工作代码:

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


    adjustTextViewSize(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) {
    adjustTextViewSize(textView)
}



func adjustTextViewSize(_ textView: UITextView) {

    let newSize = textView.sizeThatFits(CGSize(width: screenWidth, height: CGFloat.greatestFiniteMagnitude))

    textView.frame = CGRect(x: (textView.frame.minX), y: (textView.frame.minY), width: newSize.width, height: newSize.height)

    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) {

    let scale:CGFloat = recognizer.scale

    guard let textView = recognizer.view as? FlexibleText else { return }

    textView.font = UIFont(name: textView.font!.fontName, size: textView.font!.pointSize*scale)

    adjustTextViewSize(textView)

    recognizer.scale = 1.0

}



}

答案 1 :(得分:-1)

添加此代码

currentTextView.contentInsetAdjustmentBehavior = UIScrollView.ContentInsetAdjustmentBehavior.never' into your method 'viewDidLoad()