Swift计算文本宽高

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

iOS 8 开始可以配合 AutoLayout 自动估算文本的高度,但是当 Cell 比较复杂的时候,还会需要手动去计算。首先声明一个样式

var TextStyle : [String : NSObject] {get{

let paraStyle=NSMutableParagraphStyle()

paraStyle.minimumLineHeight=17.3paraStyle.lineSpacing=0paraStyle.lineBreakMode=NSLineBreakMode.ByWordWrapping

paraStyle.paragraphSpacing=0paraStyle.paragraphSpacingBefore=0return[NSForegroundColorAttributeName: UIColor.blackColor(),

NSKernAttributeName: CGFloat(0.5),

NSFontAttributeName: UIFont.systemFontOfSize(15.0),

NSParagraphStyleAttributeName: paraStyle

]

}

}

如果你的文本里含有很多特殊字符,例如颜文字,通过 NSKernAttributeName 设置字间距可以算出更宽松的结果。

以 UILabel 为例,设置其 attributedText

label.attributedText = NSAttributedString(string:"Hello World", attributes: TextStyle))

再利用 boundingRectWithSize 这个方法来计算其约束后的高度,传入一个 width 为宽度约束,然后利用我们声明的样式来计算高度。

func sizeHeightWithText(attrString: NSString, width: CGFloat, textAttributes: [NSObject : AnyObject]) ->CGSize {

var rect= attrString.boundingRectWithSize(CGSizeMake(width, CGFloat.max), options: .UsesLineFragmentOrigin |.UsesFontLeading, attributes: textAttributes, context: nil)returnCGSize(width: rect.width, height: rect.height)

}

如果你使用的是 UITextView,那么还需要对 UITextView 做一些处理来去掉他的边距。

textView.textContainer.lineFragmentPadding =0textView.textContainerInset= UIEdgeInsetsZero

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航