[ios] 탭에 응답하도록 UILabel을 얻는 방법은 무엇입니까?

UITextField보다 훨씬 빠르게 UILabel을 만들 수 있음을 발견했으며 대부분의 경우 데이터 표시 앱에 UILabel을 사용할 계획입니다.

긴 이야기를 짧게 만들기 위해 사용자가 UILabel을 탭하고 내 콜백이 이에 응답하도록하고 싶습니다. 가능합니까?

감사.



답변

UITapGestureRecognizerUILabel에 인스턴스를 추가 할 수 있습니다 .

예를 들면 :

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


답변

스토리 보드를 사용하는 경우 추가 코드없이 스토리 보드에서 전체 프로세스를 수행 할 수 있습니다. 스토리 보드에 레이블을 추가 한 다음 레이블에 탭 제스처를 추가하십시오. 유틸리티 창에서 레이블에 대해 “사용자 상호 작용 활성화”가 선택되어 있는지 확인합니다. 탭 제스처 (스토리 보드의보기 컨트롤러 하단에 있음)에서 ctrl + 클릭하고 ViewController.h 파일로 드래그하고 액션을 만듭니다. 그런 다음 ViewController.m 파일에서 작업을 구현합니다.


답변

스위프트 3.0

에 대한 제스처 초기화 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
}

스위프트 4.0

에 대한 제스처 초기화 tempLabel

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

액션 수신기

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


답변

스위프트 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")
}


답변

대신 UIButton을 사용하고 원하는대로 텍스트를 설정할 수 있습니다. 원하지 않는 경우 버튼이 버튼처럼 보이지 않아도됩니다.


답변

UILable에 탭 제스처를 추가하려면

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 추가


답변

스위프트 버전 :
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:")