从标签复制按钮(IOS) [英] Copy button from Label (IOS)
问题描述
从标签复制按钮.
我有计算器的代码,但我不知道怎么做,所以当我点击按钮时,带有标签的数字被复制.或者我怎样才能让它在我长按时看起来像复制:
I have code for the calculator but I can not figure out how to do it so that when I click on the button, the number with the Label is copied. Or how can I make it so that when I press long it appears to copy:
"
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var displayResultLabel: UILabel!
var stillTyping = false
var dotIsPlaced = false
var firstOperand: Double = 0
var secondOperand: Double = 0
var operationSign: String = ""
var currentInput: Double {
get {
return Double (displayResultLabel.text!)!
}
set {
let value = "\(newValue)"
let ValueArray = (value.components(separatedBy:"."))
if ValueArray[1] == "0" {
displayResultLabel.text = "\(ValueArray[0])"
} else {
displayResultLabel.text = "\(newValue)"
}
stillTyping = false
}
}
@IBAction func numberPressed(_ sender: UIButton) {
let number = sender.currentTitle!
if stillTyping {
if (displayResultLabel.text?.characters.count)! < 20 {
displayResultLabel.text = displayResultLabel.text! + number
}
} else {
displayResultLabel.text = number
stillTyping = true
}
}
@IBAction func twoOperandsSignPressed(sender: UIButton) {
operationSign = sender.currentTitle!
firstOperand = currentInput
stillTyping = false
dotIsPlaced = false
}
func operateWithTwoOperands(operation: (Double, Double) -> Double) {
currentInput = operation(firstOperand, secondOperand)
stillTyping = false
}
@IBAction func equalitySignPressed(sender: UIButton) {
if stillTyping {
secondOperand = currentInput
}
dotIsPlaced = false
switch operationSign {
case "+":
operateWithTwoOperands{$0 + $1}
case "-":
operateWithTwoOperands{$0 - $1}
case "×":
operateWithTwoOperands{$0 * $1}
case "÷":
operateWithTwoOperands{$0 / $1}
default: break
}
}
@IBAction func dotButtonPressed(_ sender: UIButton) {
if stillTyping && !dotIsPlaced {
displayResultLabel.text = displayResultLabel.text! + "."
dotIsPlaced = true
} else if !stillTyping && !dotIsPlaced {
displayResultLabel.text = "0."
}
}
}
推荐答案
我会回答你的两个问题.
I will answer both of your questions.
第一个问题:如何按下一个按钮,可以复制UILabel
上正在显示的文字?
The first question: How to press a button that can copy the text that is being displayed on the UILabel
?
答案:
@IBAction func yourButtonAction(_ sender: UIButton) {
UIPasteboard.general.string = yourLabel.text
}
第二个问题:如何长按显示复制"动作的UILabel
?
The second question: How to long-press on the UILabel
that shows the "Copy" action?
答案:
为了使 UILabel
可复制,我们需要创建一个自定义类,它是 UILabel
的子类.暂时称为CopyableLabel
:
To make UILabel
copyable we need to create a custom class that is a subclass of UILabel
. Temporarily called CopyableLabel
:
import UIKit
class CopyableLabel: UILabel {
override var canBecomeFirstResponder: Bool { return true }
override init(frame: CGRect) {
super.init(frame: frame)
sharedInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
sharedInit()
}
override func copy(_ sender: Any?) {
UIPasteboard.general.string = text
UIMenuController.shared.setMenuVisible(false, animated: true)
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return action == #selector(copy(_:))
}
}
// MARK: Actions methods
extension CopyableLabel {
func longPressGestureActionHandler(_ sender: UILongPressGestureRecognizer) {
becomeFirstResponder()
let menu = UIMenuController.shared
if !menu.isMenuVisible {
menu.setTargetRect(bounds, in: self)
menu.setMenuVisible(true, animated: true)
}
}
}
// MARK: Helper methods
extension CopyableLabel {
func sharedInit() {
isUserInteractionEnabled = true
addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(longPressGestureActionHandler(_:))))
}
}
并使用 CopyableLabel
而不是纯 UILabel
:
And use a CopyableLabel
instead of a pure UILabel
:
let yourLabel: CopyableLabel = CopyableLabel()
如果您使用 Interface Builder,请确保您已定义标签的基类:
If you use the Interface Builder, make sure you've defined the base class of your Label:
这篇关于从标签复制按钮(IOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!