Skip to content

Instantly share code, notes, and snippets.

@bnickel
Created September 15, 2014 17:28
Show Gist options
  • Save bnickel/5772c3e64f33ca5f0376 to your computer and use it in GitHub Desktop.
Save bnickel/5772c3e64f33ca5f0376 to your computer and use it in GitHub Desktop.
Shorthand declaration of NSLayoutConstraint without visual format. (For those one-off cases where you don't want to use the visual format language.)
import Cocoa
struct PartialConstraint {
private let item:AnyObject
private let attribute:NSLayoutAttribute
private let multiplier:CGFloat?
private let constant:CGFloat?
private func constraintWith(partial:PartialConstraint, relation:NSLayoutRelation) -> NSLayoutConstraint {
assert(multiplier == nil && constant == nil, "Cannot define multiplier or constant on LHS.")
return NSLayoutConstraint(item: item, attribute: attribute, relatedBy: relation, toItem: partial.item, attribute: partial.attribute, multiplier: partial.multiplier ?? 1, constant: partial.constant ?? 0)
}
private func constraintWith(value:CGFloat, relation:NSLayoutRelation) -> NSLayoutConstraint {
assert(multiplier == nil && constant == nil, "Cannot define multiplier or constant on LHS.")
return NSLayoutConstraint(item: item, attribute: attribute, relatedBy: relation, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: value)
}
}
func attr(item:AnyObject, attribute:NSLayoutAttribute) -> PartialConstraint {
return PartialConstraint(item: item, attribute: attribute, multiplier: nil, constant: nil)
}
func == (lhs:PartialConstraint, rhs:PartialConstraint) -> NSLayoutConstraint {
return lhs.constraintWith(rhs, relation:.Equal)
}
func >= (lhs:PartialConstraint, rhs:PartialConstraint) -> NSLayoutConstraint {
return lhs.constraintWith(rhs, relation:.GreaterThanOrEqual)
}
func <= (lhs:PartialConstraint, rhs:PartialConstraint) -> NSLayoutConstraint {
return lhs.constraintWith(rhs, relation:.LessThanOrEqual)
}
func == (lhs:PartialConstraint, rhs:CGFloat) -> NSLayoutConstraint {
return lhs.constraintWith(rhs, relation:.Equal)
}
func >= (lhs:PartialConstraint, rhs:CGFloat) -> NSLayoutConstraint {
return lhs.constraintWith(rhs, relation:.GreaterThanOrEqual)
}
func <= (lhs:PartialConstraint, rhs:CGFloat) -> NSLayoutConstraint {
return lhs.constraintWith(rhs, relation:.LessThanOrEqual)
}
func * (lhs:PartialConstraint, rhs:CGFloat) -> PartialConstraint {
assert(lhs.constant == nil, "Cannot assign multiplier after constant")
return PartialConstraint(item: lhs.item, attribute: lhs.attribute, multiplier: (lhs.multiplier != nil ? lhs.multiplier! * rhs : rhs), constant: nil)
}
func * (lhs:CGFloat, rhs:PartialConstraint) -> PartialConstraint {
return rhs * lhs
}
func / (lhs:PartialConstraint, rhs:CGFloat) -> PartialConstraint {
return lhs * (1 / rhs)
}
func + (lhs:PartialConstraint, rhs:CGFloat) -> PartialConstraint {
return PartialConstraint(item: lhs.item, attribute: lhs.attribute, multiplier: lhs.multiplier, constant: (lhs.constant != nil ? lhs.constant! + rhs : rhs))
}
func + (lhs:CGFloat, rhs:PartialConstraint) -> PartialConstraint {
return rhs + lhs
}
func - (lhs:PartialConstraint, rhs:CGFloat) -> PartialConstraint {
return lhs + (-rhs)
}
let view1 = NSView()
let view2 = NSView()
view1.addSubview(view2)
view1.addConstraint(attr(view2, .Width) == attr(view1, .Width) / 2 - 5)
println(view1.constraints)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment