I've constrained a UILabel to leading, and trailing anchors, the intrinsicContentSize exceeds the anchors I've specified. Just to be sure, I added a widthAnchor as well, still no improvement.
Then I added these properties:
self.preferredMaxLayoutWidth = 356.25 // didn't hard code
self.invalidateIntrinsicContentSize()
Still the intrinsicContentSize is equal to 356.5, which is something I don't expect from my UILabel, is there anyway to fix this issue?
I'd have to go look up how preferredMaxLayoutWidth
interacts with intrinsicContentSize
, but assuming an interaction its likely the 356.25 is getting rounded up to 356.5 since .25 won't address a whole pixel.
But more specifically, what are you trying to use intrinsicContentSize
here? Its not meant to be a way to measure a view size, or generally inspected by client code, but rather a way to signal values to auto layout that cannot be communicated via constraints (in this case, text measurement).
So even if your layout width is (for example) 350, the intrinsic content size can continue to return something bigger (such as 356.5) but the other constraints in your system ensure the view lays out as expected (at least in general). If you want to get a measurement of the label under specific conditions, you should instead call systemLayoutSizeFitting()
, which allows you to specify specific size limitations.