您的位置:首页 > 移动开发 > Swift

【iOS】swift-文字宽度的计算

2016-02-23 14:20 411 查看


如图所示,需要sectionView的标题宽度可以动态变化

举例说明:

只需在tableView的代理方法

func tableView(tableView:
UITableView, viewForHeaderInSection section:
Int) -> UIView? {

let view = tableView.dequeueReusableHeaderFooterViewWithIdentifier(sectionViewId)
as! NameListSectionView

view.updateLabel(“xxxxxxxxxx”)

return view

}

然后在sectionView文件里

func updateLabel(labelText:
String){

label.text = labelText

let statusLabelSize = labelText.sizeWithAttributes([NSFontAttributeName
: UIFont.systemFontOfSize(13)])

labelWidthConstraint.constant = statusLabelSize.width
+ 4

layoutIfNeeded()

}

labelWidthConstraint为Xib文件里 label宽度的约束,右边横线的左边设置为到label右端的间隔即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: