iOS Question TextField and Context Menu

Semen Matusovskiy

Well-Known Member
Licensed User
Hi, guys --

I want to prevent Copy/Paste/Select menu for TextField (not TextView).

Stackoverflow talks that it's possible to subclass control and to use callback function canPerformAction.
But how to do the same in B4I ?
 

Erel

Administrator
Staff member
Licensed User
Full example:
B4X:
Private Sub Application_Start (Nav As NavigationController)
   NavControl = Nav
   Page1.Initialize("Page1")
   Page1.Title = "Page 1"
   Page1.RootPanel.Color = Colors.White
   NavControl.ShowPage(Page1)
   Dim tf As TextField = CreateTextField
   Page1.RootPanel.AddView(tf, 10dip, 10dip, 200dip, 50dip)
End Sub

Sub CreateTextField As TextField
   Dim no As NativeObject
   no = no.Initialize("MyTextField").RunMethod("new", Null)
   Dim tf As TextField
   tf = no
   tf.BorderStyle = tf.STYLE_ROUNDEDRECT
   Return tf
End Sub


#if OBJC
@end
@interface MyTextField : UITextField
@end
@implementation MyTextField
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    return NO;
}
#End If
Note that the events will not work with this text field. It is possible to add them with a bit more work.
 

Semen Matusovskiy

Well-Known Member
Licensed User
Thanx, Erel. I replaced "return no;" to

B4X:
if (action == @selector(cut:))          return NO;
if (action == @selector(copy:))         return NO;
if (action == @selector(paste:))        return NO;
if (action == @selector(delete:))       return NO; 
if (action == @selector(select:))       return NO;   
if (action == @selector(selectAll:))    return NO;
return [super canPerformAction:action withSender:sender];
Alone, what I see during LongClick is a magnifier. Ok. But what about events ? Or maybe there is another solution like in Android ? (if I remember correctly it was enough to return true in long click event)
 

Semen Matusovskiy

Well-Known Member
Licensed User
Erel --

I tried
B4X:
Sub TextField1_BeginEdit As Boolean
    textField1.UserInteractionEnabled = False  
    Return True      
End Sub

Sub TextField1_EndEdit
    textField1.UserInteractionEnabled = True
End Sub
It "works", but obviously not like I want.

My real wish is to make LongClick equal to regular Click for all GUI elements.
Maybe to change something in system settings ?
 
Top