Reputation: 2312
I can't get the UILabel to automatically word wrap without setting an explicit preferred width. Please help.
Here's my Interface Builder for UILabel in a UITableViewCell
And my result is:
However, if I specify an explicit preferredWidth as in:
I get the following:
Granted it has some weird vertical spacing, but at least it's wrapping correctly.
Also, here's how I'm building the row in my UITableViewController:
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier(Constants.CellReuseIdentifier, forIndexPath: indexPath) as? BurnFeedTableViewCell ??
BurnFeedTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: Constants.CellReuseIdentifier)
cell.nameOfBurnLabel?.text = object.objectForKey("name") as? String
if let owner = object.objectForKey("owner") as? PFUser {
cell.ownerLabel?.text = owner.username
} else {
cell.ownerLabel?.text = "unknown"
}
return cell
}
and defining the rowHeight & estimatedRowHeigh as:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.estimatedRowHeight = 100.0
self.tableView.rowHeight = UITableViewAutomaticDimension
}
I would really like to get this working without having to specify an explicit height. Also, I have played around with the contentCompressionResistancePriority with no luck.
This is definitely similar to the following SO Question: Using iOS 8 flexible table cells, cell height changes but text doesn't wrap
However, the solution there doesn't seem to be working. In fact, there are a few similar questions. I cannot seem to get them to work though.
EDIT: XML of the tableViewCell from main.storyboard
<tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="Burn Feed Cell"
rowHeight="95" id="bif-hV-Vqt" customClass="BurnFeedTableViewCell" customModule="Burn"
customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="600" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES"
contentMode="center" tableViewCell="bif-hV-Vqt" id="KGP-Sx-cZC">
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251"
verticalHuggingPriority="251" text="owner" lineBreakMode="tailTruncation"
baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO"
translatesAutoresizingMaskIntoConstraints="NO" id="TZ9-cx-K29">
<rect key="frame" x="8" y="37" width="48" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251"
verticalHuggingPriority="251" text="Burn Name" lineBreakMode="wordWrap" numberOfLines="0"
baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" preferredMaxLayoutWidth="90"
translatesAutoresizingMaskIntoConstraints="NO" id="v96-dx-9z5">
<rect key="frame" x="8" y="8" width="584" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251"
verticalHuggingPriority="251" text="countdown" lineBreakMode="tailTruncation"
baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO"
translatesAutoresizingMaskIntoConstraints="NO" id="6YZ-ne-79f">
<rect key="frame" x="506" y="37" width="86" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<constraints>
<constraint firstItem="v96-dx-9z5" firstAttribute="leading" secondItem="KGP-Sx-cZC"
secondAttribute="leadingMargin" id="Iwe-kT-1Do"/>
<constraint firstItem="6YZ-ne-79f" firstAttribute="trailing" secondItem="KGP-Sx-cZC"
secondAttribute="trailingMargin" id="JOA-d6-q3X"/>
<constraint firstItem="6YZ-ne-79f" firstAttribute="baseline" secondItem="TZ9-cx-K29"
secondAttribute="baseline" id="JXU-Hf-vVb"/>
<constraint firstItem="TZ9-cx-K29" firstAttribute="leading" secondItem="KGP-Sx-cZC"
secondAttribute="leadingMargin" id="VOG-Xh-9wd"/>
<constraint firstAttribute="bottomMargin" relation="greaterThanOrEqual" secondItem="TZ9-cx-K29"
secondAttribute="bottom" constant="5" id="am1-l0-8EV"/>
<constraint firstItem="TZ9-cx-K29" firstAttribute="top" secondItem="v96-dx-9z5" secondAttribute="bottom"
constant="8" symbolic="YES" id="jLQ-yQ-A8V"/>
<constraint firstItem="v96-dx-9z5" firstAttribute="trailing" secondItem="KGP-Sx-cZC"
secondAttribute="trailingMargin" id="sl2-Fb-sWy"/>
<constraint firstItem="v96-dx-9z5" firstAttribute="top" secondItem="KGP-Sx-cZC" secondAttribute="topMargin"
id="xt3-Em-EaP"/>
</constraints>
</tableViewCellContentView>
<connections>
<outlet property="countdownLabel" destination="6YZ-ne-79f" id="te1-QF-Nuo"/>
<outlet property="nameOfBurnLabel" destination="v96-dx-9z5" id="Wki-e1-0Np"/>
<outlet property="ownerLabel" destination="TZ9-cx-K29" id="wqh-Cj-yQX"/>
</connections>
</tableViewCell>
Upvotes: 2
Views: 1180
Reputation: 4839
Have you disabled autoresizing mask translation for all labels?
cell.nameOfBurnLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
cell.ownerLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
cell.countdownLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
At least that is what I had to do to get dynamic cell heights to work with labels.
Upvotes: 0
Reputation: 12843
Your issue is with autolayout. In order to get correct width/height wrapping you need to assure that for all elements in your UITableViewCell
are properly constrained from the left margin of the cell to the right and from the top margin to the bottom.
Upvotes: 0