0

I need to select text by swiping gesture without long press. There seems two way: One is to subclass UITextView or do something about UITextView, the other is to use Core Text to make a new UI component.

What should I do?

Han Pengbo
  • 1,338
  • 1
  • 18
  • 37

4 Answers4

1

This is another answer with pan gesture. Hope this is what you want.

- (IBAction)pan:(UIPanGestureRecognizer *)ges
{
    CGPoint point = [ges locationInView:ges.view];
    if (ges.state == UIGestureRecognizerStateBegan)
    {
        startPoint = point;
    }
    else if (ges.state == UIGestureRecognizerStateChanged || ges.state == UIGestureRecognizerStateEnded)
    {
        UITextPosition *start = [self.textView closestPositionToPoint:startPoint];
        UITextPosition *end = [self.textView closestPositionToPoint:point];
        UITextRange *range = [self.textView textRangeFromPosition:start toPosition:end];
        [self.textView select:self.textView];
        self.textView.selectedTextRange = range;
    }
}
PowHu
  • 2,159
  • 16
  • 17
0

UIResponder contains

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

Make a custom UITextView class and override these event could get what you want.

Hopes it could help you.

Steven Jiang
  • 1,006
  • 9
  • 21
0

Just like baliman said.

A little modified.

First separate textView.text into array.

_swipeValue = [textView componentsSeparatedByString:@" "];

Seconde in gesture selector

if(gestureReconize.direction == UISwipeGestureRecognizerDirectionLeft) {
    swipeIndex++;
    swipeIndex = (swipeIndex >= [self.swipeValues count]) ? 0 : swipeIndex;
} else {
    swipeIndex--;
    swipeIndex = (swipeIndex < 0) ? [self.swipeValues count] - 1 : swipeIndex;
}
NSString *selectedValue = [self.swipeValues objectAtIndex:swipeIndex];

Third convert selectedValue to range of textView.text

NSRange range = [textView.text rangeOfString:selectedValue];

Final set textView.selectedRange

[textView select:self]; //See http://stackoverflow.com/questions/1708608/uitextview-selectedrange-not-displaying-when-set-programatically
textView.selectedRange = range;

It work. But not perfect if your textView.text has same word.

PowHu
  • 2,159
  • 16
  • 17
  • Thank you ,but I need to select any part of text in UITextView by finger's position in UITextView. – Han Pengbo Feb 04 '15 at 07:06
  • I think what you need is pan gesture not swipe. – PowHu Feb 05 '15 at 04:28
  • use [ - (UITextRange *)textRangeFromPosition: toPosition: ] convert point to range.Then set selectedTextRange. – PowHu Feb 05 '15 at 04:31
  • You can see more here https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInput_Protocol/index.html#//apple_ref/occ/intfp/UITextInput – PowHu Feb 05 '15 at 04:32
-1

Hi Maybe something like this

//
//  ViewController.m
//  SwipeTextFieldDemo
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *swipeTextField;
@property (strong, nonatomic) NSMutableArray *swipeValues;
@end

int swipeIndex = 0;

@implementation ViewController
@synthesize swipeTextField = _swipeTextField;
@synthesize swipeValues = _swipeValues;

// Create list of colors
- (NSMutableArray *)swipeValues
{
    if(_swipeValues == nil) {
        _swipeValues = [[NSMutableArray alloc]initWithObjects:@"Red",
                                                              @"Blue",
                                                              @"Green",
                                                              @"Yellow",
                                                              @"Orange",
                                                              @"Pink",
                                                              @"White",
                                                              @"Black",
                                                              nil];
    }
    return _swipeValues;
}

- (void)swipeColor:(UISwipeGestureRecognizer *)gestureReconize
{   
    if(gestureReconize.direction == UISwipeGestureRecognizerDirectionLeft) {
        swipeIndex++;
        swipeIndex = (swipeIndex >= [self.swipeValues count]) ? 0 : swipeIndex;
    } else {
        swipeIndex--;
        swipeIndex = (swipeIndex < 0) ? [self.swipeValues count] - 1 : swipeIndex;
    }
    self.swipeTextField.text = [self.swipeValues objectAtIndex:swipeIndex];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Right swipe
    UISwipeGestureRecognizer *swr = [[UISwipeGestureRecognizer alloc] 
                                initWithTarget:self action:@selector(swipeColor:)];
    [swr setNumberOfTouchesRequired:1];
    [swr setDirection:UISwipeGestureRecognizerDirectionRight];
    [self.swipeTextField addGestureRecognizer:swr];

    // Left swipe
    UISwipeGestureRecognizer *swl = [[UISwipeGestureRecognizer alloc] 
                                initWithTarget:self action:@selector(swipeColor:)];
    [swl setNumberOfTouchesRequired:1];
    [swl setDirection:UISwipeGestureRecognizerDirectionLeft];
    [self.swipeTextField addGestureRecognizer:swl];

    self.swipeTextField.text = [self.swipeValues objectAtIndex:swipeIndex];
}

- (void)viewDidUnload
{
    [self setSwipeTextField:nil];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end



//
//  ViewController.h
//  SwipeTextFieldDemo
//
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITextFieldDelegate, UIGestureRecognizerDelegate>

- (void)swipeColor:(UISwipeGestureRecognizer *)gestureReconize;

@end
baliman
  • 588
  • 2
  • 8
  • 27
  • Thank you, but that isn't what I need. I need to select some text in UITextView by swiping gesture, not just detecting swiping gesture. – Han Pengbo Jan 30 '15 at 06:37
  • it selects colors (text) in a textfield when you swipe on that textfield – baliman Jan 30 '15 at 07:00