Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

401
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda