不使用“\n”获取 UITextView 的行数?
Get number of lines UITextView without using "\n"?
我有一个 textView,我需要它的行数,或者在它变为 sizeToFit 之前需要 sizeToFit。我试过检查 textView 中的换行符“\n”的数量。但是,textView 中的文本已被解析,因为它来自服务器非常混乱,有大量 html 代码和可怕的换行符,我不得不使用 "\n" 的 replaceOccurence 来删除换行符将文本分成一个漂亮的段落。
var rawString = self.selectedProduct.description
var cleanString = rawString?.stringByReplacingOccurrencesOfString("\n", withString: " ", options: NSStringCompareOptions.LiteralSearch, range: nil)
这段代码,当然只会在上面的代码之后return1。
let desString = self.descriptionTextView.text
let numLines = desString.componentsSeparatedByString("\n")
println(numLines.count)
此代码崩溃。
let numLines = self.descriptionTextView.contentSize.height / self.descriptionTextView.font.leading
最终,我想要部分显示文本并有一个显示所有文本的按钮。
谢谢
我已将@aBilal17 的评论转换为未折旧的内容和swift。
// This gives you the size of that string
let font = UIFont.systemFontOfSize(11)
let style = NSMutableParagraphStyle()
style.lineBreakMode = NSLineBreakMode.ByWordWrapping
let size = someString.sizeWithAttributes([NSFontAttributeName: font, NSParagraphStyleAttributeName: style])
我使用我制作的这个扩展来将字符串的大小限制在宽度范围内:
extension String {
func sizeForWidth(width: CGFloat, font: UIFont) -> CGSize {
let attr = [NSFontAttributeName: font]
let height = NSString(string: self).boundingRectWithSize(CGSize(width: width, height: CGFloat.max), options:.UsesLineFragmentOrigin, attributes: attr, context: nil).height
return CGSize(width, ceil(height))
}
}
所以在你的情况下你可以这样做:
let numLines = desString.sizeForWidth(descriptionTextView.contentSize.width, font: descriptionTextView.font).height / descriptionTextView.font.lineHeight
我有一个 textView,我需要它的行数,或者在它变为 sizeToFit 之前需要 sizeToFit。我试过检查 textView 中的换行符“\n”的数量。但是,textView 中的文本已被解析,因为它来自服务器非常混乱,有大量 html 代码和可怕的换行符,我不得不使用 "\n" 的 replaceOccurence 来删除换行符将文本分成一个漂亮的段落。
var rawString = self.selectedProduct.description
var cleanString = rawString?.stringByReplacingOccurrencesOfString("\n", withString: " ", options: NSStringCompareOptions.LiteralSearch, range: nil)
这段代码,当然只会在上面的代码之后return1。
let desString = self.descriptionTextView.text
let numLines = desString.componentsSeparatedByString("\n")
println(numLines.count)
此代码崩溃。
let numLines = self.descriptionTextView.contentSize.height / self.descriptionTextView.font.leading
最终,我想要部分显示文本并有一个显示所有文本的按钮。
谢谢
我已将@aBilal17 的评论转换为未折旧的内容和swift。
// This gives you the size of that string
let font = UIFont.systemFontOfSize(11)
let style = NSMutableParagraphStyle()
style.lineBreakMode = NSLineBreakMode.ByWordWrapping
let size = someString.sizeWithAttributes([NSFontAttributeName: font, NSParagraphStyleAttributeName: style])
我使用我制作的这个扩展来将字符串的大小限制在宽度范围内:
extension String {
func sizeForWidth(width: CGFloat, font: UIFont) -> CGSize {
let attr = [NSFontAttributeName: font]
let height = NSString(string: self).boundingRectWithSize(CGSize(width: width, height: CGFloat.max), options:.UsesLineFragmentOrigin, attributes: attr, context: nil).height
return CGSize(width, ceil(height))
}
}
所以在你的情况下你可以这样做:
let numLines = desString.sizeForWidth(descriptionTextView.contentSize.width, font: descriptionTextView.font).height / descriptionTextView.font.lineHeight