5

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.

PinkeshGjr
  • 8,460
  • 5
  • 41
  • 56
  • Looking at your comments in the answer, I'm guessing you are running into an issue with `UIFont` and how a custom font's vectors are implemented. (I know *just enough* about creating a custom font to be dangerous.) Two thoughts: (1) Do you know that your sources for solution are actually using a "canned" `UIFont`? Is it possible they created their own custom font - they do have the headcount and resources to do such a thing. (2) If they aren't, is it possible they "go around" `UIFont` by using `UIImage` and animating? Good luck figuring this out. –  Jan 12 '19 at 16:10
  • No i just want to zoom-in, zoom-out textView without font getting blurry – PinkeshGjr Jan 12 '19 at 16:12
  • I get that. But I'm curious - are *you* making a wrong assumption? Is you belief that Instagram and SnapChat are doing what *you* think... animating a font size change without blurriness... wrong? If you believe you are wrong, then why? Do you know - again, I know just enough to be dangerous - the internals of `UIFont` to a point that you know this can be done? That's what I see is you "base" assumption. –  Jan 12 '19 at 16:15
  • Then how can they implemented this kind of functionality? – PinkeshGjr Jan 12 '19 at 16:16
  • See my edit to my comments, and combine them with my first comment. Maybe they are using a custom `UIFont` - they surely have the resources to make a rasterized alphabet that can do this - or maybe they found an easier way - using a `UIImage` to "expand/contract" the textfield without blurriness a then showing the "finished" textfield. You ask a great question - I upvoted it - and I'm just thinking "outside of the box" because so far none has an answer. –  Jan 12 '19 at 16:20

3 Answers3

7

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

serhat sezer
  • 1,330
  • 1
  • 10
  • 26
Bhavik Modi
  • 1,517
  • 14
  • 29
1

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
        }
    }
PinkeshGjr
  • 8,460
  • 5
  • 41
  • 56
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
  }
}
emrepun
  • 2,496
  • 2
  • 15
  • 33