问题
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:
- Set the delegate of the textview to your viewcontroller
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