无法在UITextView中选择文本

时间:2015-04-27 08:32:59

标签: ios uitextview

我有一个UITextView。然后我在UITextView中添加了双击手势识别器。因此,当我在UITextView中双击时,我无法在UITextView中选择文本。 我如何选择文字并同时获得双击事件? 提前谢谢

这是我的代码:

rowid

5 个答案:

答案 0 :(得分:0)

如果你添加手势 - >事件选择不可用

所以,你应该改变你的流程!

答案 1 :(得分:0)

试试这个:

- (void)viewDidLoad
{
   [super viewDidLoad];
   // Do any additional setup after loading the view, typically from a nib.

   UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapped:)];
   [tap setNumberOfTapsRequired:2];
   [_txtVw addGestureRecognizer:tap];


}

-(void)tapped:(UITapGestureRecognizer *)recognizer
 { 

      UITextView *textView_New = (UITextView *)recognizer.view;
      CGPoint pos = [recognizer locationInView:textView_New];

      NSLog(@"Tap Gesture Coordinates: %.2f %.2f", pos.x, pos.y);

      UITextPosition *tapPos = [textView_New closestPositionToPoint:pos];

      UITextRange * word = [textView_New.tokenizer rangeEnclosingPosition:tapPos withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight];
     NSLog(@"WORD: %@", [textView_New textInRange:word]);

 }

答案 2 :(得分:0)

在您目前的代码中尝试此操作:

.h文件

UITextField *txtField;

.m文件

- (void)viewDidLoad {
    [super viewDidLoad];

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
    tapGesture.numberOfTapsRequired = 2;
    [txtField addGestureRecognizer:tapGesture];
}

- (void)handleTapGesture:(UITapGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateRecognized) {
    // Perform your operation which you want to do
    }
}

答案 3 :(得分:0)

- (void)viewDidLoad {
    [super viewDidLoad];

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
    tapGesture.numberOfTapsRequired = 2;
    [txtField addGestureRecognizer:tapGesture];
}

- (void)handleTapGesture:(UITapGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateRecognized) {

  [self.titleField setSelectedTextRange:[self.titleField textRangeFromPosition:self.titleField.beginningOfDocument toPosition:self.titleField.endOfDocument]];

    }
}

试试这个

选择范围内的文字

int start = 2;
int end = 5;
UITextPosition *startPosition = [self positionFromPosition:self.beginningOfDocument offset:start];
UITextPosition *endPosition = [self positionFromPosition:self.beginningOfDocument offset:end];
UITextRange *selection = [self textRangeFromPosition:startPosition toPosition:endPosition];
self.selectedTextRange = selection;

答案 4 :(得分:0)

我不希望用户能够更改文本,因此取消选择"可编辑"在UITextView的界面构建器中。 将UITextView的委托设置为此类!!!

然后使用

-(void)textViewDidChangeSelection:(UITextView *)textView{
[textView setSelectedTextRange:[textView textRangeFromPosition:textView.beginningOfDocument toPosition:textView.endOfDocument]]; }