I want the text in a UITextField
(or ideally, a UILabel
) to be non-editable, but at the same time give the user the ability to copy it to paste elsewhere.
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Xcode: Is there a way to change line spacing (UI L
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
My final solution was the following:
I created a subclass of UILabel (UITextField should work the same) that displays a UIMenuController after being tapped. CopyableLabel.m looks like this:
Try
UITextView
instead (I suspect it would work like aUILabel
for you). I tested this with itseditable
property set toNO
, and double-tapping-to-copy worked for me.This will do everything you need. Will be copyable. But not editable, and won't show a keyboard or a cursor.
Another solution is keeping the
UITextField
enabled but programmatically preventing it from being edited. This is done with the following delegate method:I'm not aware of possible limitations though, currently suits my needs.
This question is pretty old and I'm surprised nobody has posted a solution without subclassing. The idea presented in @mrueg's answer is correct, but you shouldn't need to subclass anything. I just came across this problem and solved it like this:
In my view controller:
If you want to make this work for a
UILabel
, it should work the same way with just adding a tap gesture recognizer instead of using the delegate method.The following code saved me.
It seems
Paste
is a single and complete edit event.