UITableview 中的 UITextview 高度 swift
UITextview height in UITableview swift
我在 UITableViewCell
中设置 UITextview
时遇到问题。由于函数 heightforrowatindexpath 在 cellforrowatindexpath 之前是 运行 所以我无法计算 UITextview
.[=26 的内容大小=]
我只是通过 cellforrowatindexpath
函数中的 textview.contentSize.height
来计算大小。
我基本上希望 UITextview
contentHeight 适合 UITableViewCell
,因此每个 UITextView
都看不到滚动,并且它将通过增加 UITableViewCell
的高度来显示完整内容。
我也在 UITableViewCell
中的 UITextView
上方设置了一个 UILabel
,我试图为两者设置约束但无法实现我的目标。
如何在以下函数中动态计算 UITableViewCell
的大小:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
}
P.S: 是否可以使用自动布局或其他技术根据设备高度稍微增加 UITableViewCell
高度。
您可以只传递包含内容的 textView 以计算 textView 的高度
func calculateHeight(textView:UITextView, data:String) -> CGRect {
var newFrame:CGRect!
// I was using the HTML content here. If your content is not HTML you can just pass the stringValue to your textView
do {
let attr = try NSAttributedString(data: html.dataUsingEncoding(NSUTF16StringEncoding)!, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType], documentAttributes: nil)
textView.attributedText = attr
}catch {
//Handle Exceptions
}
let fixedWidth = textView.frame.size.width
textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
newFrame = textView.frame
newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
print("height \(newFrame.height)")
return newFrame
}
您可以在 heightForRowAtIndex
中调用此方法,这样在 return 中您可以获得一个新的更新框架,您可以从中获取新的更新高度。
确保 scrollEnabled
保持 cellForRowAtIndexPath
中单元格 textView 的 false
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//Cell making here......
cell.yourTextView.scrollEnabled = false
return cell
}
}
1) 在 heightForRowAtIndexPath 中给单元格大小如下:
return UITableViewAutomaticDimension
2) ... 并添加以下内容:
override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
希望对您有所帮助...
我在 UITableViewCell
中设置 UITextview
时遇到问题。由于函数 heightforrowatindexpath 在 cellforrowatindexpath 之前是 运行 所以我无法计算 UITextview
.[=26 的内容大小=]
我只是通过 cellforrowatindexpath
函数中的 textview.contentSize.height
来计算大小。
我基本上希望 UITextview
contentHeight 适合 UITableViewCell
,因此每个 UITextView
都看不到滚动,并且它将通过增加 UITableViewCell
的高度来显示完整内容。
我也在 UITableViewCell
中的 UITextView
上方设置了一个 UILabel
,我试图为两者设置约束但无法实现我的目标。
如何在以下函数中动态计算 UITableViewCell
的大小:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
}
P.S: 是否可以使用自动布局或其他技术根据设备高度稍微增加 UITableViewCell
高度。
您可以只传递包含内容的 textView 以计算 textView 的高度
func calculateHeight(textView:UITextView, data:String) -> CGRect {
var newFrame:CGRect!
// I was using the HTML content here. If your content is not HTML you can just pass the stringValue to your textView
do {
let attr = try NSAttributedString(data: html.dataUsingEncoding(NSUTF16StringEncoding)!, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType], documentAttributes: nil)
textView.attributedText = attr
}catch {
//Handle Exceptions
}
let fixedWidth = textView.frame.size.width
textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
newFrame = textView.frame
newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
print("height \(newFrame.height)")
return newFrame
}
您可以在 heightForRowAtIndex
中调用此方法,这样在 return 中您可以获得一个新的更新框架,您可以从中获取新的更新高度。
确保 scrollEnabled
保持 cellForRowAtIndexPath
false
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//Cell making here......
cell.yourTextView.scrollEnabled = false
return cell
}
}
1) 在 heightForRowAtIndexPath 中给单元格大小如下:
return UITableViewAutomaticDimension
2) ... 并添加以下内容:
override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return UITableViewAutomaticDimension }
希望对您有所帮助...