按字符数设置每个设备的框架(CGRect)-Swift 3[英] Set frame (CGRect) per devices by count of characters - Swift 3

本文是小编为大家收集整理的关于按字符数设置每个设备的框架(CGRect)-Swift 3的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在创建标签和字符,我想要按字符和y轴的uilabel设置帧(x,y和width),因此我如何设置x轴的中心并通过计数来控制字符和宽度(50)的每个设备越来越大?

这是我的代码:

func createTarget(id: Int) {

     listdata = dbHelpr.getDatabase(rowId: id)

     for data in listdata {

         let lengthOfChar : CGFloat = data.ans.length
         let targetSize : CGFloat = self.view.frame.width / 2
         var xAxis : CGFloat = self.view.frame.width / 2 + 55
         let yAxis : CGFloat = self.view.frame.height / 2 + 70
         let targetLabel = UILabel(frame: CGRect(x: xAxis, y: yAxis, width: 50, height: 5))
         xAxis -= 50 + (lengthOfChar)

     }
}

在此处输入图像说明

在这张图片中,它的标签是其位置是iPhone 7的X轴中心,以及模拟器的数量和标签的数量,因此我想要在所有设备和宽度中像这个位置(x,y和width)一样变得更小,最大,如果字符的数量为9,则必须在X轴的中心,并且宽度必须变小,并且设备的左右空格.

我该怎么做?!

谢谢:)

推荐答案

这是解决方案:

func createTarget(id: Int) {

    listdata = dbHelpr.getDatabase(rowId: id)

    for data in listdata {

        let lengthOfChar : CGFloat = data.ans.length
        let yAxis : CGFloat = self.view.frame.height / 2 + 70

        let width: CGFloat = view.frame.size.width - 40 // frame width
        var targetWidth: CGFloat = (width - (lengthOfChar - 1) * 5) / lengthOfChar

        if targetWidth > 50 {
            targetWidth = 50
        }

        let totalWidth: CGFloat = (targetWidth * lengthOfChar) + ((lengthOfChar - 5) * 5)

        for (indexTar, tar) in data.ans.characters.enumerated() {

            let x : CGFloat = (width / 2) - (totalWidth / 2)
            let xx : CGFloat = (CGFloat(indexTar) * targetWidth) + (CGFloat(indexTar) * 5) + 20
            var xAxis : CGFloat = (x + xx)
            xAxis = width - xAxis

            let targetLabel = UILabel(frame: CGRect(x: xAxis, y: yAxis, width: targetWidth, height: 5))

            targetLabel.backgroundColor = .white
            targetLabel.layer.masksToBounds = true
            targetLabel.layer.cornerRadius = 5
            targetLabel.text = String(describing: tar)
            targetLabel.textAlignment = .center
            targetLabel.textColor = .white
            self.view.addSubview(targetLabel)

        }

    }

}

本文地址:https://www.itbaoku.cn/post/2091188.html

问题描述

I'm creating label and characters, I want set frame (X, Y and width) of UILabel by characters and Y-axis is constant of all devices, so how can I set center of X-axis and controlling by count of characters, and width (50) gets smaller and bigger per device ??

This my code :

func createTarget(id: Int) {

     listdata = dbHelpr.getDatabase(rowId: id)

     for data in listdata {

         let lengthOfChar : CGFloat = data.ans.length
         let targetSize : CGFloat = self.view.frame.width / 2
         var xAxis : CGFloat = self.view.frame.width / 2 + 55
         let yAxis : CGFloat = self.view.frame.height / 2 + 70
         let targetLabel = UILabel(frame: CGRect(x: xAxis, y: yAxis, width: 50, height: 5))
         xAxis -= 50 + (lengthOfChar)

     }
}

enter image description here

In this picture is my label its position is center of X-axis of iPhone 7 plus simulator and numbers of label it's per count of characters, so I want like this position (X, Y and width) in all devices and width gets smaller and biggest and if count of characters for example is 9 it's must be on center of X-axis and width must be gets smaller little and spaces right and left of device.

How can I do it ?!

Thank you :)

推荐答案

This is the solution :

func createTarget(id: Int) {

    listdata = dbHelpr.getDatabase(rowId: id)

    for data in listdata {

        let lengthOfChar : CGFloat = data.ans.length
        let yAxis : CGFloat = self.view.frame.height / 2 + 70

        let width: CGFloat = view.frame.size.width - 40 // frame width
        var targetWidth: CGFloat = (width - (lengthOfChar - 1) * 5) / lengthOfChar

        if targetWidth > 50 {
            targetWidth = 50
        }

        let totalWidth: CGFloat = (targetWidth * lengthOfChar) + ((lengthOfChar - 5) * 5)

        for (indexTar, tar) in data.ans.characters.enumerated() {

            let x : CGFloat = (width / 2) - (totalWidth / 2)
            let xx : CGFloat = (CGFloat(indexTar) * targetWidth) + (CGFloat(indexTar) * 5) + 20
            var xAxis : CGFloat = (x + xx)
            xAxis = width - xAxis

            let targetLabel = UILabel(frame: CGRect(x: xAxis, y: yAxis, width: targetWidth, height: 5))

            targetLabel.backgroundColor = .white
            targetLabel.layer.masksToBounds = true
            targetLabel.layer.cornerRadius = 5
            targetLabel.text = String(describing: tar)
            targetLabel.textAlignment = .center
            targetLabel.textColor = .white
            self.view.addSubview(targetLabel)

        }

    }

}