Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 339
PinkeshGjr Avatar asked Jan 12 '19 12:01

PinkeshGjr


2 Answers

Here is the code to resize font size along with frame on pinch zoom in/out using UITextView and isScrollEnabled = false

@objc func pinchRecoginze(_ pinchGesture: UIPinchGestureRecognizer) {
   guard recognizer.view != nil, let view = recognizer.view else {return}

    if view is UITextView {
        let textView = view as! UITextView
        if recognizer.state == .began {
            let font = textView.font
            let pointSize = font!.pointSize
            recognizer.scale = pointSize * 0.1
        }
        if 1 <= recognizer.scale && recognizer.scale <= 10  {
            textView.font = UIFont(name: textView.font!.fontName, size: recognizer.scale * 10)
            let textViewSiSize = textView.intrinsicContentSize
            textView.bounds.size = textViewSiSize
        }
    }
}

Smaller font size

Pinch zoom to larger font size

Updated answer to compatible with UITextView

like image 76
Bhavik Modi Avatar answered Nov 15 '22 01:11

Bhavik Modi


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
        }
    }
like image 32
PinkeshGjr Avatar answered Nov 15 '22 01:11

PinkeshGjr