0
在UITextView中获取游标CGPoint有很多答案。但我需要找到与self.view(或手机屏幕边框)相关的光标位置。在Objective-C中有这样做的方法吗?与self.view相关的游标位置
在UITextView中获取游标CGPoint有很多答案。但我需要找到与self.view(或手机屏幕边框)相关的光标位置。在Objective-C中有这样做的方法吗?与self.view相关的游标位置
UIView
有一个convert(_:to:)
方法,确实如此。它将坐标从接收器坐标空间转换到另一个视图坐标空间。
下面是一个例子:
目标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.
}
夫特
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.
}