简体   繁体   中英

UITextView clear selection when touching outside

There is UITextView which is selectable but not editable. When I highlight text and touch outside the selection is not cleared. I tried to use UIMenuControllerWillHideMenu, it helps to clear selection when I touch other as the menu controller dismiss. But the problem is when I change the selection the selection is cleared because the menu controller dismissed and re appear after the selection modification. Has anyone workaround this problem too?

Add UITapGestureRecognizer and call endEditing:YES on your UITextView.

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTextViewTap:)];
 
- (void)onTextViewTap:(UITapGestureRecognizer *)tapGesture {
    if (tapGesture.state == UIGestureRecognizerStateEnded) {
            self.textView.selectedTextRange = nil;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM