【发布时间】:2015-03-03 00:32:41
【问题描述】:
如果不设置明确的首选宽度,我无法让 UILabel 自动换行。请帮忙。
这是我在 UITableViewCell 中的 UILabel 界面生成器
我的结果是:
但是,如果我指定一个明确的首选宽度,如下所示:
我得到以下信息:
虽然它有一些奇怪的垂直间距,但至少它是正确换行的。
另外,这是我在 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
}
并将 rowHeight 和estimatedRowHeigh 定义为:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.estimatedRowHeight = 100.0
self.tableView.rowHeight = UITableViewAutomaticDimension
}
我真的很想在不必指定明确高度的情况下让它工作。另外,我玩过 contentCompressionResistancePriority 没有运气。
这绝对类似于以下 SO 问题:Using iOS 8 flexible table cells, cell height changes but text doesn't wrap
但是,那里的解决方案似乎不起作用。事实上,类似的问题还有很多。不过,我似乎无法让它们工作。
编辑:来自 main.storyboard 的 tableViewCell 的 XML
<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>
【问题讨论】:
标签: ios xcode uitableview swift uilabel