这就是我要设置的最大行数:
self.text_description.textContainer.maximumNumberOfLines = 2
self.text_description.layoutManager.textContainerChangedGeometry(self.text_description.textContainer)
Run Code Online (Sandbox Code Playgroud)
当第3行即将开始时,光标消失.要把它放回去,我必须点击退格键.
我试图将文本输入限制为可可触摸中的UITextView.我真的想限制行数而不是字符数.到目前为止,我有这个来计算行数:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:@"\n"]) {
rows++;
}
NSLog(@"Rows: %i", rows);
return YES;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果自动换行而不是用户按下返回键,则不起作用.有没有办法检查文本是否包装类似于检查"\n"?
谢谢.
有没有办法"准确"限制UITextView中针对目标iOS 5.0的行数?
因为我在堆栈溢出搜索.我在之前的链接中发现了这些问题.
在UITextView中,如何获得下一个输入将开始另一行的点
限制UITextview
的行数限制UITextView中的行数
但是UITextView当我试图决定是否返回YES或NO时,我仍然无法获得准确的行数textView:shouldChangeTextInRange:replacementText:.我曾尝试使用代码,它是UITextView中限制文本的答案,修改后的代码(在答案中删除-15)如下所示.
- (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString*)aText
{
NSString* newText = [aTextView.text stringByReplacingCharactersInRange:aRange withString:aText];
// TODO - find out why the size of the string is smaller than the actual width, so that you get extra, wrapped characters unless you take something off
CGSize tallerSize = CGSizeMake(aTextView.frame.size.width,aTextView.frame.size.height*2); // pretend there's more vertical space to get that extra line to check on
CGSize newSize = [newText …Run Code Online (Sandbox Code Playgroud) 在情节提要中,是否可以设置最大行数(UITextView类似于使用UILabel?)?所有其他SO帖子仅显示编程解决方案。