Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

407
Views
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.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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

over 4 years ago · Santiago Trujillo Report

0

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
        }
    }
over 4 years ago · Santiago Trujillo Report

0

What if you try to enable scrolling in the beginning of your handlePinch method and disable it again at the end of pinching?:

@IBAction func handlePinch(recognizer:UIPinchGestureRecognizer) {
  if let textView = recognizer.view as? UITextView {
    textView.isScrollingEnabled = true
    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)
    let width = view.frame.size.width
    textView.frame.size = textView.sizeThatFits(CGSize(width: width, height: CGFloat.greatestFiniteMagnitude))
    textView.isScrollingEnabled = false
  }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!