1

For reason I had to use a Label instead of a button. Now I try to figure out how to pretend it's a regular button. Important point: Highlighting the text while the label is pressed (Action realized with a UITapGestureRecognizer). So I tried to find any gesture that realizes when a touch ends but I don't find anything :(

Any suggestions?

Regards, Patrick

patreu22
  • 2,988
  • 4
  • 22
  • 31
  • I think this can help you: http://stackoverflow.com/questions/6324724/is-there-a-touch-method-for-uilabel – egor.zhdan Sep 21 '15 at 17:16
  • 2
    "For reason I had to use a Label instead of a button" _What_ reason? A button basically just _is_ a tappable label, so why didn't you use a button? – matt Sep 21 '15 at 17:17
  • You can also always use touchesBegan: and touchesEnded: and then determine whether or not the point was within the bounds of the UILabel. – jaller200 Sep 21 '15 at 17:21
  • @matt I have very long strings and have to make them fit to the button size what didn't work with all methods (you can follow my dark history here: http://stackoverflow.com/questions/32694519/uibutton-auto-adjust-button-font-size-swift ). So I had to improvize and switch to labels :) – patreu22 Sep 22 '15 at 11:11

2 Answers2

0

UIButton actually has a booleans that you need to assigned for different activities, one of those booleans being called highlighted.

If you are using the View builder, it would be under the Attribute inspector on the right hand side, otherwise you would use btnHighlight.highlighted, where btnHighlight is the name of your outlet to the button

edit: if you have to absolutely use uilabel, you can do many things like assign gestures inside the label class or overriding the touchesEnded event

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        super.touchesEnded(touches, withEvent: event)
        for touch in touches
        {
          let location = touch.locationInNode(self);
          self.highlighted = true;  //I am not sure how you would be handling this,  this will run for every time a new touch happens.
          break;
        }
        if(touches.count ==  0)
        {
          self.highlighted = false;
        }

    }

Note, you may have to do casting on touch

Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44
-1

UIButton actually has a booleans that you need to assigned for different activities, one of those booleans being called highlighted.

eeeee
  • 1
  • From Review: Hi, this post does not seem to provide a [quality answer](https://stackoverflow.com/help/how-to-answer) to the question. Please either edit your answer and improve it, or just post it as a comment. – sɐunıɔןɐqɐp Oct 25 '18 at 06:55