gpt4 book ai didi

ios - 相对于 self.view 的光标位置

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:25:03 24 4
gpt4 key购买 nike

在 UITextView 中获取光标 CGPoint 有很多答案。但我需要找到与 self.view(或手机屏幕边框)相关的光标位置。有没有办法在 Objective-C 中这样做?

最佳答案

UIView 有一个 convert(_:to:) 方法可以做到这一点。它将坐标从接收器坐标空间转换到另一个 View 坐标空间。

这是一个例子:

objective-C

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectZero];
UITextRange *selectedTextRange = textView.selectedTextRange;
if (selectedTextRange != nil)
{
// `caretRect` is in the `textView` coordinate space.
CGRect caretRect = [textView caretRectForPosition:selectedTextRange.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.
CGRect windowRect = [textView convertRect:caretRect toView:nil];
}
else {
// No selection and no caret in UITextView.
}

swift

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.
}

关于ios - 相对于 self.view 的光标位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43166781/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com