2017-08-14 6 views
1

ラベルから番号をコピーするコードがあります。ラベルからコピーするメニュー

私はコピーするためのボタンがあります。クリックすると、写真のメニューが開き、コピーボタンをクリックすることができます。あなたが最初にあなたがuserInteractionEnable = trueを設定する必要があり、UILabelをサブクラス化し、これを達成するためにいくつかのデフォルトのものを変更する必要がある

enter image description here

@IBAction func copybutton(_ sender: UIButton) { 
    UIPasteboard.general.string = displayResultLabel.text 
} 
+0

あなたはアクションシートにコピーアイコンを付けるだけですか? –

+0

@MohammadBashirSidaniコピー機能でこのメニューを開くには、このボタンを押す必要があります。 – KVL

答えて

1

に役立ちます。

私はUILabelUIButtonUIViewControllerを持っている例を作成しました。

ボタンを押すたびに、画面に3つのボタン(Copy,PasteView Memory)を含むアクションシートメニューが表示されます。

アクションシートのCopyボタンを押すと、ラベルのテキストがコピーされます。

enter image description here

これはコードです:

注:displayResultLabelという名前

UILabel

UIButtonのアクションは、showActionSheetButtonActionとなります。

class ViewController: UIViewController { 

    @IBOutlet weak var displayResultLabel: UILabel! 

    @IBAction func showActionSheetButtonAction(_ sender: UIButton) { 
     let actionSheetController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) 

     actionSheetController.addAction(
      UIAlertAction(title: "Copy", style: .default, handler: { [weak self] _ in 
       guard let strongSelf = self else { return } 

       UIPasteboard.general.string = strongSelf.displayResultLabel.text 
      }) 
     ) 

     actionSheetController.addAction(
      UIAlertAction(title: "Paste", style: .default, handler: { _ in 
       // Where to handle when the Paste button is pressed 
      }) 
     ) 

     actionSheetController.addAction(
      UIAlertAction(title: "View Memory", style: .default, handler: { _ in 
       // Where to handle when the View Memory button is pressed 
      }) 
     ) 

     actionSheetController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) 

     present(actionSheetController, animated: true, completion: nil) 
    } 
} 
+0

ありがとう、それは動作します。これは私が必要なものです)) – KVL

+0

ああ、あなたは大歓迎です。 –

0

UILabelにデフォルトでfalseであり、あなたは、どのプロパティcanBecomeFirstResponderをオーバーライドする必要もありまた、最初にラベルfirstResponderになり、longPressGestureRecognizerでUIMenuControllerを使用するメニューを表示すると、デフォルトでfalseになります。クラスフルコードはここにあります。

使用このUILabelサブクラス

import UIKit 

class ContextMenuLabel: UILabel { 

    /* 
    // Only override draw() if you perform custom drawing. 
    // An empty implementation adversely affects performance during animation. 
    override func draw(_ rect: CGRect) { 
     // Drawing code 
    } 
    */ 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     self.isUserInteractionEnabled = true 
     let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.showContextMenu)) 
     self.addGestureRecognizer(longPressGesture) 
    } 

    func showContextMenu() 
    { 
     self.becomeFirstResponder() 
     UIMenuController.shared.setTargetRect(self.frame, in: self.superview!) 
     let itemCopy = UIMenuItem(title: "Copy", action: #selector(self.copyAction)) 
     UIMenuController.shared.arrowDirection = .down 
     UIMenuController.shared.menuItems = [itemCopy] 
     UIMenuController.shared.setMenuVisible(true, animated: true) 

     debugPrint(UIMenuController.shared.menuFrame) 
    } 

    func copyAction() 
    { 
     UIPasteboard.general.string = self.text 
    } 

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

} 

結果

enter image description here

希望はこれがうまくいけば、私は問題を正しく理解し、あなたの質問からの情報のわずかな量で

関連する問題