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?
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;
}
}
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.
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.