Scale text label by screen size

前端 未结 7 1342
长情又很酷
长情又很酷 2020-12-08 10:13

Is there a way to scale text so that it takes up close to the same screen real estate no matter what the device size is? I\'ve found that the text on an iPad sized device is

7条回答
  •  悲&欢浪女
    2020-12-08 10:37

    I had decent behavior with the following code in my viewDidLoad function:

    (Note that the screen bounds are in 'points' not 'pixels')

        // Set the clock font size according to the height.
        //  Design was done on iPhone XR with height of 896 points and font size of 98.
        if (UIScreen.main.bounds.height != 896). // Only need code if not on original design size.
        {
            let scaleFactor: Float = Float(UIScreen.main.bounds.height) / 896.0
            let fontSize = CGFloat(98.0 * scaleFactor)
            self.clock.font = self.clock.font.withSize(fontSize)
        }
    

    This was using the "Helvetica Neue" font which is a fixed width font. It wasn't completely scaled up completely for some reason on larger devices, so still a bit smaller than the target on iPads, but close enough.

提交回复
热议问题