2016-10-11 43 views

回答

2

是的,您需要通過應用於您的UILabel的長按手勢來實現UIMenuController。有關於NSHipster的優秀文章,但文章的要點如下。

創建的UILabel的一個子類,並實現以下方法:

override func canBecomeFirstResponder() -> Bool { 
    return true 
} 

override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool { 
    return (action == "copy:") 
} 

// MARK: - UIResponderStandardEditActions 

override func copy(sender: AnyObject?) { 
    UIPasteboard.generalPasteboard().string = text 
} 

然後在您的視圖控制器,你可以添加一個長按手勢您的標籤:

let gestureRecognizer = UILongPressGestureRecognizer(target: self, action: "handleLongPressGesture:") 
label.addGestureRecognizer(gestureRecognizer) 

和處理長用此方法按下:

func handleLongPressGesture(recognizer: UIGestureRecognizer) { 
    if let recognizerView = recognizer.view, 
     recognizerSuperView = recognizerView.superview 
    { 
     let menuController = UIMenuController.sharedMenuController() 
     menuController.setTargetRect(recognizerView.frame, inView: recognizerSuperView) 
     menuController.setMenuVisible(true, animated:true) 
     recognizerView.becomeFirstResponder() 
    }} 

注意:此代碼直接從t他NSHipster的文章,我只是在這裏包括它符合SO。

+1

您還需要在標籤上啓用用戶交互。沒有這個手勢將永遠得不到承認。在handleLongPressGesture方法中檢查手勢的狀態也很重要。 NSHipster的文章做得不好,做得不好。 – rmaddy

+0

發現這個以及:) http://stephenradford.me/make-uilabel-copyable/ – user2636197

0

UILabel從UIView繼承,因此您只需將長按手勢識別器添加到標籤即可。請注意,您必須將isUserInteractionEnabled更改爲true,因爲它對於標籤默認爲false。

import UIKit 

    class ViewController: UIViewController { 
     let label = UILabel() 

     override func viewDidLoad() { 
      view.addSubview(label) 
      label.text = "hello" 
      label.translatesAutoresizingMaskIntoConstraints = false 
      label.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 
      label.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true 
      let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressLabel(longPressGestureRecognizer:))) 
      label.addGestureRecognizer(longPressGestureRecognizer) 
      label.isUserInteractionEnabled = true 
     } 

     @objc private func longPressLabel (longPressGestureRecognizer: UILongPressGestureRecognizer) { 
      if longPressGestureRecognizer.state == .began { 
       print("long press began") 
      } else if longPressGestureRecognizer.state == .ended { 
       print("long press ended") 
      } 

     } 

    } 
+0

發現這也是http://stephenradford.me/make-uilabel-copyable/ :) – user2636197

-1

我已經實現了一個UILabel子類,它提供了所需的所有功能。請注意,如果您將此與界面構建器配合使用,則需要調整init方法。

/// A label that can be copied. 
class CopyableLabel: UILabel 
{ 
    // MARK: - Initialisation 

    /// Creates a new label. 
    init() 
    { 
     super.init(frame: .zero) 

     let gestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPressGesture(_:))) 
     self.addGestureRecognizer(gestureRecognizer) 

     self.isUserInteractionEnabled = true 
    } 

    required init?(coder aDecoder: NSCoder) 
    { 
     fatalError("init(coder:) has not been implemented") 
    } 

    // MARK: - Responder chain 

    override var canBecomeFirstResponder: Bool 
    { 
     return true 
    } 

    // MARK: - Actions 

    /// Method called when a long press is triggered. 
    func handleLongPressGesture(_ gestuerRecognizer: UILongPressGestureRecognizer) 
    { 
     guard let superview = self.superview else { return } 

     let menuController = UIMenuController.shared 
     menuController.setTargetRect(self.frame, in: superview) 
     menuController.setMenuVisible(true, animated:true) 
     self.becomeFirstResponder() 
    } 

    override func copy(_ sender: Any?) 
    { 
     UIPasteboard.general.string = self.text 
    } 
}