Johno2110
Johno2110

Reputation: 1141

Resizing views' bounds according to frame

My situation now is I am trying to resize a label, I can comfortable resize it using a UIPinchGestureRecognizer but what this does is resize the frame of the label only.

func handleScale(recognizer: UIPinchGestureRecognizer)
{

    recognizer.view!.transform = CGAffineTransformScale(recognizer.view!.transform,
        recognizer.scale, recognizer.scale)
    recognizer.scale = 1

    recognizer.view!.frame = CGRectMake(recognizer.view!.frame.minX,recognizer.view!.frame.minY, recognizer.view!.frame.width, recognizer.view!.frame.height)  
}   

I want to be able to change the bounds of the label so the text is rendered again in the new frames dimensions. I have tried only resizing the labels bounds in the scale, but doesn't work properly.

I have made a button upon clicked it sets the labels bounds according to the labels frame. Though it doesn't seem to work.

    label.bounds = CGRectMake(0, 0, CGRectGetWidth(self.label.frame), CGRectGetHeight(self.label.frame))

What this does above is resize it to a different coordinate then the frame.

This is the print logs from the button action:

Frame Before(-54.2193053279009,279.213449112017,513.438610655802,85.5731017759669)
Bounds Before(0.0,0.0,300.0,50.0)
label.bounds = CGRectMake(0, 0, CGRectGetWidth(self.label.frame), CGRectGetHeight(self.label.frame))
Frame After(-236.8653448536,248.7724425244,878.7306897072,146.4551149512)
Bounds After(0.0,0.0,513.438610655802,85.5731017759669)

What it's doing is once the bounds are set it changes the frame's values. Does anyone know how to fix this?

I have tried setting the frame from the previous frame values but still doesn't work.

Upvotes: 3

Views: 337

Answers (1)

jszumski
jszumski

Reputation: 7416

You are running into the scenario described in the documentation for UIView setTransform::

WARNING

If this property is not the identity transform, the value of the frame property is undefined and therefore should be ignored.

If your objective is to simply visually shrink the label, just apply the transform without adjusting the frame.

If you are intending to shrink the geometric rectangle of the label while keeping the text size the same, you can generate a new frame with CGRectApplyAffineTransform(CGAffineTransformMakeScale(recognizer.scale, recognizer.scale)). If you find that your text is still not updating, you should try calling setNeedsLayout or setNeedsDisplay on the label (depending on how its drawing is handled).

Upvotes: 1

Related Questions