Ios 首先放置视图,使文本的最后一部分可见 CGFloat offsetY=textSize.height-textViewHeight; (或)测试插入符号是否位于文本的中间位置。 如果((caret.origin.y+(textViewHeight/2)),
首先放置视图,使文本的最后一部分可见 CGFloat offsetY=textSize.height-textViewHeight; (或)测试插入符号是否位于文本的中间位置。Ios 首先放置视图,使文本的最后一部分可见 CGFloat offsetY=textSize.height-textViewHeight; (或)测试插入符号是否位于文本的中间位置。 如果((caret.origin.y+(textViewHeight/2)),,ios,objective-c,uitextview,uitextviewdelegate,Ios,Objective C,Uitextview,Uitextviewdelegate,首先放置视图,使文本的最后一部分可见 CGFloat offsetY=textSize.height-textViewHeight; (或)测试插入符号是否位于文本的中间位置。 如果((caret.origin.y+(textViewHeight/2)),很遗憾,这并不能解决我的问题。但是感谢您的回复!我错了!它确实解决了我的问题。我仍然有一些逻辑,在某些情况下阻止滚动。非常感谢! // in the text view's delegate - (void)textViewDidChangeS
如果((caret.origin.y+(textViewHeight/2)),很遗憾,这并不能解决我的问题。但是感谢您的回复!我错了!它确实解决了我的问题。我仍然有一些逻辑,在某些情况下阻止滚动。非常感谢!
// in the text view's delegate
- (void)textViewDidChangeSelection:(UITextView *)textView
{
[textView scrollRangeToVisible:textView.selectedRange];
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
CGRect caret = [textView caretRectForPosition:textView.selectedTextRange.start];
UIEdgeInsets textInsets = textView.textContainerInset;
CGFloat textViewHeight = textView.frame.size.height - textInsets.top - textInsets.bottom;
// only reposition the scroll view if the caret position is out of view
if (textViewHeight < caret.origin.y) {
CGSize textSize = [textView.layoutManager usedRectForTextContainer:textView.textContainer].size;
// initially place the view such that the last part of the text is visible
CGFloat offsetY = textSize.height - textViewHeight;
// test to see if the caret is in the middle of the text somewhere
if ((caret.origin.y + (textViewHeight / 2)) <= textSize.height) {
// we can, so center the caret in the middle of the view
offsetY = caret.origin.y - (textViewHeight / 2);
}
// the offset indicates the point in the scrollView that will correspond to the top left of the scrollView box
// therefore, we need to subtract the text view height to place the caret at the bottom of the scrollView, rather than the top and some empty whitespace
[self repositionScrollView:textView newOffset:CGPointMake(caret.origin.x, offsetY)];
}
}
/**
This method allows for changing of the content offset for a UIScrollView without triggering the scrollViewDidScroll: delegate method.
*/
- (void)repositionScrollView:(UIScrollView *)scrollView newOffset:(CGPoint)offset {
CGRect scrollBounds = scrollView.bounds;
scrollBounds.origin = offset;
scrollView.bounds = scrollBounds;
}