UITextView - 设置最大行数会隐藏光标

8
这是我正在做的设置最大行数的方法:
self.text_description.textContainer.maximumNumberOfLines = 2
self.text_description.layoutManager.textContainerChangedGeometry(self.text_description.textContainer)  

当第三行即将开始时,光标会消失。要恢复它,我必须点击退格键。


光标是隐藏还是停留在键盘后面? - byJeevan
2个回答

3
假设在文本视图中,光标不会停留在键盘后面。我已经编写了一个帮助方法,使UITextField文本滚动到文本的末尾。
- (void)scrollToCaretInTextView:(UITextView *)textView animated:(BOOL)animated
{
    CGRect rect = [textView caretRectForPosition:textView.selectedTextRange.end]; //Get the content size of textview 
    rect.size.height += textView.textContainerInset.bottom;
    [textView scrollRectToVisible:rect animated:animated];
}

2

我不太确定你的期望是什么。如果你只想要两行,就不应该有第三行。

使用这个:

self.text_description.textContainer.maximumNumberOfLines = 2
self.text_description.textContainer.lineBreakMode = .byTruncatingTail

限制UITextView的行数

看起来你实际上想要一个两行高度的文本视图? 如果是这样,请在您的视图控制器中尝试以下内容。

self.text_description = UITextView(frame: CGRect(x: 100.0, y: 100.0, width: 200.0, height: 28.0))
self.text_description.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
self.view.addSubview(self.text_description)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接