How to disable magnification in UITextView

随声附和 提交于 2019-12-10 21:58:12

问题


I want to get rid of magnification and text selection in UITextView but I need phone number, link and address detectors. I am using

-(void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
    if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {
        gestureRecognizer.enabled = NO;
    }
    [super addGestureRecognizer:gestureRecognizer];
    return;}

to stop magnification, but it also stops selection phone number / address / link detected by textview. If I do [_txtView setSelectable:NO]; it stops both magnification and text selection as well as data detection.


回答1:


Put image on your UITextview in .xib file then put below code.

 - (void)viewDidLoad
  {
      [super viewDidLoad];
      self.navigationController.navigationBarHidden = YES;

      UITapGestureRecognizer *tappress= [[UITapGestureRecognizer alloc] initWithTarget:self    action:@selector(longPressed:)];
      img.userInteractionEnabled = YES;
      [img addGestureRecognizer:tappress];
}

-(void)longPressed:(UILongPressGestureRecognizer *)sender
{
    [yourtextview becomeFirstResponder];
}

in my code img is a UIImageview




回答2:


After quite a long time trying, I managed to stop text selection, magnifying, and keeping data detection (links clickable etc) by overriding addGestureRecognizer on a UITextView subclass allowing only UILongPressGestureRecognizer delaying touch ending:

UIUnselectableTextView.m

-(void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
    if([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] && gestureRecognizer.delaysTouchesEnded)
    {
        [super addGestureRecognizer:gestureRecognizer];
    }
}



回答3:


Try this:

  1. Set the delegate of the textview to your viewcontroller
  2. add this method

    - (void)textViewDidChangeSelection:(UITextView *)textView
    {
         NSRange selected;
         selected.location = 0;
         selected.length = 0;
         textView.selectedRange = selected;
    }
    

This would disable the magnification but still have links clickable




回答4:


You just need to make sure you have set right parameters for the UItextfield (and there is no need to actually have it done by overriding the gestures). I guess if you change your attributes for "Behaviour" and "Detection" in interface builder as following you will have your desired behaviour.



来源:https://stackoverflow.com/questions/24823154/how-to-disable-magnification-in-uitextview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!