web-dev-qa-db-ja.com

UILabelをタップに応答させる方法は?

UILabelをUITextFieldよりもはるかに高速に作成できることを発見し、ほとんどの場合、データ表示アプリにUILabelを使用する予定です。

長い話を短くするために、ユーザーにUILabelをタップさせ、コールバックに応答させたいと思います。それは可能ですか?

ありがとう。

87
Happy

UITapGestureRecognizerインスタンスをUILabelに追加できます。

例えば:

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapped)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[myLabel addGestureRecognizer:tapGestureRecognizer];
myLabel.userInteractionEnabled = YES;
203
pythonquick

ストーリーボードを使用している場合、追加のコードなしでストーリーボードでこのプロセス全体を実行できます。ストーリーボードにラベルを追加してから、ラベルにタップジェスチャを追加します。 [ユーティリティ]ペインで、ラベルの[ユーザーインタラクションが有効]がオンになっていることを確認します。 (ストーリーボードのView Controllerの下部にある)タップジェスチャから、Ctrlキーを押しながらクリックしてViewController.hファイルにドラッグし、アクションを作成します。次に、ViewController.mファイルにアクションを実装します。

36
leenyburger

Swift 3.

tempLabelのジェスチャーを初期化します

tempLabel?.text = "Label"
let tapAction = UITapGestureRecognizer(target: self, action: #selector(self.actionTapped(_:)))
tempLabel?.isUserInteractionEnabled = true
tempLabel?.addGestureRecognizer(tapAction)

アクションレシーバー

func actionTapped(_ sender: UITapGestureRecognizer) {
    // code here
}

Swift 4.

tempLabelのジェスチャーを初期化します

tempLabel?.text = "Label"
let tapAction = UITapGestureRecognizer(target: self, action:@selector(actionTapped(_:)))
tempLabel?.isUserInteractionEnabled = true
tempLabel?.addGestureRecognizer(tapAction)

アクションレシーバー

func actionTapped(_ sender: UITapGestureRecognizer) {
    // code here
}
13
Koushik

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")
}
8
A.G

代わりにUIButtonを使用して、テキストを必要なものに設定できます。あなたがしたくない場合、ボタンはボタンのように見える必要はありません

4
Matt S.

ILabelにタップジェスチャを追加するには

UITapGestureRecognizer *tapAction = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lblClick:)];
tapAction.delegate =self;
tapAction.numberOfTapsRequired = 1;

//Enable the lable UserIntraction
lblAction.userInteractionEnabled = YES;
[lblAction addGestureRecognizer:tapAction];   

およびセレクターメソッドを評価するため

- (void)lblClick:(UITapGestureRecognizer *)tapGesture {

}

注:.hファイルにUIGestureRecognizerDelegateを追加

3
Hardik Thakkar

Swiftバージョン:var tapGesture : UITapGestureRecognizer = UITapGestureRecognizer()

次にviewDidLoad()の中に、これを追加します:

  let yourLbl=UILabel(frame: CGRectMake(x,y,width,height)) as UILabel!

    yourLbl.text = "SignUp"
    tapGesture.numberOfTapsRequired = 1
    yourLbl.addGestureRecognizer(tapGesture)
    yourLbl.userInteractionEnabled = true
    tapGesture.addTarget(self, action: "yourLblTapped:")
2
Aditya Jha

ボタンで複数行テキストを使用する場合は、MultilineテキストでUILabelを作成し、ボタンにサブビューとして追加します。

例えば:

yourLabel=[Uilabel alloc]init];
yourLabel.frame=yourButtom.Frame;//(frame size should be equal to your button's frame)
[yourButton addSubView:yourLabel]
1
Chandramani

アルビンジョージのスウィフト3

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: #selector(ViewController.tapResponse))
    tapGesture.numberOfTapsRequired = 1
    sampleLabel.isUserInteractionEnabled =  true
    sampleLabel.addGestureRecognizer(tapGesture)
}

func tapResponse(recognizer: UITapGestureRecognizer) {
    print("tap")
}
1
MRustamzade

Swiftバージョンは次のようになります。

func addGestureRecognizerLabel(){
    //Create a instance, in this case I used UITapGestureRecognizer,
    //in the docs you can see all kinds of gestures
    let gestureRecognizer = UITapGestureRecognizer()

    //Gesture configuration
    gestureRecognizer.numberOfTapsRequired = 1
    gestureRecognizer.numberOfTouchesRequired = 1
    /*Add the target (You can use UITapGestureRecognizer's init() for this)
    This method receives two arguments, a target(in this case is my ViewController) 
    and the callback, or function that you want to invoke when the user tap it view)*/
    gestureRecognizer.addTarget(self, action: "showDatePicker")

    //Add this gesture to your view, and "turn on" user interaction
    dateLabel.addGestureRecognizer(gestureRecognizer)
    dateLabel.userInteractionEnabled = true
}

//How you can see, this function is my "callback"
func showDatePicker(){
    //Your code here
    print("Hi, was clicked")
}

//To end just invoke to addGestureRecognizerLabel() when
//your viewDidLoad() method is called

override func viewDidLoad() {
    super.viewDidLoad()
    addGestureRecognizerLabel()
}
0
LeoGalante