从Label。复制按钮。
我有计算器的代码,但我无法弄清楚如何做到这一点,当我点击按钮时,复制带有标签的数字。 或者我怎样才能做到这一点,当我长按它似乎复制:
“
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."
}
}
}
答案 0 :(得分:5)
我会回答你的两个问题。
第一个问题:如何按下可以复制UILabel
上显示的文字的按钮?
<强>答案:强>
@IBAction func yourButtonAction(_ sender: UIButton) {
UIPasteboard.general.string = yourLabel.text
}
第二个问题:如何长按显示“复制”操作的UILabel
?
<强>答案:强>
要使UILabel
可复制,我们需要创建一个自定义类,它是UILabel
的子类。暂时叫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
:
let yourLabel: CopyableLabel = CopyableLabel()
如果使用Interface Builder,请确保已定义Label的基类:
答案 1 :(得分:0)
您可以使用UIPasteboard
实施复制和粘贴。您可以在其属性string
上读取和写入值,例如:
displayResultLabel.text = UIPasteboard.general.string
将粘贴板中的当前值粘贴到您的标签中,然后
UIPasteboard.general.string = displayResultLabel.text
将值从标签复制到粘贴板。