如何在标签中设置动作?
How to set action in label?
这是我的代码:
在 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)
但是吐司消息不能设置动作了?有什么想法
请post评论。谢谢
您正在将 tap
手势识别器添加到 viewDidLoad()
中的 toastLabel
,但随后您正在创建一个 new 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()
到:
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)
这就是您的 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...
}
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)
}
这是我的代码:
在 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)
但是吐司消息不能设置动作了?有什么想法 请post评论。谢谢
您正在将 tap
手势识别器添加到 viewDidLoad()
中的 toastLabel
,但随后您正在创建一个 new 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()
到:
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)
这就是您的 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...
}
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)
}