Easier hugging / compression handling

:  ~ 1 min read

I'm pretty sure this won't suit all cases, but, usually, a label / button should highly resist being vertically shrunk more than its intrinsic size. On the other hand, we won't always mind if it grows larger than its intrinsic size, but we'd like to avoid it, if possible.

I, personally, find this a bit of a mouthful:

label.setContentCompressionResistancePriority(UILayoutPriorityRequired, forAxis: .Vertical)
label.setContentHuggingResistancePriority(UILayoutPriorityDefaultHigh, forAxis: .Vertical)

So, let's extract them into a method, with default values, as added bonus, and use an enum, as extra bonus:

enum Priority: UILayoutPriority {
  case Required = 1000 // UILayoutPriorityRequired
  case High = 750 // UILayoutPriorityDefaultHigh
  case Low = 250 // UILayoutPriorityDefaultLow
  case Fitting = 50 // UILayoutPriorityFittingSizeLevel
  case None = 1 // For the cases where we don't care, since 0 is not a valid value
}

func setVerticalContentPriorities(compression compression: Priority = .Required, hugging: Priority = .High) {
  // I doubt the checks are really needed, but I also doubt they hurt
  if compression.rawValue != contentCompressionResistancePriorityForAxis(.Vertical) {
	setContentCompressionResistancePriority(compression.rawValue, forAxis: .Vertical)
  }
  if hugging.rawValue != contentHuggingPriorityForAxis(.Vertical) {
	setContentHuggingPriority(hugging.rawValue, forAxis: .Vertical)
  }
}

For hugging, DefaultLow is the default, while for compression it's DefaultHigh, so we're upping them one level. Now it's much simpler to use (and easy to extrapolate to .Horizontal):

label1.setVerticalContentPriorities()
label2.setVerticalContentPriorities(hugging: .Required)