0

在 IB 中,无法在标签的对齐属性上添加变体。我的需要是在宽度紧凑时将文本对齐在左侧,而在宽度正常时将文本居中。

我能怎么做?

谢谢

4

2 回答 2

2

您可以将此行为添加到

override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
super.willTransition(to: newCollection, with: coordinator)
switch newCollection.verticalSizeClass {
        case .compact:
            yourLabel.textAligment = UITextAligment.left
        case .regular, .unspecified:
            yourLabel.textAligment = UITextAligment.center
        }
 }

确定旋转使用verticalSizeClass,确定设备类型(如iPad 或iPhone)使用horizo​​ntalSizeClass。

于 2017-08-08T12:42:30.007 回答
0
if(width >= yourDesiredWidth){ 
    yourLabel.textAligment = UITextAligment.center
}
else{
    yourLabel.textAligment = UITextAligment.left
}
于 2017-08-08T12:36:27.960 回答