Pregunta

I want to disable text editing in UITextView, only cursor should stay. I already disabled keyboard, cut-copy-paste menu and zoom edit mode. But there still one problem - if I double tapping on TextView it's selects whole word. And one more thing, how can I let cursor select any place, not only end or start of word?

I did screenshots, which better describes my problem, but cant post it because of reputation. So I hope, that you will understand what exactly I mean.

Subclass of UITextView:

#import "UIUneditableTextView.h"

@implementation UIUneditableTextView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    UIMenuController *menuController = [UIMenuController sharedMenuController];
    if (menuController) {
        [UIMenuController sharedMenuController].menuVisible = NO;
    }
    return NO;
}

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

@end
¿Fue útil?

Solución 3

Here comes Swift working example

class TextView: UITextView {

    override func canPerformAction(action: Selector, withSender sender: AnyObject!) -> Bool {
        return false
    }

    override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer!) -> Bool {
        if gestureRecognizer.isKindOfClass(UITapGestureRecognizer) && ((gestureRecognizer as UITapGestureRecognizer).numberOfTapsRequired == 1) {
            let touchPoint = gestureRecognizer.locationOfTouch(0, inView: self)
            let cursorPosition = closestPositionToPoint(touchPoint)
            selectedTextRange = textRangeFromPosition(cursorPosition, toPosition: cursorPosition)
            return true
        }
        else {
            return false
        }
    }

}

Otros consejos

Since you're already overloading the text view, implement gestureRecognizerShouldBegin: I did an implementation like this.

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    // Check for gestures to prevent
    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
        // Check for double tap
        if (((UITapGestureRecognizer *)gestureRecognizer).numberOfTapsRequired == 2) {
            // Prevent the double tap
            return NO;
        }
    }

    // Always anything that makes it here
    return YES;
}

You might want to consider checking for the long gesture recognizer here, this is much less of a hack.

Look at the methods in UITextInput as this is where selection is controlled. You can act as the inputDelegate to find out when selectionWillChange: and override selectedTextRange to control the selection and caret.

Docs: http://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInput_Protocol/Reference/Reference.html#//apple_ref/occ/intfp/UITextInput

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top