iosswiftuitextviewinstagramuipinchgesturerecognizer

Resize font and frame with pinchGesture when textView isScrollEnabled = false


I have used pinchGesture to zoom-in and zoom-out textView using below code.

added pinchGesture to textView

let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(self.handlePinch))
pinchGesture.delegate = self
view.addGestureRecognizer(pinchGesture)

delagate

@IBAction func handlePinch(recognizer:UIPinchGestureRecognizer) {
    if let view = recognizer.view as? UITextView {
        view.transform = view.transform.scaledBy(x: recognizer.scale, y: recognizer.scale)
        recognizer.scale = 1
    }
}

Result

enter image description here enter image description here

Here is the possible solution i have applied but still not able to find perfect solution.

@IBAction func handlePinch(recognizer:UIPinchGestureRecognizer) {
    if let textView = recognizer.view as? UITextView {
        let font = textView.font!
        var pointSize = font.pointSize
        let fontName = font.fontName
        pointSize = ((recognizer.velocity > 0) ? 1 : -1) * 1 + pointSize;
        if (pointSize < 13) {
            pointSize = 13
        }
        if (pointSize > 100) {
            pointSize = 100
        }
        textView.font = UIFont(name: fontName, size: pointSize)
    }
}

Result

enter image description here

Using above solution i am successfully able to increase font size but textView frame is not updating so text is getting cut off because textView frame is smaller.

Expected Result

Font will get increased and also frame will get update so it will look like simple zoom-in and zoom-out but without blurry.

Looking for best possible solution to increase font size with frame like instagram and snapchat is doing.

Thanks.


Solution

  • Here is the to resize font and frame with pinchGesture when textView isScrollEnabled = false.

    @IBAction func handlePinch(recognizer:UIPinchGestureRecognizer) {
            if let view = recognizer.view {
                if view is UITextView {
                    let textView = view as! UITextView
                    if textView.font!.pointSize * recognizer.scale < 90 {
                        let font = UIFont(name: textView.font!.fontName, size: textView.font!.pointSize * recognizer.scale)
                        textView.font = font
                        let sizeToFit = textView.sizeThatFits(CGSize(width: UIScreen.main.bounds.size.width,
                                                                     height:CGFloat.greatestFiniteMagnitude))
                        textView.bounds.size = CGSize(width: textView.intrinsicContentSize.width,
                                                      height: sizeToFit.height)
                    } else {
                        let sizeToFit = textView.sizeThatFits(CGSize(width: UIScreen.main.bounds.size.width,
                                                                     height:CGFloat.greatestFiniteMagnitude))
                        textView.bounds.size = CGSize(width: textView.intrinsicContentSize.width,
                                                      height: sizeToFit.height)
                    }
                    textView.setNeedsDisplay()
                } else {
                    view.transform = view.transform.scaledBy(x: recognizer.scale, y: recognizer.scale)
                }
                recognizer.scale = 1
            }
        }