如何在标签中设置动作?

时间:2019-06-12 11:52:07

标签: ios swift toast

这是我的代码:

在viewDidLoad中添加了手势

let tap = UITapGestureRecognizer(target: self, action: #selector(tapLabel(tap:)))
            toastLabel.addGestureRecognizer(tap)
            toastLabel.isUserInteractionEnabled = true

func showLongToast( message: String) {        
    toastLabel = UILabel(frame: CGRect(x: controller.view.frame.origin.x + 20, y: controller.view.frame.size.height-200, width: controller.view.frame.size.width - 40, height: 125))
    toastLabel.numberOfLines = 0
    toastLabel.textAlignment = .center
    toastLabel.contentMode = .center
    toastLabel.backgroundColor = UIColor.white.withAlphaComponent(0.7)
    toastLabel.textColor = UIColor(red: 74/255, green: 74/255, blue: 74/255, alpha: 1)
    toastLabel.font = UIFont(name: "Montserrat-Medium", size: 15.0)
    let trimmedString = message.trimmingCharacters(in: .whitespacesAndNewlines)
    let string = NSMutableAttributedString(string: trimmedString)
    string.setColorForText("Enter Manually", with: #colorLiteral(red: 1, green: 0.4196078431, blue: 0.1812392979, alpha: 1))
    toastLabel.attributedText = string
    toastLabel.layer.cornerRadius = 25
    toastLabel.clipsToBounds = true
    controller.view.addSubview(toastLabel)
    controller.view.bringSubviewToFront(toastLabel)
  }

我已经从viewController调用了函数:

showLongToast(message: "Please Hold the lens or choose you can Enter Manually.", controller: self)

但是吐司消息无法再设置动作了吗?有什么想法 请发表评论。谢谢。

3 个答案:

答案 0 :(得分:1)

这就是您的viewDidLoad()tapLabel(tap:)的样子,

override func viewDidLoad() {
    super.viewDidLoad()
    self.showLongToast(message: "Please Hold the lens or choose you can Enter Manually.", controller: self)
    let tap = UITapGestureRecognizer(target: self, action: #selector(tapLabel(tap:)))
    toastLabel.addGestureRecognizer(tap)
    toastLabel.isUserInteractionEnabled = true
}

@objc func tapLabel(tap: UITapGestureRecognizer) {
    print("tapped..!!!")
}

showLongToast的签名将不会与您的代码一起编译。应该是

func showLongToast( message: String, controller: UIViewController) {
    //your code here...
}

enter image description here

答案 1 :(得分:0)

您要将tap手势识别器添加到toastLabel中的viewDidLoad(),但是随后您正在创建 { 1}}在UILabel

更改:

showLongToast()

toastLabel = UILabel(frame: CGRect(x: controller.view.frame.origin.x + 20, y: controller.view.frame.size.height-200, width: controller.view.frame.size.width - 40, height: 125)) 中:

showLongToast()

答案 2 :(得分:0)

    override func viewDidLoad() {
    let tap = UITapGestureRecognizer(target: self, action: #selector(tapLabel(tap:)))
    toastLabel.addGestureRecognizer(tap)
    toastLabel.isUserInteractionEnabled = true
    }    
    func showLongToast( message: String) {        
        toastLabel.frame = CGRect(x: controller.view.frame.origin.x + 20, y: controller.view.frame.size.height-200, width: controller.view.frame.size.width - 40, height: 125)
        toastLabel.numberOfLines = 0
        toastLabel.textAlignment = .center
        toastLabel.contentMode = .center
        toastLabel.backgroundColor = UIColor.white.withAlphaComponent(0.7)
        toastLabel.textColor = UIColor(red: 74/255, green: 74/255, blue: 74/255, alpha: 1)
        toastLabel.font = UIFont(name: "Montserrat-Medium", size: 15.0)
        let trimmedString = message.trimmingCharacters(in: .whitespacesAndNewlines)
        let string = NSMutableAttributedString(string: trimmedString)
        string.setColorForText("Enter Manually", with: #colorLiteral(red: 1, green: 0.4196078431, blue: 0.1812392979, alpha: 1))
        toastLabel.attributedText = string
        toastLabel.layer.cornerRadius = 25
        toastLabel.clipsToBounds = true
        controller.view.addSubview(toastLabel)
        controller.view.bringSubviewToFront(toastLabel)
}