Zoom UILabel & Re-render font at correct size

后端 未结 8 2427
情歌与酒
情歌与酒 2020-12-13 07:23

I have a multi-line UILabel that I want to enable zooming on.

I embedded it with a UIScrollView and set min zoom to .25 and max zoom to 4.

8条回答
  •  一个人的身影
    2020-12-13 08:02

    Here is something that I have come up with:

    func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
            label.font = UIFont(name: "YourFont", size: fontSize * scale)
            label.transform = CGAffineTransformConcat(CGAffineTransformMakeScale(1 / scale, 1 / scale), CGAffineTransformMakeRotation(0))
        }
    

    Updated for Swift 4.0:

    func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
        label.font = label.font.withSize(fontSize * scale)
        label.transform = CGAffineTransform(scaleX: 1 / scale, y: 1 / scale).concatenating(CGAffineTransform(rotationAngle: 0))
    }
    

    And also remember that the label should be large enough to show the whole text. It sometimes takes a tiny bit to redraw, but this method is fairly simple.

提交回复
热议问题