设置最大行数可编辑文本视图-Swift/iOS
以下代码将设置TextView的最大行数设置最大行数可编辑文本视图-Swift/iOS,ios,swift,uitextview,Ios,Swift,Uitextview,以下代码将设置TextView的最大行数 textView.textContainer.maximumNumberOfLines = 5; textView.textContainer.lineBreakMode = .byTruncatingTail 但是,在编辑模式下,您可以继续按enter键,使其超出最大行数,并在屏幕外键入 如果您尝试键入更多内容,如何使光标不会转到下一行?试试这个 func sizeOfString (string: String, constrainedToWidt
textView.textContainer.maximumNumberOfLines = 5;
textView.textContainer.lineBreakMode = .byTruncatingTail
但是,在编辑模式下,您可以继续按enter键,使其超出最大行数,并在屏幕外键入
如果您尝试键入更多内容,如何使光标不会转到下一行?试试这个
func sizeOfString (string: String, constrainedToWidth width: Double, font: UIFont) -> CGSize {
return (string as NSString).boundingRectWithSize(CGSize(width: width, height: DBL_MAX),
options: NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes: [NSFontAttributeName: font],
context: nil).size
}
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
let newText = (textView.text as NSString).stringByReplacingCharactersInRange(range, withString: text)
var textWidth = CGRectGetWidth(UIEdgeInsetsInsetRect(textView.frame, textView.textContainerInset))
textWidth -= 5.0 * textView.textContainer.lineFragmentPadding;
let boundingRect = sizeOfString(newText, constrainedToWidth: Double(textWidth), font: textView.font!)
let numberOfLines = boundingRect.height / textView.font!.lineHeight;
return numberOfLines <= 5;
}
func sizeOfString(字符串:字符串,约束宽度:双精度,字体:UIFont)->CGSize{
返回(字符串作为NSString).boundingRectWithSize(CGSize(宽度:宽度,高度:DBL_最大值),
选项:NSStringDrawingOptions.UsesLineFragmentOrigin,
属性:[NSFontAttributeName:font],
上下文:nil)
}
func textView(textView:UITextView,shouldChangeTextInRange范围:NSRange,replacementText:String)->Bool{
让newText=(textView.text作为NSString)。StringByRePlacingCharactersRange(范围,带字符串:text)
var textWidth=CGRectGetWidth(uiedgeInSetInSetRect(textView.frame,textView.textContainerSet))
textWidth-=5.0*textView.textContainer.lineFragmentPadding;
让boundingRect=sizeOfString(newText,constrainedToWidth:Double(textWidth),font:textView.font!)
让numberOfLines=boundingRect.height/textView.font!.lineHeight;
返回numberOfLines试试这个
func sizeOfString (string: String, constrainedToWidth width: Double, font: UIFont) -> CGSize {
return (string as NSString).boundingRectWithSize(CGSize(width: width, height: DBL_MAX),
options: NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes: [NSFontAttributeName: font],
context: nil).size
}
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
let newText = (textView.text as NSString).stringByReplacingCharactersInRange(range, withString: text)
var textWidth = CGRectGetWidth(UIEdgeInsetsInsetRect(textView.frame, textView.textContainerInset))
textWidth -= 5.0 * textView.textContainer.lineFragmentPadding;
let boundingRect = sizeOfString(newText, constrainedToWidth: Double(textWidth), font: textView.font!)
let numberOfLines = boundingRect.height / textView.font!.lineHeight;
return numberOfLines <= 5;
}
func sizeOfString(字符串:字符串,约束宽度:双精度,字体:UIFont)->CGSize{
返回(字符串作为NSString).boundingRectWithSize(CGSize(宽度:宽度,高度:DBL_最大值),
选项:NSStringDrawingOptions.UsesLineFragmentOrigin,
属性:[NSFontAttributeName:font],
上下文:nil)
}
func textView(textView:UITextView,shouldChangeTextInRange范围:NSRange,replacementText:String)->Bool{
让newText=(textView.text作为NSString)。StringByRePlacingCharactersRange(范围,带字符串:text)
var textWidth=CGRectGetWidth(uiedgeInSetInSetRect(textView.frame,textView.textContainerSet))
textWidth-=5.0*textView.textContainer.lineFragmentPadding;
让boundingRect=sizeOfString(newText,constrainedToWidth:Double(textWidth),font:textView.font!)
让numberOfLines=boundingRect.height/textView.font!.lineHeight;
返回行数