2017-04-02 5 views
0

UITextView 내에서 커서 CGPoint를 얻는 방법은 여러 가지가 있습니다. 그러나 self.view (또는 전화 화면 테두리)와 관련하여 커서 위치를 찾아야합니다. Objective-C에서 그렇게 할 수있는 방법이 있습니까?self.view와 관련된 커서 위치

답변

1

UIView은 정확히 정확히 convert(_:to:)입니다. 이것은 리시버 좌표 공간에서 다른 뷰 좌표 공간으로 좌표를 변환합니다. 여기

은 예이다 :

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

스위프트

오브젝티브 C

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