Tapping the cursor in a UITextView brings up a UIMenuController. Tapping Select causes the relevant text to be selected and another UIMenuController with new options to be displayed. Tapping anywhere else in the text view causes the "second" UIMenuController to hide.
I have a custom UIMenuItem that, when tapped, selects the current line of text in a UITextView and then displays the UIMenuController again for subsequent actions, however the "second" UIMenuController does not hide when tapping anywhere else in the view as expected.
Custom menu item action:
- (void)selectLine:(id)sender {
NSString *string = [[self textView] text];
NSRange range = [[self textView] selectedRange];
NSRange newRange = [string lineRangeForRange:range];
if ([[string substringWithRange:newRange] hasSuffix:@"\n"]) {
newRange.length -= 1;
}
[[self textView] setSelectedRange:newRange];
CGRect targetRect = [[self textView] firstRectForRange:[[self textView] selectedTextRange]];
UIMenuController *menuController = [UIMenuController sharedMenuController];
[menuController setTargetRect:targetRect inView:[self textView]];
[menuController setMenuVisible:YES animated:YES];
}
canPerformAction:withSender:
method. I check theselectedRange
property ofUITextView
to determine if I should show my custom menu item. – CyberMoai