HTCopyableLabel is a subclass of UILabel that makes it easy to allow users to copy a label's text.
pod 'HTCopyableLabel'to your
Manually add the following files into your project:
HTCopyableLabel instance exactly as as you would
UILabel, or subclass
HTCopyableLabel if you'd like. You can do this or in Interface Builder. Programmatically, this looks like:
HTCopyableLabel *copyableLabel = [[HTCopyableLabel alloc] init]; [self.view addSubview:copyableLabel];
Now, long pressing on the label will make a UIMenuController appear with a "Copy" option. Pressing "Copy" will copy the label's
text. Make sure the superview of the label has
userInteractionEnabled set to
HTCopyableLabelDelegate protocol allows you more fine-tuned control of the
UIMenuController's position, as well as the actual string that is to be copied.
UIMenuController appearance and position
UIMenuController positions itself according to a frame passed to it by
HTCopyableLabel. By default,
HTCopyableLabel will pass its own bounds. To specify the frame explicitly, implement the following in your
- (CGRect)copyMenuTargetRectInCopyableLabelCoordinates:(HTCopyableLabel *)copyableLabel
UIMenuController will try to intelligently position itself above, below, or along side the frame you pass it according to its position within the screen. If you wish to override this behavior, you should set
[HTCopyableLabel copyMenuArrowDirection] explicitly.
Specifying the text to be copied
If you wish to specify which string is copied to the pasteboard, implement the following method in your
- (NSString *)stringToCopyForCopyableLabel:(HTCopyableLabel *)copyableLabel
- Use this in your apps whenever you can, particularly email addresses -- your users will appreciate it!
- Contributions are very welcome.
- Attribution is appreciated (let's spread the word!), but not mandatory.
Use it? Love/hate it?
Also, check out HotelTonight's other iOS open source: