UITextView自动滚动到最后一行
-
05-07-2019 - |
解决方案
我的实现略有不同(我想禁用滚动),但我也不得不停止光标跳出我的UITextView。为此,我在UITextView子类中实现了一个null scrollRectToVisible。像这样:
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated
{
// do nothing. This fixes the cursor jumping above the field defect.
}
其他提示
优秀的解决方案是在子类UITextView中添加行
-(void) setContentOffset:(CGPoint)contentOffset {
[self setContentInset:UIEdgeInsetsZero];
[super setContentOffset:contentOffset];
}
这是工作!
使用此
NSRange myRange=NSMakeRange(outPutTextView.text.length, 0);
[outPutTextView scrollRangeToVisible:myRange];
如果我理解正确,你可以使用
[textView setScrollEnabled:NO];
禁用滚动。当光标到达下边距时不要输入...也许它不是很好的解决方案,但你可以添加一些阈值( [textView text]
中的最大字符)并返回
如果- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
[[textView text] length]> UITextView的委托方法maxCharacters 代码>
我发现似乎属性'contentOffset'也可以使用......
我得到了setframe的最后一行:
- 您可以将 setframe 设置为大约行高
- 在编辑时,在 - (BOOL)
textView:shouldChangeTextInRange:replacementText:
中,使用scrollRangeToVisible:
,参数为selectedRange
- 结束编辑时, setframe 为原始尺寸 醇>
不隶属于 StackOverflow