Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Copyable UILabel
//
// SRCopyableLabel.swift
//
// Created by Stephen Radford on 08/09/2015.
// Copyright (c) 2015 Cocoon Development Ltd. All rights reserved.
//
import UIKit
class SRCopyableLabel: UILabel {
override public var canBecomeFirstResponder: Bool {
get {
return true
}
}
override init(frame: CGRect) {
super.init(frame: frame)
sharedInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
sharedInit()
}
func sharedInit() {
isUserInteractionEnabled = true
addGestureRecognizer(UILongPressGestureRecognizer(
target: self,
action: #selector(showMenu(sender:))
))
}
override func copy(_ sender: Any?) {
UIPasteboard.general.string = text
UIMenuController.shared.setMenuVisible(false, animated: true)
}
func showMenu(sender: Any?) {
becomeFirstResponder()
let menu = UIMenuController.shared
if !menu.isMenuVisible {
menu.setTargetRect(bounds, in: self)
menu.setMenuVisible(true, animated: true)
}
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return (action == #selector(copy(_:))
}
}
@steve228uk

This comment has been minimized.

Copy link
Owner Author

@steve228uk steve228uk commented Oct 19, 2016

Updated for Swift 2.3

@s2339956

This comment has been minimized.

Copy link

@s2339956 s2339956 commented Oct 21, 2016

swift 3
error : Method does not override any method from its superclass

?!

@ehamberg

This comment has been minimized.

Copy link

@ehamberg ehamberg commented Oct 22, 2016

Swift 3 version:

import UIKit

class CopyableLabel: UILabel {

    override public var canBecomeFirstResponder: Bool {
        get {
            return true
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        sharedInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        sharedInit()
    }

    func sharedInit() {
        isUserInteractionEnabled = true
        addGestureRecognizer(UILongPressGestureRecognizer(
            target: self,
            action: #selector(showMenu(sender:))
        ))
    }

    override func copy(_ sender: Any?) {
        UIPasteboard.general.string = text
        UIMenuController.shared.setMenuVisible(false, animated: true)
    }

    func showMenu(sender: Any?) {
        becomeFirstResponder()
        let menu = UIMenuController.shared
        if !menu.isMenuVisible {
            menu.setTargetRect(bounds, in: self)
            menu.setMenuVisible(true, animated: true)
        }
    }

    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(copy(_:)) {
            return true
        }

        return false
    }
}
@s2339956

This comment has been minimized.

Copy link

@s2339956 s2339956 commented Oct 24, 2016

Thanks for the reply, this is a great one

@petercampanelli

This comment has been minimized.

Copy link

@petercampanelli petercampanelli commented Mar 1, 2017

Just found @ehamberg's comment with the Swift 3 version and it would be great if you updated the original so it reflects on your blog post!

@igoMobile

This comment has been minimized.

Copy link

@igoMobile igoMobile commented Jun 7, 2017

Do you know of some limitations, when using CopyableLabel in the Today widget? I can see, that the functions canBecomeFirstResponder and canPerformAction are called, but menu is not shown... Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.