Git Product home page Git Product logo

htcopyablelabel's Introduction

HTCopyableLabel

HotelTonight

Overview

HTCopyableLabel is a subclass of UILabel that makes it easy to allow users to copy a label's text.

HotelTonight

Read the accompanying blog post on the HotelTonight Engineering Blog. For an excellent explanation how copying on a UILabel is implemented, be sure to read UIMenuController on NSHipster.

Usage

Installation

With Cocoapods:

  • Add pod 'HTCopyableLabel' to your Podfile

Without Cocoapods:

Manually add the following files into your project:

  • HTCopyableLabel.m
  • HTCopyableLabel.h

Quickstart Guide

Create an 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 YES.

Advanced Usage

Implementing the 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 HTCopyableLabelDelegate:

- (CGRect)copyMenuTargetRectInCopyableLabelCoordinates:(HTCopyableLabel *)copyableLabel

Furthermore, 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 HTCopyableLabelDelegate:

- (NSString *)stringToCopyForCopyableLabel:(HTCopyableLabel *)copyableLabel

Etc.

  • 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?

Tweet the author @jonsibs, and check out HotelTonight's engineering blog: http://engineering.hoteltonight.com

Also, check out HotelTonight's other iOS open source:

htcopyablelabel's People

Contributors

russ-ht avatar sibljon avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.