Ios 计算属性字符串高度
我正在尝试将tableViewCell高度设置为单元格内Ios 计算属性字符串高度,ios,swift,Ios,Swift,我正在尝试将tableViewCell高度设置为单元格内attributedString的高度。然而,无论我做什么,它似乎没有正确的大小。这就是我到目前为止所尝试的 单元高度 //Convert description to NSAttributedString and get height //width is equal to screen width - right and left offset //at the end add the bottom and height offset
attributedString
的高度。然而,无论我做什么,它似乎没有正确的大小。这就是我到目前为止所尝试的
单元高度
//Convert description to NSAttributedString and get height
//width is equal to screen width - right and left offset
//at the end add the bottom and height offset to the cell height
return detailPetViewModel!.description
.lineSpacing(spacing: 4)
.heightWithConstrainedWidth(width: Sizes.screenWidth - 40) + 10
自定义单元格子类中的descLabel
//Customize descLabel
descLabel.font = FontFamily.Avenir.Regular.font(size: 16)
descLabel.textColor = UIColor(named: .SecondaryTextColor)
//Multiple lines
descLabel.numberOfLines = 0
descLabel.lineBreakMode = .byWordWrapping
descLabel.sizeToFit()
行间距扩展
extension String {
func lineSpacing(spacing: CGFloat) -> NSAttributedString {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = spacing
let attributedString = NSMutableAttributedString(string: self)
attributedString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attributedString.length))
return attributedString
}
}
extension NSAttributedString {
func heightWithConstrainedWidth(width: CGFloat) -> CGFloat {
let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, context: nil)
return boundingBox.height
}
}
高度扩展
extension String {
func lineSpacing(spacing: CGFloat) -> NSAttributedString {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = spacing
let attributedString = NSMutableAttributedString(string: self)
attributedString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attributedString.length))
return attributedString
}
}
extension NSAttributedString {
func heightWithConstrainedWidth(width: CGFloat) -> CGFloat {
let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, context: nil)
return boundingBox.height
}
}
这是我使用的扩展名。您不必传入任何关于字体或行距的信息,因为属性字符串已经知道其值
extension NSAttributedString {
func height(containerWidth: CGFloat) -> CGFloat {
let rect = self.boundingRect(with: CGSize.init(width: containerWidth, height: CGFloat.greatestFiniteMagnitude),
options: [.usesLineFragmentOrigin, .usesFontLeading],
context: nil)
return ceil(rect.size.height)
}
func width(containerHeight: CGFloat) -> CGFloat {
let rect = self.boundingRect(with: CGSize.init(width: CGFloat.greatestFiniteMagnitude, height: containerHeight),
options: [.usesLineFragmentOrigin, .usesFontLeading],
context: nil)
return ceil(rect.size.width)
}
}
这样使用:
let height = detailPetViewModel!.description.height(containerWidth: Sizes.screenWidth - 40 + 10)
您只需使用(NSAttributedString的实例).size.height
e、 g
让a=NSAttributedString(字符串:“a字符串”,属性:[NSAttributedString.Key.font:UIFont.boldSystemFont(大小:14)])
设高度=a.尺寸().高度
为什么不使用动态单元格高度的优势呢?您需要对示例中的语法做一点小的修改(否则,非常正确):您使用了两个句点(detailPetViewModel!.description..height),而它应该是一个句点(detailPetViewModel!.description.height)。请注意“Font”属性必须在属性字符串中设置,否则计算将出错。考虑给定的宽度如何?