/* 选中文字后是否能够呼出菜单 */ - (BOOL)canBecameFirstResponder { return YES; } /* 选中文字后的菜单响应的选项 */ - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { if (action == @selector(copy:)) { // 菜单不能响应copy项 return NO; } else if (action == @selector(selectAll:)) { // 菜单不能响应select all项 return NO; } // 事实上一个return NO就可以将系统的所有菜单项全部关闭了 return NO; }
// 自定义text view选中文字后的菜单 UIMenuItem *selectItem = [[UIMenuItem alloc] initWithTitle:@"选择文字" action:@selector(callSelectText:)]; UIMenuItem *cancelItem = [[UIMenuItem alloc] initWithTitle:@"取消选中" action:@selector(cancelSelection:)]; [UIMenuController sharedMenuController].menuItems = @[selectItem, cancelItem];
#pragma mark - Menu Item Actions - (void)callSelectText:(id)sender { self.currentSelection_ = self.myTextView.selectedRange; self.selectOptionView.hidden = NO; [self.location_inputTextField becomeFirstResponder]; } - (void)cancelSelection:(id)sender { self.myTextView.selectedRange = NSRangeZero; }
@property(nonatomic) NSRange selectedRange;
NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"SelectOptionView" owner:self options:nil]; self.selectOptionView = nibViews[0]; self.selectOptionView.center = CGPointMake(self.view.center.x, self.view.bounds.size.height / 3); self.selectOptionView.hidden = YES; [self.view addSubview:self.selectOptionView]; // 要先加载了nib,IBOutlet才有意义,然后再设置其属性 self.location_inputTextField.delegate = self; self.length_inputTextField.delegate = self;
self.location_inputTextField.delegate =self;
self.length_inputTextField.delegate =self;
这两行代码置于loadNibNamed方法之前,那么两个文本输入框的delegate将为空(因为他们本身都是空,还没有加载)。#pragma mark - Select View Actions - (IBAction)selectText:(id)sender { NSInteger loc = self.location_inputTextField.text.integerValue; NSInteger len = self.length_inputTextField.text.integerValue; NSUInteger textLength = self.myTextView.text.length; if (loc < 0 || len < 0 || loc > textLength || len > textLength) { UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"错误" message:@"输入出错,输入的数不能小于0和大于文本长度" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alerView show]; return; } self.currentSelection_ = NSMakeRange(loc, len); [self finishSelectingText]; } - (IBAction)cancelSelectText:(id)sender { [self finishSelectingText]; } - (void)finishSelectingText { [self.location_inputTextField resignFirstResponder]; [self.length_inputTextField resignFirstResponder]; self.selectOptionView.hidden = YES; [self.myTextView becomeFirstResponder]; self.myTextView.selectedRange = self.currentSelection_; }
#pragma mark - UITextView Delegate - (void)textViewDidChangeSelection:(UITextView *)textView { NSLog(@"Selection changed"); NSLog(@"loc = %d", self.myTextView.selectedRange.location); NSLog(@"len = %d", self.myTextView.selectedRange.length); }
2014-02-16 23:33:56.197 MyTextView[4890:70b] Selection changed 2014-02-16 23:33:56.198 MyTextView[4890:70b] loc = 507 2014-02-16 23:33:56.198 MyTextView[4890:70b] len = 0 2014-02-16 23:33:56.334 MyTextView[4890:70b] Selection changed 2014-02-16 23:33:56.335 MyTextView[4890:70b] loc = 507 2014-02-16 23:33:56.335 MyTextView[4890:70b] len = 5 2014-02-16 23:34:05.291 MyTextView[4890:70b] Selection changed 2014-02-16 23:34:05.292 MyTextView[4890:70b] loc = 10 2014-02-16 23:34:05.292 MyTextView[4890:70b] len = 100
[self.location_inputTextField becomeFirstResponder];
#pragma mark - UITextField Delegate - (BOOL)textFieldShouldReturn:(UITextField *)textField { if ([self.location_inputTextField isFirstResponder]) { [self.location_inputTextField resignFirstResponder]; [self.length_inputTextField becomeFirstResponder]; } else if ([self.length_inputTextField isFirstResponder]) { [self.length_inputTextField resignFirstResponder]; } return YES; }