MobileLabel: setSelectable?

Following up my previous post I wonder if it is possible to make text selectable and copy it to clipboard, for a MobileLabel on iOS?

ChatGPT is suggesting declares …

In Xojo iOS development, MobileLabel does not support selectable text by default. It is a simple label control that displays non-interactive text.

Workaround for Selectable Text in iOS Custom Cells

To make text selectable inside a MobileTableCustomCell, you need to use a UITextView or UILabel configured from a custom iOSCustomControl. Xojo allows wrapping native UIKit controls using iOSCustomControl.

Steps to Make Selectable Text in a Custom Cell:

  1. Create an iOSCustomControl subclass.
  2. In the Open event of the control, instantiate a native UITextView or UILabel using a Declare.
  3. Enable user interaction and selection.
  4. Add it as a subview to the control’s native handle.
  5. Use this custom control in your MobileTableCustomCell instead of MobileLabel.

Sample Code Using

UITextView

in

iOSCustomControl

:

#If TargetiOS Then
Declare Function Create UITextView Lib "UIKit" Selector "initWithFrame:" (cls As Ptr, frame As CGRect) As Ptr
Declare Sub AddSubview Lib "UIKit" Selector "addSubview:" (parent As Ptr, child As Ptr)
Declare Sub SetSelectable Lib "UIKit" Selector "setSelectable:" (tv As Ptr, flag As Boolean)
Declare Sub SetEditable Lib "UIKit" Selector "setEditable:" (tv As Ptr, flag As Boolean)
Declare Sub SetText Lib "UIKit" Selector "setText:" (tv As Ptr, text As CFStringRef)
Declare Function UIColor_whiteColor Lib "UIKit" Selector "whiteColor" (cls As Ptr) As Ptr
Declare Sub SetBackgroundColor Lib "UIKit" Selector "setBackgroundColor:" (tv As Ptr, color As Ptr)

Dim frame As New CGRect(0, 0, Me.Width, Me.Height)
Dim tvPtr As Ptr = CreateUITextView(NSClassFromString("UITextView"), frame)

SetEditable(tvPtr, False)
SetSelectable(tvPtr, True)
SetText(tvPtr, "Your selectable text here")

SetBackgroundColor(tvPtr, UIColor_whiteColor(NSClassFromString("UIColor")))
AddSubview(Self.Handle, tvPtr)
#EndIf

I don’t think you need to create a custom control here. Everything you need already exists on UITextView which is what the MobileTextField control is.

Just these two should work:

Declare Sub SetSelectable Lib "UIKit" Selector "setSelectable:" (tv As Ptr, flag As Boolean)
Declare Sub SetEditable Lib "UIKit" Selector "setEditable:" (tv As Ptr, flag As Boolean)

With code like this in the field’s opening event:

SetSelectable(me.handle, true)
SetEditable(me.handle, false)
1 Like