iOS: ScrollView 到 TextView 中的当前光标位置
<p><p>我有 UIScrollView,它有一些 View ,包括 UITextView。 UITextView 比屏幕大,有一些文本,并且禁用了滚动。如果用户点击 TextView ,我想将我的主 ScrollView 滚动到 TextView 中光标的位置。我该怎么做?我尝试使用 selectedRange 但它似乎不起作用。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这是我的一个更复杂的解决方案:</p>
<pre class="lang-c prettyprint-override"><code>- (void)scrollView:(UIScrollView *)scrollView scrollToTextInput:(UIResponder *)responder
{
if (!responder || ! || !]) {
return;
}
UIView<UITextInput> *textInput = (UIView<UITextInput> *)responder;
UIView *nextView = textInput;
while (nextView && (nextView != scrollView))
{
nextView = ;
}
if (!nextView)
{
// Oh, this view is not from our `scrollView`.
return;
}
CGRect cursorRect = ;
CGPoint cursorPoint = CGPointMake(CGRectGetMidX(cursorRect), CGRectGetMidY(cursorRect));
cursorPoint = ;
CGFloat contentHeight = scrollView.contentSize.height;
CGFloat containerHeight = scrollView.frame.size.height;
CGFloat topInset = scrollView.contentInset.top;
CGFloat bottomInset = scrollView.contentInset.bottom;
CGFloat verticalInsets = scrollView.contentInset.top + scrollView.contentInset.bottom;
CGFloat offsetY = cursorPoint.y - (containerHeight - verticalInsets) / 2.f;
offsetY = MIN(MAX(offsetY, topInset), contentHeight - containerHeight + verticalInsets);
;
}
</code></pre></p>
<p style="font-size: 20px;">关于iOS: ScrollView 到 TextView 中的当前光标位置,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/23111065/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/23111065/
</a>
</p>
页:
[1]