ios - 相对于 self.view 的光标位置
<p><p>在 UITextView 中获取光标 CGPoint 有很多答案。但我需要找到与 self.view (或手机屏幕边框)相关的光标位置。在 Objective-C 中有没有办法做到这一点?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p><code>UIView</code> 有一个 <code>convert(_:to:)</code> 方法可以做到这一点。它将坐标从接收坐标空间转换到另一个 View 坐标空间。</p>
<p>这是一个例子:</p>
<p><strong>Objective-C</strong></p>
<pre><code>UITextView *textView = [ initWithFrame:CGRectZero];
UITextRange *selectedTextRange = textView.selectedTextRange;
if (selectedTextRange != nil)
{
// `caretRect` is in the `textView` coordinate space.
CGRect caretRect = ;
// Convert `caretRect` in the main window coordinate space.
// Passing `nil` for the view converts to window base coordinates.
// Passing any `UIView` object converts to that view coordinate space.
CGRect windowRect = ;
}
else {
// No selection and no caret in UITextView.
}
</code></pre>
<p><strong> swift </strong></p>
<pre><code>let textView = UITextView()
if let selectedRange = textView.selectedTextRange
{
// `caretRect` is in the `textView` coordinate space.
let caretRect = textView.caretRect(for: selectedRange.end)
// Convert `caretRect` in the main window coordinate space.
// Passing `nil` for the view converts to window base coordinates.
// Passing any `UIView` object converts to that view coordinate space.
let windowRect = textView.convert(caretRect, to: nil)
}
else {
// No selection and no caret in UITextView.
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 相对于 self.view 的光标位置,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/43166781/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/43166781/
</a>
</p>
页:
[1]