iOS custom backspace button to clear UITextField- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *clear_Butoon=[[UIButton alloc]initWithFrame:CGRectMake(10,10,45,25)];
[clear_Butoon setTitle:@"Clear" forState:UIControlStateNormal];
[self.view addSubview:clear_Butoon];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleLongPress:)];
[messageviewer_btn addGestureRecognizer:longPress];
}
- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture
{
if(UIGestureRecognizerStateBegan == gesture.state)
{
// Called on start of gesture, do work here
cTime = [NSTimer scheduledTimerWithTimeInterval: 0.1 target: self selector: @selector(clearTextField) userInfo: nil repeats: YES];
}
if(UIGestureRecognizerStateChanged == gesture.state)
{
// Do repeated work here (repeats continuously) while finger is down
}
if(UIGestureRecognizerStateEnded == gesture.state)
{
// Do end work here when finger is lifted
[cTime invalidate];
}
}
-(void)clearTextField
{
UITextRange *deleteRange =txtText.selectedTextRange;
NSInteger offset = [txtText offsetFromPosition:txtText.beginningOfDocument toPosition:deleteRange.start];
NSLog(@"%d",offset);
if (txtText.text.length>0)
{
NSString *str=txtText.text;
if (offset==0)
{
}else
{
str = [str stringByReplacingCharactersInRange:NSMakeRange((offset-1), 1) withString:@""];
}
UITextRange *selectedRange = [txtText selectedTextRange];
// Calculate the existing position, relative to the end of the field (will be a - number)
int pos = [txtText offsetFromPosition:txtText.endOfDocument toPosition:selectedRange.start];
txtText.text=str;
// Work out the position based by offsetting the end of the field to the same
// offset we had before editing
UITextPosition *newPos = [txtText positionFromPosition:txtText.endOfDocument offset:pos];
// Reselect the range, to move the cursor to that position
txtText.selectedTextRange = [txtText textRangeFromPosition:newPos toPosition:newPos];
}
}
}
{
[super viewDidLoad];
UIButton *clear_Butoon=[[UIButton alloc]initWithFrame:CGRectMake(10,10,45,25)];
[clear_Butoon setTitle:@"Clear" forState:UIControlStateNormal];
[self.view addSubview:clear_Butoon];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleLongPress:)];
[messageviewer_btn addGestureRecognizer:longPress];
}
- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture
{
if(UIGestureRecognizerStateBegan == gesture.state)
{
// Called on start of gesture, do work here
cTime = [NSTimer scheduledTimerWithTimeInterval: 0.1 target: self selector: @selector(clearTextField) userInfo: nil repeats: YES];
}
if(UIGestureRecognizerStateChanged == gesture.state)
{
// Do repeated work here (repeats continuously) while finger is down
}
if(UIGestureRecognizerStateEnded == gesture.state)
{
// Do end work here when finger is lifted
[cTime invalidate];
}
}
-(void)clearTextField
{
UITextRange *deleteRange =txtText.selectedTextRange;
NSInteger offset = [txtText offsetFromPosition:txtText.beginningOfDocument toPosition:deleteRange.start];
NSLog(@"%d",offset);
if (txtText.text.length>0)
{
NSString *str=txtText.text;
if (offset==0)
{
}else
{
str = [str stringByReplacingCharactersInRange:NSMakeRange((offset-1), 1) withString:@""];
}
UITextRange *selectedRange = [txtText selectedTextRange];
// Calculate the existing position, relative to the end of the field (will be a - number)
int pos = [txtText offsetFromPosition:txtText.endOfDocument toPosition:selectedRange.start];
txtText.text=str;
// Work out the position based by offsetting the end of the field to the same
// offset we had before editing
UITextPosition *newPos = [txtText positionFromPosition:txtText.endOfDocument offset:pos];
// Reselect the range, to move the cursor to that position
txtText.selectedTextRange = [txtText textRangeFromPosition:newPos toPosition:newPos];
}
}
}