ios 将 UITextView 滚动到底部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16698638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scroll UITextView To Bottom
提问by Jonis
I am making a an app that has a UITextViewand a button.
我正在制作一个有一个UITextView和一个按钮的应用程序。
When I click the button some text will add in the UITextView.
当我单击按钮时,一些文本将添加到UITextView.
But when clicking the button, I wan't to scroll down to the bottom of the text field so the user can see the last text added.
但是当单击按钮时,我不想向下滚动到文本字段的底部,这样用户就可以看到最后添加的文本。
How to make the UITextViewto scroll down to the bottom?
如何使UITextView向下滚动到底部?
I tried:
我试过:
int numLines = LogTextView.contentSize.height / LogTextView.font.lineHeight+1;
NSLog(@"%d",numLines);
NSUInteger length = self.LogTextView.text.length;
self.LogTextView.selectedRange = NSMakeRange(0, length);
but it will not work...
但它不会工作......
I also tried:
我也试过:
self.LogTextView.contentSize=CGSizeMake(length,0);
回答by danypata
You can use the following code if you are talking about UITextView:
如果您正在谈论 UITextView,则可以使用以下代码:
-(void)scrollTextViewToBottom:(UITextView *)textView {
if(textView.text.length > 0 ) {
NSRange bottom = NSMakeRange(textView.text.length -1, 1);
[textView scrollRangeToVisible:bottom];
}
}
SWIFT 4:
快速 4:
func scrollTextViewToBottom(textView: UITextView) {
if textView.text.count > 0 {
let location = textView.text.count - 1
let bottom = NSMakeRange(location, 1)
textView.scrollRangeToVisible(bottom)
}
}
回答by Hong Duan
Try this if you have problem on iOS 7 or above. See this SO answer.
如果您在 iOS 7 或更高版本上遇到问题,请尝试此操作。请参阅此 SO 答案。
- (void)scrollTextViewToBottom:(UITextView *)textView {
NSRange range = NSMakeRange(textView.text.length, 0);
[textView scrollRangeToVisible:range];
// an iOS bug, see https://stackoverflow.com/a/20989956/971070
[textView setScrollEnabled:NO];
[textView setScrollEnabled:YES];
}
回答by zeeawan
With Swift 3
使用 Swift 3
let bottom = self.textView.contentSize.height - self.textView.bounds.size.height
self.textView.setContentOffset(CGPoint(x: 0, y: bottom), animated: true)
回答by wolffan
You have to implement a delegate method. The code below checks whether a newline has been entered and, if so, scrolls to the bottom of the textView:
你必须实现一个委托方法。下面的代码检查是否输入了换行符,如果是,则滚动到 textView 的底部:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text isEqualToString:@"\n"]) {
textView.contentOffset = CGPointMake(0.0, textView.contentSize.height);
}
return YES;
}
回答by Brandon Boynton
This works for me! :D
这对我有用!:D
CGPoint bottomOffset = CGPointMake(0, self.textView.contentSize.height - self.textView.bounds.size.height);
[self.description1 setContentOffset:bottomOffset animated:YES];
回答by Chris Amelinckx
Make a range, specifying encoding, to the last character, then scroll to that range Something other than utf8 might be appropriate depending on your content
创建一个范围,指定编码,到最后一个字符,然后滚动到该范围根据您的内容,utf8 以外的其他内容可能更合适
let range = NSMakeRange(self.OutputTextView.text.lengthOfBytes(using: String.Encoding.utf8), 0);
self.OutputTextView.scrollRangeToVisible(range);
回答by ArunGJ
As a generic approach for scrolling to bottom, it can be done on a UIScrollView.
作为滚动到底部的通用方法,它可以在 UIScrollView 上完成。
extension UIScrollView {
func scrollToBottom() {
let contentHeight = contentSize.height - frame.size.height
let contentoffsetY = max(contentHeight, 0)
setContentOffset(CGPoint(x: 0, y: contentoffsetY), animated: true)
}
}
This will work on all descendants of UIScrollView like UITextView, UITableView etc..
这将适用于 UIScrollView 的所有后代,如 UITextView、UITableView 等。
回答by neoneye
Swift 4
斯威夫特 4
extension UITextView {
func simple_scrollToBottom() {
let textCount: Int = text.count
guard textCount >= 1 else { return }
scrollRangeToVisible(NSMakeRange(textCount - 1, 1))
}
}
// Usage
textView.simple_scrollToBottom()
I wasn't able to find a solution without using NSRange.
我无法在不使用的情况下找到解决方案NSRange。
回答by Nathan Hosselton
textView.scrollRangeToVisible(NSRange(..<textView.text.endIndex, in: textView.text))
This solution does a couple of notable things slightly different:
这个解决方案做了几个值得注意的事情,略有不同:
- Utilizes the
String.Indexinterface (likely more performant than e.g..count) - Uses a
PartialRangeUpTowhich avoids an explicit range start position, reducing the code to a clean one-liner
- 利用
String.Index接口(可能比 eg 性能更高.count) - 使用 a
PartialRangeUpTo避免明确的范围开始位置,将代码简化为干净的单行代码
回答by yoAlex5
The Swift version of @Hong Duan answer
@Hong Duan的 Swift 版回答
func scrollTextViewToBottom(textView: UITextView) {
if textView.text.count > 0 {
let location = textView.text.count - 1
let bottom = NSMakeRange(location, 1)
textView.scrollRangeToVisible(bottom)
// an iOS bug, see https://stackoverflow.com/a/20989956/971070
textView.isScrollEnabled = false
textView.isScrollEnabled = true
}
}

