在捏手势中增加字体大小

lax*_*nal 1 uilabel ios swift

我有一个UILabel,其字体大小将增加捏手势iOS swift.我能够增加字体大小,但视图有问题.思想sizeToFit()根据需要增加高度和宽度,但它不会反映在视图上,即标签在视图中保持不变.随着字体大小的增加,请帮我增加标签的大小.

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

    }
Run Code Online (Sandbox Code Playgroud)

Bha*_*odi 5

我为演示目的做了一个测试演示,你可以看到相同的结果,

在此输入图像描述

标签的约束如下,

在此输入图像描述

您可能需要根据您必须设计的UI或要求更改约束,只需确保不设置标签的高度约束,如果必须使标签成为多线,则相应地设置约束和标签属性.

Pinch手势的代码,

@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)
    }
}
Run Code Online (Sandbox Code Playgroud)

您还需要根据要求限制文本增长.

希望这会对你有所帮助.