0
UITextView内でカーソルCGPointを取得するには、多くの答えがあります。しかし、私はself.view(または電話の画面の枠線)に関連してカーソルの位置を見つける必要があります。 Objective-Cでそうする方法はありますか?self.viewに対するカーソルの位置
UITextView内でカーソルCGPointを取得するには、多くの答えがあります。しかし、私はself.view(または電話の画面の枠線)に関連してカーソルの位置を見つける必要があります。 Objective-Cでそうする方法はありますか?self.viewに対するカーソルの位置
UIView
には、正確にはconvert(_:to:)
の方法があります。それは、レシーバ座標空間から別のビュー座標空間へ座標を変換する。ここ
例である:
の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.
}
スウィフト
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.
}