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];
}
You can hide the UIMenuController on changing selection of your UITextView:
@interface ViewController () <UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *textView;
@end
@implementation ViewController
@synthesize textView;
- (void)viewDidLoad {
[super viewDidLoad];
textView.delegate = self;
}
- (void)selectLine:(id)sender {
...
}
#pragma mark - UITextViewDelegate
- (void)textViewDidChangeSelection:(UITextView *)textView {
if (UIMenuController.sharedMenuController.isMenuVisible) {
[UIMenuController.sharedMenuController setMenuVisible:NO animated:YES];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With