使用以下代码我试图通过禁止用户在UITextView
超出特定内容大小后输入字符来限制UITextView
的高度。但问题是,对于当前代码,最后写入高度限制之后的最后一个字符,使得一个字符单独终止于其自己的行,并且该行超出了高度限制。
如何修复我的代码以使文字不超过我的高度限制?
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
var frame:CGRect = textView.frame;
frame.size.height = textView.contentSize.height;
if(frame.size.height <= 33.0){
return true
}
else {
return false
}
}
答案 0 :(得分:1)
当前代码的问题在于,当您的文字视图尚未包含替换文字时,您正在使用textView.contentSize.height
进行比较;因此,如果您的代码代表,如果文本视图的当前内容大小为&lt; = 33,它仍然允许您输入字符(即return true
),以便返回的字符实际上可能违反文本查看身高限制。
更新:我并不是非常喜欢我的原始答案,因为我认为boundingRectWithSize
会提供更清晰的解决方案。问题是,它不适合我,文本会超出行限制......直到现在。诀窍在于考虑文本容器的填充。
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
// Combine the new text with the old
let combinedText = (textView.text as NSString).stringByReplacingCharactersInRange(range, withString: text)
// Create attributed version of the text
let attributedText = NSMutableAttributedString(string: combinedText)
attributedText.addAttribute(NSFontAttributeName, value: textView.font, range: NSMakeRange(0, attributedText.length))
// Get the padding of the text container
let padding = textView.textContainer.lineFragmentPadding
// Create a bounding rect size by subtracting the padding
// from both sides and allowing for unlimited length
let boundingSize = CGSizeMake(textView.frame.size.width - padding * 2, CGFloat.max)
// Get the bounding rect of the attributed text in the
// given frame
let boundingRect = attributedText.boundingRectWithSize(boundingSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, context: nil)
// Compare the boundingRect plus the top and bottom padding
// to the text view height; if the new bounding height would be
// less than or equal to the height limit, append the text
if (boundingRect.size.height + padding * 2 <= 33.0){
return true
}
else {
return false
}
}
原始解决方案:
要使用尽可能接近当前代码的解决方案来修复此问题,您可以复制文本视图,将新文本追加到旧文本,然后仅当包含新文本的更新文本视图具有尺寸小于高度限制,例如:
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
// Combine the new text with the old
let combinedText = (textView.text as NSString).stringByReplacingCharactersInRange(range, withString: text)
// Create a duplicate of the text view with the same frame and font
let duplicateTextView = UITextView(frame: textView.frame)
duplicateTextView.font = textView.font
// Set the text view to contain the tentative new version of the text
duplicateTextView.text = combinedText
// Use sizeToFit in order to make the text view's height fit the text exactly
duplicateTextView.sizeToFit()
// Then use the duplicate text view's height for the comparison
if(duplicateTextView.frame.size.height <= 33.0){
return true
}
else {
return false
}
}
答案 1 :(得分:0)
谢谢@ lyndsey-scott,下面是为xcode 9.1中最新的sdk更新的相同代码。进行了少量编辑(将最大高度替换为变量)
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
// Combine the new text with the old
let combinedText = (textView.text as NSString).replacingCharacters(in: range, with: text)
// Create attributed version of the text
let attributedText = NSMutableAttributedString(string: combinedText)
let font = textView.font ?? UIFont.systemFont(ofSize: 12.0)
attributedText.addAttribute(NSAttributedStringKey.font, value: font, range: NSMakeRange(0, attributedText.length))
// Get the padding of the text container
let padding = textView.textContainer.lineFragmentPadding
// Create a bounding rect size by subtracting the padding
// from both sides and allowing for unlimited length
let boundingSize = CGSize(width: textView.frame.size.width - padding * 2, height: CGFloat.greatestFiniteMagnitude)
// Get the bounding rect of the attributed text in the
// given frame
let boundingRect = attributedText.boundingRect(with: boundingSize, options: NSStringDrawingOptions.usesLineFragmentOrigin, context: nil)
// Compare the boundingRect plus the top and bottom padding
// to the text view height; if the new bounding height would be
// less than or equal to the height limit, append the text
if (boundingRect.size.height + padding * 2 <= MyViewController.maximumHeaderHeight){
return true
} else {
return false
}
}