第一 如果在Cell中用自动布局,拖好constrains,改变Cell的高度。
第二 如果是自己添加的,计算字符串的高宽,然后改变frame,计算高宽用
我写的一个extension嘛
extension NSString {
func textSizeWithFont(font: UIFont, constrainedToSize size:CGSize) -> CGSize {
var textSize:CGSize!
if CGSizeEqualToSize(size, CGSizeZero) {
let attributes = NSDictionary(object: font, forKey: NSFontAttributeName)
textSize = self.sizeWithAttributes(attributes)
} else {
let option = NSStringDrawingOptions.UsesLineFragmentOrigin
let attributes = NSDictionary(object: font, forKey: NSFontAttributeName)
let stringRect = self.boundingRectWithSize(size, options: option, attributes: attributes, context: nil)
textSize = stringRect.size
}
return textSize
}
}
constrainedToSize 如果一行,用CGSizeZero,否则用CGSizeMake(你想要的宽度, CGFloat(MAXFLOAT)