iOS: selectable but not editable text
Gerriet M. Denkmann
NSTextField can set both isSelectable (true) and isEditable (false). So far so good.
But how to do this in iOS? I have a textField which is output only (editing makes no sense at all) but the user might want to select, copy and paste the content. How can this be done? When the delegate has: func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { false } then the field cannot be selected. Gerriet. |
|
Bob Stern
Is this a new project or one in which you already developed a lot of code? I get the impression it is new because your preceding post concerns an existing UIKit app that you want to port to Mac using Catalyst, whereas your second post, quoted above, implies you're starting from AppKit. If this is a new project, it might be easier to develop it in SwiftUI. Bob |
|
On Apr 9, 2022, at 11:59 PM, Gerriet M. Denkmann <gerriet@...> wrote:On iOS the idiomatic way to do this is not selecting the text, but a long-press gesture that brings up a context menu with a Copy item. —Jens |
|
Gerriet M. Denkmann
On 10 Apr 2022, at 14:11, Bob Stern via groups.io <bobs.lists@...> wrote:One project is just a toy to try Catalyst. Another is a real project, which could be done via Catalyst (some minor problems) A third is a real project, which could be not be done via Catalyst, because it crashed on UIReferenceLibraryViewController (which is marked “Availability Mac Catalyst 13.1+”) I tried SwiftUI (on macOS) some time ago. But gave up, because: Cannot select (and by implication: cannot copy) Texts. HStack { Text("Day: \(self.sets.globalTime)") Text("Susceptible: \(String(format: "%.1f", 100 * self.sets.sCnt))") … more Texts ... Spacer() } .padding(5) How can I make my Text selectable? Gerriet. |
|
Bob Stern
I don’t know a perfect solution for allowing a user to select and copy static text in SwiftUI. If you use a TextField instead of Text, the content will be selectable, but it also will be editable, which you do not want. A workaround would be to implement the onEditingChanged() and/or onCommit() closures to revert the content to the original text. Alternatives that seem less Mac-like might be to use a Button instead of a Text view, or a Text view with an onTapGesture() modifier as I believe Jens suggested. Bob Stern |
|
Bob Stern
Here's an untested idea worth a quick experiment: In a ZStack, superimpose a TextField view with .opacity(0) over a Text view. Bob Stern |
|
Gerriet M. Denkmann
On 12 Apr 2022, at 15:23, Jens Alfke <jens@...> wrote:So first I enabled isUserInteractionEnabled for my UILabel.On Apr 9, 2022, at 11:59 PM, Gerriet M. Denkmann <gerriet@...> wrote:On iOS the idiomatic way to do this is not selecting the text, but a long-press gesture that brings up a context menu with a Copy item. Then, In viewDidLoad() I added: let interaction = UIContextMenuInteraction(delegate: self) myLabel.addInteraction(interaction) Long-press on my UILabel works fine on iPhone and iPad. Somehow idiomatic. What I really expected: double click on my label (or long-press) and get a standard menu on top with: Copy, Look Up, Translate, etc. And on Mac (using Catalyst): a control-click also gets my context menu. Thanks for your suggestions! Gerriet. |
|
Gerriet M. Denkmann
On 12 Apr 2022, at 19:31, Bob Stern via groups.io <bobs.lists@...> wrote:Can select and copy, but only the content of the invisible TextField, not the (visible) Text below. Gerriet. |
|
Quincey Morris
toggle quoted message
Show quoted text
|
|
Gerriet M. Denkmann
On 12 Apr 2022, at 22:32, Quincey Morris <quinceymorris@...> wrote:That is exactly what I was missing when I started this project to years ago in macOS 10.15.4. textSelection Availability macOS 12.0+ One minor problem: HStack { Text(“One Red”) .foregroundColor(.red) Text(“Two Brown”) .foregroundColor(.brown) } .textSelection(.enabled) .padding(5) When I triple-click inside my text line, only one colour gets selected. Probably I need one attributed string with several colours and one Text. Thanks a lot for your help! Gerriet. |
|