Alisher
Alisher

Reputation: 89

UILabel blurred text using UIPinchGestureRecognizer

I have a UILabel; when I enlarge it using UIPinchGestureRecognizer, the text becomes blurred. before

after

I use CGAffineTransformScale my code

self.myLabel.transform = CGAffineTransformScale(self.myLabel.transform, pinchRecognizer.scale, pinchRecognizer.scale);

How to fix it?

Upvotes: 3

Views: 939

Answers (2)

Maria Ortega
Maria Ortega

Reputation: 381

After a lot of trying and failed. I found a solution.

  @IBAction func handlePinch(recognizer : UIPinchGestureRecognizer) {
    var pinchScale = recognizer.scale
    signatureLabel.layer.contentsScale = UIScreen.main.scale + pinchScale;
    signatureLabel.transform = signatureLabel.transform.scaledBy(x: pinchScale, y:pinchScale)
    pinchScale = round(pinchScale * 1000) / 1000.0

    if recognizer.state == .changed {
        signatureLabel.font = UIFont(name: signatureLabel.font.fontName, size: signatureLabel.font.pointSize * pinchScale)
        pinchScale = recognizer.scale
    }
    recognizer.scale = 1
}

Although, It is not working properly if you don't update the constraints for that UILabel. So, in the storyboard I have added the Vertical & Horizontal center constraints to my label. Then, created the outlets into my class. And in my handlePan method I have done:

@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
    let translation = recognizer.translation(in: recognizer.view)
    self.signatureLabel.center = CGPoint(x:self.signatureLabel.center.x + translation.x,
                                         y:self.signatureLabel.center.y + translation.y)
    signatureLabelCenterConstraint.constant = signatureLabelCenterConstraint.constant + translation.y
    signatureLabelCenterXConstraint.constant = signatureLabelCenterXConstraint.constant + translation.x
    signatureLabel.setNeedsLayout()
    recognizer.setTranslation(.zero, in: recognizer.view)
}

I hope that help everyone!

Upvotes: 1

Alisher
Alisher

Reputation: 89

I found how to solve this problem, it was so easy.

give scale:

CGFloat scale =  self.myLable.transform.a *pinchRecognizer.scale *[UIScreen mainScreen].scale;

this a - (self.myLable.transform.a) return current scale factor.

 self.myLable.transform = CGAffineTransformScale(self.myLable.transform, pinchRecognizer.scale, pinchRecognizer.scale);
[self.myLable setContentScaleFactor:scale];

Done, happy coding!

Upvotes: 2

Related Questions