按比例缩小的UILabels的质量

时间:2017-06-13 17:32:58

标签: ios swift uiview uilabel scaling

我使用Swift 3创建了一个iOS界面,其中包含UIViewUILabel的{​​{1}}根据它们所处的位置进行放大和缩小位于屏幕上。我的第一种方法是以一个舒适的大尺寸(例如100x100)创建和填充视图,然后使用CGAffineTransform(scaleX:y:)根据需要缩放它们,但是我注意到标签中文本的缩减是&#完全没有优雅,文字变得像素化,并且在小尺度上接近不可读。作为比较(参见下面的示例),直接更改字体大小可以提供更好的结果,但是我的视图中的结构有些复杂,并且必须根据某个大小因素重绘所有内容会很麻烦。是否有更好,更顺畅的方法来解决这个问题?

这是我创建的示例项目,用于说明问题,以及模拟器中的输出(与iPhone本身相同),缩小的视图位于左侧(红色)并更改字体大小是正确的(绿色)。

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        for i in 1...10 {
            let f = CGFloat(1.0) / CGFloat(i)
            let view1 = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 50))
            view1.backgroundColor = UIColor.red
            let label1 = UILabel(frame: CGRect(x: 0, y: 0, width: 150, height: 50))
            label1.text = "\(100 / i)%"
            label1.font = UIFont(name: "Verdana", size: 24.0)
            label1.textAlignment = .right
            view1.addSubview(label1)
            view1.transform = CGAffineTransform(scaleX: f, y: f)
            view1.center = CGPoint(x: 160 - 75.0 * f, y: CGFloat(60 * i) + 25.0 * f)
            self.view.addSubview(view1)

            let view2 = UIView(frame: CGRect(x: CGFloat(170), y: CGFloat(60 * i), width: 150 * f, height: 50 * f))
            view2.backgroundColor = UIColor.green
            let label2 = UILabel(frame: CGRect(x: 0, y: 0, width: 150 * f, height: 50 * f))
            label2.text = "\(100 / i)%"
            label2.font = UIFont(name: "Verdana", size: 24.0 * f)
            view2.addSubview(label2)
            self.view.addSubview(view2)

        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

downscaling the view vs. changing the font size

1 个答案:

答案 0 :(得分:1)

这个可能是一个答案 - 但不太适合评论,所以...

尝试一下 - 它使用.adjustsFontSizeToFitWidth创建黄色背景视图的第3个“列”。字体大小将根据包含标签的视图大小自动调整。

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    for i in 1...10 {
        let f = CGFloat(1.0) / CGFloat(i)
        let view1 = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 50))
        view1.backgroundColor = UIColor.red
        let label1 = UILabel(frame: CGRect(x: 0, y: 0, width: 150, height: 50))
        label1.text = "\(100 / i)%"
        label1.font = UIFont(name: "Verdana", size: 24.0)
        label1.textAlignment = .right
        view1.addSubview(label1)
        view1.transform = CGAffineTransform(scaleX: f, y: f)
        view1.center = CGPoint(x: 160 - 75.0 * f, y: CGFloat(60 * i) + 25.0 * f)
        self.view.addSubview(view1)

        let view2 = UIView(frame: CGRect(x: CGFloat(170), y: CGFloat(60 * i), width: 150 * f, height: 50 * f))
        view2.backgroundColor = UIColor.green
        let label2 = UILabel(frame: CGRect(x: 0, y: 0, width: 150 * f, height: 50 * f))
        label2.text = "\(100 / i)%"
        label2.font = UIFont(name: "Verdana", size: 24.0 * f)
        view2.addSubview(label2)
        self.view.addSubview(view2)

        let view3 = UIView(frame: CGRect(x: CGFloat(270), y: CGFloat(60 * i), width: 150 * f, height: 50 * f))
        view3.backgroundColor = UIColor.yellow
        let label3 = UILabel(frame: view3.bounds)
        label3.text = "\(100 / i)%"
        label3.font = UIFont(name: "Verdana", size: 24.0)
        label3.adjustsFontSizeToFitWidth = true
        label3.minimumScaleFactor = 0.05
        label3.numberOfLines = 0
        // we want the label to resize with the view, if the view frame changes
        label3.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view3.autoresizesSubviews = true
        view3.addSubview(label3)
        self.view.addSubview(view3)

    }
}