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

swift - 按设备组织和调整UILabel位置-Swift 3-LMLPHP

我在这张图片中的问题是:UILabel的大小和位置在每个设备中没有组织,我希望它们在X轴的中心位置

问题:

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

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

3)最后,如果字符数大于8,标签如何缩小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)

        }

    }

}

最佳答案

为了放置标签,您需要注意以下几点:


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


要确定设备种类并具有不同的逻辑,可以查看以下信息:
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()


这给出了输出:

swift - 按设备组织和调整UILabel位置-Swift 3-LMLPHP

您可能需要进行的更改:


根据您的要求更改“ totalLabels”数字
也许在第一个生成的标签上添加另一个宽度约束,以便为所有标签定义特定的标签-其余标签与第一个标签保持相同的宽度
从两侧针对不同设备调整包含的视图边距
通过预先计算标签可能使用的宽度来手动处理新行。


祝好运

关于swift - 按设备组织和调整UILabel位置-Swift 3,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45544072/

10-16 18:02