gpt4 book ai didi

ios - 从标签复制按钮(IOS)

转载 作者:可可西里 更新时间:2023-11-01 00:35:27 24 4
gpt4 key购买 nike

从 Label 复制按钮。

我有计算器的代码,但我不知道如何做,所以当我点击按钮时,带有标签的数字被复制。或者我怎样才能让它在我长按时看起来像是在复制:

Photo1 "

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."
}
}
}

最佳答案

我会回答你的两个问题。

第一个问题:如何按下一个可以复制显示在UILabel上的文本的按钮?

回答:

@IBAction func yourButtonAction(_ sender: UIButton) {
UIPasteboard.general.string = yourLabel.text
}

第二个问题:如何长按显示“复制” Action 的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 的基类:

enter image description here

关于ios - 从标签复制按钮(IOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45662182/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com