限制UITextview的行数
我想知道如何限制用户在编辑 UITextField 时可以输入的行数(不是其他问题中询问的字符数)。
理想情况下,我想将输入限制为最大值。 10 行。
我需要从哪里开始?我是否用某种方法来做到这一点?在
- (BOOL)textViewShouldBeginEditing:(UITextView *)aTextView
I was wondering how to limit the amount of LINES (not characters as asked in other questions) a user can enter when editing a UITextField.
Ideally, I would like to limit the input to max. 10 lines.
Where would I need to start? Do I do this with a method? In
- (BOOL)textViewShouldBeginEditing:(UITextView *)aTextView
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
Maciek Czarnik 的回答对我来说不起作用,但它让我了解了该怎么做。
iOS 7+
Swift
ObjC
Maciek Czarnik answer does not worked for me, but it got me insights what to do.
iOS 7+
Swift
ObjC
也许这可以帮助(iOS 7+):
我猜即使是第一行也应该能达到目的,但没有......也许它是 SDK 中的一个错误
Maybe this can help (iOS 7+):
Even first line should do the trick I guess, but doesn't... Maybe its a bug in SDK
你的想法是对的,但方法是错误的。每当文本要更改时,就会调用
textView:shouldChangeTextInRange:replacementText:
;您可以使用其text
属性访问文本视图的当前内容,并且可以使用[textView.text stringByReplacingCharactersInRange:range withString:replacementText 从传递的范围和替换文本构造新内容]
。然后,您可以计算行数并返回“是”以允许更改,或返回“否”以拒绝更改。You have the right idea, but the wrong method.
textView:shouldChangeTextInRange:replacementText:
is called whenever the text is going to change; you can access the current content of the text view using itstext
property, and you can construct the new content from the passed range and replacement text with[textView.text stringByReplacingCharactersInRange:range withString:replacementText]
. You can then count the number of lines and return YES to allow the change or NO to reject it.在 Swift 3.0 版本中:
in
Swift 3.0
version:Maciek Czarnik 的答案似乎对我不起作用,即使在 iOS7 中也是如此。它给了我奇怪的行为,我不知道为什么。
我限制 UITextView 中行数的方法很简单:(
仅在 iOS7 中测试)在以下 UITextViewDelegate 方法中:
Maciek Czarnik's answer does not seem to work for me, even in iOS7. It gives me strange behavior, I don't know why.
What I do to limit the number of lines in the UITextView is simply :
(tested only in iOS7) In the following UITextViewDelegate method :
这是 Swift 4.2 / Swift 5 中 Numereyes 答案的改进版本,
我做了一些扩展,以便可以重用代码。
我正在使用 While 循环来检查尺寸是否合适。当用户一次粘贴大量文本时,这也适用。
Here's a improved Version of Numereyes answer in Swift 4.2 / Swift 5
I made a little extension so I can reuse the code.
I'm using a While-Loop to check if the size fits. This also works when the user pastes a lot of text at once.
Swift 4
如果您还想限制字符:
不要忘记在
viewDidLoad
中添加您希望限制的行数:Swift 4
And if you want to limit characters also:
don't forget to add how many lines you want the limit to be in
viewDidLoad
:给出的其他解决方案不能解决与最后创建的最后一行(问题案例中的第 11 行)相关的问题。
这是一个使用 Swift 4.0 和 Swift 4.0 的可行解决方案。 Xcode 9.0 beta(位于 这篇博文)
注意:此解决方案无法处理最后一行太长而无法显示的情况(文本将隐藏在 UITextView 的最右侧)。
The other solutions given do not solve an issue related to a last line being created at the end (an 11th line in the question's case).
Here is a working solution with
Swift 4.0
& Xcode 9.0 beta (found on this blog post)Nota bene: This solution does not handle the scenario where the last line is too long to display (text will be hidden on the far right side of the UITextView).
Swift 5.7、iOS 16
在
UITextView
(或UITextField
)的UIViewRepresentable
包装器中Swift 5.7, iOS 16
Inside your
UIViewRepresentable
wrapper forUITextView
(orUITextField
)与其他答案类似,但可以直接从情节提要使用,无需子类化:
Similar to other answers, but usable directly from Storyboard and without subclassing:
请参阅 APPLE 文档:计算文本行数 https ://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/TextLayout/Tasks/CountLines.html
}
处理“\n”
}
Refer to APPLE documentation: Counting Lines of Text https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/TextLayout/Tasks/CountLines.html
}
handle "\n"
}