Reputation: 998
I have an UILabel whose font size is to be increased on pinch gesture iOS swift. I am able to increase the font size but having problem with the view. Thought sizeToFit() increases the height and width as needed but it isn't reflected on the view i.e. the label remains the same in view. Please help me to increase the size of label with the increase in font size.
@IBAction func increaseTextFont(sender: UIPinchGestureRecognizer) {
var pinchScale = sender.scale
pinchScale = round(pinchScale * 1000) / 1000.0
if (pinchScale < 1) {
testLabel.font = UIFont( name: "arial", size: testLabel.font.pointSize - pinchScale)
}
else{
testLabel.font = UIFont( name: "arial", size: testLabel.font.pointSize + pinchScale)
}
testLabel.frame.height * pinchScale))
testLabel.frame.size.height *= pinchScale
testLabel.frame.size.width *= pinchScale
self.testLabel.layoutIfNeeded()
print(testLabel.frame.size.height)
print(testLabel.frame.size.width)
}
Upvotes: 2
Views: 2515
Reputation: 4180
I have done a test demo for the purpose of demonstration and you can see the outcome of the same,
The constraint of the label are as below,
You might need to change the constraint as per the UI you have to design or the requirement, just make sure you do not set the height constraint of the label and if you have to make the label multiline then set the constraint and the label properties accordingly.
The code for Pinch Gesture,
@IBOutlet weak var labelTest: UILabel!
@IBAction func increaseTextFont(sender: UIPinchGestureRecognizer) {
var pinchScale = sender.scale
pinchScale = round(pinchScale * 1000) / 1000.0
if (pinchScale < 1) {
labelTest.font = UIFont( name: "arial", size: labelTest.font.pointSize - pinchScale)
}
else{
labelTest.font = UIFont( name: "arial", size: labelTest.font.pointSize + pinchScale)
}
}
You also need to limit the text growing, as per the requirement.
Hope this would help you.
Upvotes: 5