开发者

Setting UITextView selection on tap

I've got a UITextView that is not editable by default so that data detectors show addresses and phone numbers. I'd like to be able to let the user single tap anywhere in the text view to set it editable and put the insertion point there. Unfortunately, I can't get the insertion point and a single tap just en开发者_开发技巧ables editing of the text view (for an example of what i'm trying to accomplish, in Notes.app create a new note with a URL.)

I've subclassed UITextView and overrode - (void)touchesBegan:withEvent: so that it checks to see if the text view is editable. If it's not, I enables editing.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
  if (!self.editable)
  {
    self.editable = YES;
  }

  [super touchesBegan:touches withEvent:event];
}

I'm unsure however where to proceed from there as just calling becomeFirstResponder puts the insertion point at the top.


Sounds like you can set the property selectedRange to put the cursor where you want. You'll need to translate from points (in your touchesBegan method) to an NSRange value, however.

Another option might be to always have your TextView editable, and use the UITextViewDelegate's textViewShouldBeginEditing method.

I've never done any of this, however, so take these suggestions with a grain of salt.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜