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

查看:91
本文介绍了按字符数设置每个设备的帧(CGRect)-Swift 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建标签和字符,我想通过字符设置UILabel的框架(X,Y和宽度),并且Y轴对于所有设备都是恒定的,所以我如何设置X轴的中心并通过计数来控制字符,每个设备的宽度(50)越来越小?

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 ??

这是我的代码:

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和宽度)变小和最大,例如,如果字符数为9,则它必须位于X轴的中心,宽度必须变小,并且设备的左右间距.

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.

我该怎么办?!

谢谢:)

推荐答案

这是解决方案:

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)

        }

    }

}

这篇关于按字符数设置每个设备的帧(CGRect)-Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆