【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右端的间隔即可
相关文章推荐
- iOS 开发 -- Swift 语法篇 (二) 可选项
- swift-集合类型03-字典和数组
- swift-集合类型02-字典的基本操作
- Swift中文教程(十五) 析构
- Swift中文教程(十四) 初始化
- Swift中文教程(十三) 继承
- swift-集合类型01-数组的基本操作
- [Swift]基础
- Swift初体验(三)
- swift学习
- swift学习笔记-UI篇之UIImageView
- swift-字符串02-字符串的插值,连接,长度
- Swift开发IOS的细节
- swift-基本操作03-区间和逻辑运算符
- swift-基本操作02-自增和复合赋值运算符
- Swift 基本控件UIAlertView学习
- swift-基本操作01-赋值取余运算符
- Swift 基本控件UISlider学习
- Swift-函数
- swift 中的delegate 写法