AutoLayout multiline UILabel cutting off some text
Found an even better answer after reading this: http://johnszumski.com/blog/auto-layout-for-table-view-cells-with-dynamic-heights
creating a UILabel
subclass to override layoutSubviews
like so:
- (void)layoutSubviews{ [super layoutSubviews]; self.preferredMaxLayoutWidth = CGRectGetWidth(self.bounds); [super layoutSubviews];}
This ensures the preferredMaxLayoutWidth
is always correct.
Update:
After several more release's of iOS and testing more usecase's i've found the above to not be sufficient for every case. As of iOS 8 (I believe), under some circumstances only didSet
bounds
was called in time before the screen load.
In iOS 9 very recently I came across another issue when using a modal UIVIewController
with a UITableView
, that both layoutSubviews
and set bounds
were being called after heightForRowAtIndexPath
. After much debugging the only solution was to override set frame
.
The below code now seems to be necessary to ensure it works across all iOS's, controllers, screen sizes etc.
override public func layoutSubviews(){ super.layoutSubviews() self.preferredMaxLayoutWidth = self.bounds.width super.layoutSubviews()}override public var bounds: CGRect{ didSet { self.preferredMaxLayoutWidth = self.bounds.width }}override public var frame: CGRect{ didSet { self.preferredMaxLayoutWidth = self.frame.width }}
I'm not sure I understand you right and my solution is really far from the best one, but here it is:
I've added a height constraint on label that was cut, connected that constraint to cell's outlet. I've overrided layoutSubview method:
- (void) layoutSubviews { [super layoutSubviews]; [self.badLabel setNeedsLayout]; [self.badLabel layoutIfNeeded]; self.badLabelHeightConstraint.constant = self.badLabel.intrinsicContentSize.height;}
and viola! Please try that method and tell me results, thank you.
Omg, just have had the same issue. The @Simon McLoughlin's answer did not help me. But
titleLabel.adjustsFontSizeToFitWidth = true
did a magic! It works, I don't know why but it does.And yes, your label must have an explicit preferredMaxLayoutWidth value either.