我发现我可以比UITextField更快地创建UILabel,并且我计划在大多数时间使用UILabel作为我的数据显示应用程序.
总而言之,我希望让用户点击UILabel并让我的回调响应.那可能吗?
谢谢.
pyt*_*ick 205
您可以UITapGestureRecognizer
向UILabel 添加实例.
例如:
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapped)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[myLabel addGestureRecognizer:tapGestureRecognizer];
myLabel.userInteractionEnabled = YES;
Run Code Online (Sandbox Code Playgroud)
lee*_*ger 36
如果您正在使用故事板,则可以在故事板中执行此整个过程而无需其他代码.在故事板上添加标签,然后在标签上添加一个点按手势.在"实用工具"窗格中,确保选中"已启用用户交互"标签.从点击手势(在故事板中视图控制器的底部),按住Ctrl键并单击并拖动到ViewController.h文件并创建一个Action.然后在ViewController.m文件中实现该操作.
Kou*_*hik 13
Swift 3.0
初始化手势 tempLabel
tempLabel?.text = "Label"
let tapAction = UITapGestureRecognizer(target: self, action: #selector(self.actionTapped(_:)))
tempLabel?.isUserInteractionEnabled = true
tempLabel?.addGestureRecognizer(tapAction)
Run Code Online (Sandbox Code Playgroud)
动作接收器
func actionTapped(_ sender: UITapGestureRecognizer) {
// code here
}
Run Code Online (Sandbox Code Playgroud)
Swift 4.0
初始化手势 tempLabel
tempLabel?.text = "Label"
let tapAction = UITapGestureRecognizer(target: self, action:@selector(actionTapped(_:)))
tempLabel?.isUserInteractionEnabled = true
tempLabel?.addGestureRecognizer(tapAction)
Run Code Online (Sandbox Code Playgroud)
动作接收器
func actionTapped(_ sender: UITapGestureRecognizer) {
// code here
}
Run Code Online (Sandbox Code Playgroud)
Swift 2.0:
我正在添加一个nsmutable字符串作为sampleLabel的文本,启用用户交互,添加点击手势并触发方法.
override func viewDidLoad() {
super.viewDidLoad()
let newsString: NSMutableAttributedString = NSMutableAttributedString(string: "Tap here to read the latest Football News.")
newsString.addAttributes([NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleDouble.rawValue], range: NSMakeRange(4, 4))
sampleLabel.attributedText = newsString.copy() as? NSAttributedString
let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapResponse:")
tapGesture.numberOfTapsRequired = 1
sampleLabel.userInteractionEnabled = true
sampleLabel.addGestureRecognizer(tapGesture)
}
func tapResponse(recognizer: UITapGestureRecognizer) {
print("tap")
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
68035 次 |
最近记录: |