组织和调整每个设备的 UILabel 位置 - Swift 3

Organize and adjust the UILabel position per devices - Swift 3

我有 SQLite 数据库文件和 UILabel,在将 String 转换为 Characters 后,我根据字符数为数据库中的标签文本设置了文本,并添加了扩展名(length), 它的工作是计算字符数。

我在这张照片中的问题是:UILabel 没有按设备的大小和位置进行组织我希望它们的位置位于 X 轴的 中心

问题:

1) 如何将标签设置为居中并为标签设置屏幕左右两侧的空间(每个设备)??

2) 如果设备是 iPhone 7/6S/6 加上宽度 = 50,如果设备是 iPhone 7/6S/6 宽度,如何设置标签的宽度= 45 如果设备是 iPhone SE/5S/5C/5 宽度 = 38 ??

3)最后,如果字符数大于8,label如何变小10?

这是我的代码:

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) * 60%
        let width: CGFloat = view.frame.size.width - 40 // frame width
        var targetWidth: CGFloat = (width - (lengthOfChar - 1) * 5) / lengthOfChar
        let targetHeigt : CGFloat = 5

        if lengthOfChar >= 8 {
            targetWidth = 40
        } else {
            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: targetHeigt))
            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)

        }

    }

}

为了定位您的标签,您需要做一些事情:

  1. 标签容器以保持所有内容居中并在屏幕两侧留有边距
  2. 标签之间的限制取决于设备尺寸等。
  3. 如果您需要换行 - 您也需要手动处理

识别设备种类和逻辑不同,可以查看这个帖子: iOS: How to determine the current iPhone/device model in Swift?

为了用标签填充您的视图,下面是实现此目的的示例代码:

self.view.backgroundColor = UIColor.black

let labelContainerView : UIView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
labelContainerView.translatesAutoresizingMaskIntoConstraints = false

let containerLeftConstraint : NSLayoutConstraint = NSLayoutConstraint(item: self.view, attribute: .left, relatedBy: .greaterThanOrEqual, toItem: labelContainerView, attribute: .left, multiplier: 1, constant: 8)
let containerRightConstraint : NSLayoutConstraint = NSLayoutConstraint(item: self.view, attribute: .right, relatedBy: .greaterThanOrEqual, toItem: labelContainerView, attribute: .right, multiplier: 1, constant: 8)
let containerCenterX : NSLayoutConstraint = NSLayoutConstraint(item: labelContainerView, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0)
let containerCenterY : NSLayoutConstraint = NSLayoutConstraint(item: labelContainerView, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1, constant: 0)

self.view.addSubview(labelContainerView)
self.view.addConstraints([ containerLeftConstraint, containerRightConstraint, containerCenterX, containerCenterY ])

// Add some labels
let totalLabels : Int = 10

var lastAddedLabel : UILabel? = nil

for labelAt : Int in 1 ... totalLabels
{
    var addedConstraint : [NSLayoutConstraint] = [NSLayoutConstraint]()

    let label : UILabel = UILabel(frame: CGRect.zero)
    label.translatesAutoresizingMaskIntoConstraints = false
    label.text = "_"
    label.textColor = UIColor.white
    label.textAlignment = .center
    label.adjustsFontSizeToFitWidth = true

    if (lastAddedLabel != nil)
    {
        // Add left constraint to previous label
        let leftConstraint : NSLayoutConstraint = NSLayoutConstraint(item: label, attribute: .left, relatedBy: .equal, toItem: lastAddedLabel!, attribute: .right, multiplier: 1, constant: 8)
        let equalWidth : NSLayoutConstraint = NSLayoutConstraint(item: label, attribute: .width, relatedBy: .equal, toItem: lastAddedLabel!, attribute: .width, multiplier: 1, constant: 0)

        addedConstraint.append(contentsOf: [ leftConstraint, equalWidth ])
    }
    else
    {
        // Add left constraint to super view
        let leftConstraint : NSLayoutConstraint = NSLayoutConstraint(item: labelContainerView, attribute: .left, relatedBy: .equal, toItem: label, attribute: .left, multiplier: 1, constant: 0)

        addedConstraint.append(leftConstraint)
    }

    // Add top bottom constraint
    let topConstraint : NSLayoutConstraint = NSLayoutConstraint(item: labelContainerView, attribute: .top, relatedBy: .equal, toItem: label, attribute: .top, multiplier: 1, constant: 0)
    let bottomConstraint : NSLayoutConstraint = NSLayoutConstraint(item: labelContainerView, attribute: .bottom, relatedBy: .equal, toItem: label, attribute: .bottom, multiplier: 1, constant: 0)

    addedConstraint.append(contentsOf: [ topConstraint, bottomConstraint ])

    // Add right constraint if this is the last label
    if (labelAt == totalLabels)
    {
        let rightConstraint : NSLayoutConstraint = NSLayoutConstraint(item: labelContainerView, attribute: .right, relatedBy: .equal, toItem: label, attribute: .right, multiplier: 1, constant: 0)

        addedConstraint.append(rightConstraint)
    }

    labelContainerView.addSubview(label)
    labelContainerView.addConstraints(addedConstraint)

    lastAddedLabel = label
}

self.view.layoutIfNeeded()

这给出了输出:

您可能需要进行的更改:

  1. 根据您的要求更改 "totalLabels" 号码
  2. 可能会向第一个生成的标签添加另一个宽度限制,以便为所有标签定义一个特定的 - 其余标签与第一个标签保持相同的宽度
  3. 针对不同设备从两侧调整包含视图边距
  4. 通过预先计算标签可能使用的宽度来手动处理新行。

祝你好运

我解决了我的问题和这个答案:

func createTarget(id: Int) {

    listdata = dbHelpr.getDatabase(rowId: id)
    var targetHeigt = CGFloat()
    let viewWidth = self.view.frame.size.width
    if viewWidth == 320 {
        targetHeigt = 2.5
    } else {
        targetHeigt = 5
    }

    for data in listdata {
        let yAxis : CGFloat = (self.view.frame.height) * 60%
        let i = data.ans.length
        // char count
        let width: Int = Int(view.frame.size.width) - 40

        // frame width
        var targetWidth: Int = (width - (i - 1) * 5) / i

        if targetWidth > 50 {
            targetWidth = 50
        }
        let totalWidth: Int = (targetWidth * i) + ((i - 1) * 5)

        for x in 0..<i {
            let currentWidth: Int = (width / 2) - (totalWidth / 2) + (x * targetWidth) + (x * 5) + 20

            let targetLabel = UILabel(frame: CGRect(x: CGFloat(currentWidth), y: yAxis, width: CGFloat(targetWidth), height: targetHeigt))
            targetLabel.backgroundColor = .white
            targetLabel.layer.masksToBounds = true
            targetLabel.layer.cornerRadius = 5
            targetLabel.textAlignment = .center
            targetLabel.textColor = .white

            for i in listdata {
                let tar = data.ans.characters.map{String([=10=])}
                targetLabel.text = String(describing: tar)
            }
            self.view.addSubview(targetLabel)
        }
    }
}