我有一个textView,我需要的行数,或sizeToFit成为sizeToFit之前。我试着在textView中检查换行符“\ n”的数量。然而,textView中的文本已被解析bc这是非常混乱来自服务器与大量的HTML代码和可怕的换行符,我必须使用replaceOccurence“\ n”删除换行符,使文本成为一个好段落。获取行数UITextView而不使用“ n”?
var rawString = self.selectedProduct.description
var cleanString = rawString?.stringByReplacingOccurrencesOfString("\n", withString: " ", options: NSStringCompareOptions.LiteralSearch, range: nil)
这段代码当然只会在上面的代码后返回1。
let desString = self.descriptionTextView.text
let numLines = desString.componentsSeparatedByString("\n")
println(numLines.count)
此代码崩溃。
let numLines = self.descriptionTextView.contentSize.height/self.descriptionTextView.font.leading
最终,我想部分显示文本,并有一个显示所有文本的按钮。
感谢
numLines = textView.contentSize.height/textView.font.lineHeight 检查它plz – aBilal17
你也可以检查它。 UIFont * font = [UIFont boldSystemFontOfSize:11.0]; CGSize size = [string sizeWithFont:font constrainedToSize:myUITextView.frame.size lineBreakMode:UILineBreakModeWordWrap]; //默认模式 float numberOfLines = size.height/font.lineHeight; – aBilal17
有趣。你的第一个解决方案会崩溃,发现“无解包可选值”错误......它每次都会给出一个3.0XX的小数。我很快就用第二种解决方案很难。 –