我希望实现的目标,其中三个是UILabels:
规格:
left
和right
是UIViews。middle
居中,即使侧面视图的大小不同。middle
和left
或middle
和right
之间发生碰撞时,middle
中的文字会在left
和{{{{}}}时被截断1}}不要改变。问题:
我不能同时满足要求(2)和(3)。
right
实现
middle.autoAlignAxisToSuperviewAxis(.Vertical)
和leftLabel
。rightLabel
和middle.autoPinEdge(.Left, toEdge: .Right, ofView: leftLabel)
的相同策略实现的。
rightLabel
位于middle
和leftLabel
之间的区域,如果最后两个不具有相同的大小,则rightLabel
不再是以超级视图为中心。 两个应用都会产生约束冲突,我不知道如何修复它,因此我的问题是:
如何将UILabel居中并将其截断为具有不同尺寸侧视图的重叠?
答案 0 :(得分:1)
您只需要添加两个约束,一个是中间和左侧之间的水平间距,将关系设置为大于或等于'和常量,例如10;另一种是中间和右边之间的水平间距,与约束一样的配置相同。
当中间标签的文本太长时,下面的约束将阻止中间与标签左右重叠。
添加示例代码:
我想你已经在故事板或代码中将约束设置为UILabel
左右。以下示例用于配置UILabel
中间
func setupConstraint() {
self.middle = UILabel()
self.middle!.backgroundColor = UIColor.yellowColor()
self.view.addSubview(self.middle!)
self.middle!.numberOfLines = 0
self.middle!.translatesAutoresizingMaskIntoConstraints = false
let middle = self.middle!
var constraint = NSLayoutConstraint(item: middle, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: left, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0)
self.view.addConstraint(constraint)
constraint = NSLayoutConstraint(item: middle, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.GreaterThanOrEqual, toItem: left, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 10)
self.view.addConstraint(constraint)
constraint = NSLayoutConstraint(item: right, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.GreaterThanOrEqual, toItem: middle, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 10)
constraint.priority = 1000
self.view.addConstraint(constraint)
constraint = NSLayoutConstraint(item: middle, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
self.view.addConstraint(constraint)
constraint = NSLayoutConstraint(item: middle, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.GreaterThanOrEqual, toItem: middle, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 100)
self.view.addConstraint(constraint)
self.middle!.text = "self.middle.translatesAutoresizingMaskIntoConstraints = false"
}
答案 1 :(得分:1)
我试图在操场上设置解决方案:
import UIKit
import XCPlayground
let viewController = UIViewController()
XCPlaygroundPage.currentPage.liveView = viewController.view
viewController.view.backgroundColor = UIColor.whiteColor()
let leftView = UIView()
leftView.translatesAutoresizingMaskIntoConstraints = false
leftView.backgroundColor = UIColor.greenColor()
let leftLabel = UILabel()
leftLabel.translatesAutoresizingMaskIntoConstraints = false
leftLabel.text = "leftleftleftleftleftleftleftleftleftleftleftleftleftleft"
leftView.addSubview(leftLabel)
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-2-[leftLabel]-2-|", options: [], metrics: nil, views: ["leftLabel": leftLabel]))
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-2-[leftLabel]-2-|", options: [], metrics: nil, views: ["leftLabel": leftLabel]))
let middleView = UIView()
middleView.translatesAutoresizingMaskIntoConstraints = false
middleView.backgroundColor = UIColor.redColor()
let middleLabel = UILabel()
middleLabel.translatesAutoresizingMaskIntoConstraints = false
middleLabel.setContentCompressionResistancePriority(UILayoutPriorityDefaultLow, forAxis: .Horizontal)
middleLabel.text = "middle"
middleView.addSubview(middleLabel)
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-2-[middleLabel]-2-|", options: [], metrics: nil, views: ["middleLabel": middleLabel]))
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-2-[middleLabel]-2-|", options: [], metrics: nil, views: ["middleLabel": middleLabel]))
let rightView = UIView()
rightView.translatesAutoresizingMaskIntoConstraints = false
rightView.backgroundColor = UIColor.greenColor()
let rightLabel = UILabel()
rightLabel.translatesAutoresizingMaskIntoConstraints = false
rightLabel.text = "right"
rightView.addSubview(rightLabel)
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-2-[rightLabel]-2-|", options: [], metrics: nil, views: ["rightLabel": rightLabel]))
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-2-[rightLabel]-2-|", options: [], metrics: nil, views: ["rightLabel": rightLabel]))
viewController.view.addSubview(leftView)
viewController.view.addSubview(middleView)
viewController.view.addSubview(rightView)
NSLayoutConstraint(item: middleView, attribute: .CenterX, relatedBy: .Equal, toItem: viewController.view, attribute: .CenterX, multiplier: 1, constant: 0).active = true
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[leftView]->=8-[middleView]->=8-[rightView]|", options: [.AlignAllTop, .AlignAllBottom], metrics: nil, views: ["leftView": leftView, "middleView": middleView, "rightView": rightView]))
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[leftView]", options: [], metrics: nil, views: ["leftView": leftView]))
结果如下:
更新(不在自己的视图中嵌入标签):
import UIKit
import XCPlayground
let viewController = UIViewController()
XCPlaygroundPage.currentPage.liveView = viewController.view
viewController.view.backgroundColor = UIColor.whiteColor()
let leftLabel = UILabel()
leftLabel.translatesAutoresizingMaskIntoConstraints = false
leftLabel.backgroundColor = UIColor.greenColor()
leftLabel.text = "leftleftleftleftleftleftleftleftleftleftleftleftleftleft"
let middleLabel = UILabel()
middleLabel.translatesAutoresizingMaskIntoConstraints = false
middleLabel.setContentCompressionResistancePriority(UILayoutPriorityDefaultLow, forAxis: .Horizontal)
middleLabel.backgroundColor = UIColor.redColor()
middleLabel.text = "middle"
let rightLabel = UILabel()
rightLabel.translatesAutoresizingMaskIntoConstraints = false
rightLabel.backgroundColor = UIColor.greenColor()
rightLabel.text = "right"
viewController.view.addSubview(leftLabel)
viewController.view.addSubview(middleLabel)
viewController.view.addSubview(rightLabel)
NSLayoutConstraint(item: middleLabel, attribute: .CenterX, relatedBy: .Equal, toItem: viewController.view, attribute: .CenterX, multiplier: 1, constant: 0).active = true
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[leftLabel]->=8-[middleLabel]->=8-[rightLabel]|", options: [.AlignAllTop, .AlignAllBottom], metrics: nil, views: ["leftLabel": leftLabel, "middleLabel": middleLabel, "rightLabel": rightLabel]))
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[leftLabel]", options: [], metrics: nil, views: ["leftLabel": leftLabel]))
希望它有所帮助:)