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

394
Views
Cambiar el tamaño de la fuente y el marco con pinchGesture cuando textView isScrollEnabled = false

He usado pinchGesture para acercar y alejar textView usando el siguiente código.

añadido pinchGesture a textView

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

delegado

 @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 } }

Resultado

ingrese la descripción de la imagen aquíingrese la descripción de la imagen aquí

Aquí está la posible solución que he aplicado pero aún no puedo encontrar la solución perfecta.

 @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) } }

Resultado

ingrese la descripción de la imagen aquí

Usando la solución anterior, puedo aumentar el tamaño de la fuente con éxito, pero el marco textView no se actualiza, por lo que el texto se corta porque el marco textView es más pequeño.

Resultado Esperado

La fuente aumentará y también el marco se actualizará, por lo que se verá como un simple acercamiento y alejamiento, pero sin desenfoque.

Buscando la mejor solución posible para aumentar el tamaño de la fuente con un marco como lo están haciendo Instagram y Snapchat .

Gracias.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Aquí está el código para cambiar el tamaño de la fuente junto con el marco al pellizcar acercar/alejar usando UITextView e 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 } } }

Tamaño de fuente más pequeño

Pellizcar zoom para tamaño de fuente más grande

Respuesta actualizada a compatible con UITextView

over 4 years ago · Santiago Trujillo Report

0

Aquí está el cambio de tamaño de fuente y marco con pinchGesture cuando 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

¿Qué sucede si intenta habilitar el desplazamiento al comienzo de su método handlePinch y lo deshabilita nuevamente al final de pellizcar?:

 @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!