During the last year I have developed hAppy, a microblogging client for App.net. You can create posts for App.net within the client. One thing which bothered me a lot when using the first betas of it was the positioning of the cursor. The solution which is build into iOS, the tiny magnifying glass, is bad. It was cool in 2007. But today there are better solutions.

The solution I use in hAppy is the positioning of the cursor by panning in a field above the keyboard. The used code is quite simple.

I have created a subclass of UITextView and called it DDHTextView. In the initialization of the class (or in awakeFromNib in the case of the use of Interface Builder) the view for the pan gesture is initialized and assigned to the inputAccessoryView of the DDHTextView.

UIView *inputAccessoryView = [[UIView alloc] initWithFrame:
    CGRectMake(0.0f, 0.0f, [[UIScreen mainScreen] applicationFrame].size.width, 40.0f)];
inputAccessoryView.backgroundColor = [UIColor colorWithWhite:0.90f alpha:1.0f];
        
self.inputAccessoryView = inputAccessoryView;

Then the gesture recognizer are attached to the inputAccessoryView.

_singleFingerPanRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self 
                                                                     action:@selector(singleFingerPanHappend:)];
_singleFingerPanRecognizer.maximumNumberOfTouches = 1;
[inputAccessoryView addGestureRecognizer:_singleFingerPanRecognizer];

The last thing we need to do is to react on the pan gesture.

- (void)singleFingerPanHappend:(UIPanGestureRecognizer*)sender {
    if (sender.state == UIGestureRecognizerStateBegan) {
        self.startRange = self.selectedRange;
    }
    
    CGPoint translation = [sender translationInView:self];
    CGFloat cursorLocation = MAX(self.startRange.location+(NSInteger)(translation.x*DDHCursorVelocity), 0);
    NSRange selectedRange = {cursorLocation, 0};
    self.selectedRange = selectedRange;
}

In addition I have added a few other gesture recognizer for selecting text or moving the cursor fast to the beginning or end of the text. You can get DDHTextView on my github page or on cocoacontrols.com.

If you have any comments about this post? I am @dasdom.