In some circumstance, I have to make a UILabel clickable.
I tried many answers from stackOverflow to make the UIlabelClickable, but they were not working.
So eventually, I decided to position UIButton on the UILabel, and make the button transparent.
However, although the button correctly position on top of the Label, clicking the button does not interact. Any idea?
Thanks
Edit: In addition, there are two views, say view1 and view2. Due to structure of my app, I placed the UIButton and UILabel on view 2, and then placed view 2 on view1.
So:
view2.addSubview(button)
view1.addSubview(view2)
You should be able to set the UILabel's userInteractionEnabled to true, as UILabel inherits that property from its UIView super-class.
I forgot to mention, you would have to use a tap gesture recogniser with the label, as UILabel does not inherit from UIControl the way it did in the beginning.
Having said that, you could simply add a tap gesture recogniser to your UILabel.
A view with an opacity of zero is not tappable. Give the button a very small opacity, but not quite zero. It won't be visible, but it will now be tappable.
Alternatively, use a normal button with no title and a background color of whose alpha is close to zero but not quite zero. Again, this won't be visible, but it will be tappable.
If you need a UILabel to be clickable then you need to remove the label and use UIButton and change the name of the button to what ever your label says. This will appear like a label but will be clickable.
Hope this helps!
Actually a UIButton has a titleLabel on it. So it is fine to use a UIButton when you want a clickable UILabel.
First I will answer fir your question. There are 2 possibilities
1) The UILabel might be on top of UIButton, So you need to take UIButton on UILabel.
2) You might have made UIButton colour to clear colour which make Alpha to 0.0f. This make button un-clickable. So you need to change its colour settings. And make Alpha to 1.0
The Best way to use UIButton only and not to use UIButton and UILabel. OR only UILabel with userInteractionEnabled property.
Related
Is it possible to detect a touch in a UINavigationItem?
I would like to create a little animation on an UINavigationItem when the user taps on it.
In other words:
my titles contain way too much text, hence I had to truncate them
I want the user to be able to read the full title text when he/she presses the navigation item
To do so I need to:
Detect the touch
Animate the String / text (I could have a timer to animate this, however I am wondering if there is already a built in function in iOS - do you know of any?)
UINavigationItem inherits directly from NSObject as johnryu stated in the comments, but instead of showing a title in your navigation bar you can do something more complex by adding a UIView in place of the title.
This view can be composed of a UILabel that will show the actual title and a transparent button.
The transparent button can have an action that triggers a popup with a full text for a "touch down" event or "touch up inside".
Or you can add to that view a simple tap gesture recognizer.
Try assigning a custom titleView to UINavigationItem. Make it a UIView with a label inside which you will have your text. In default state, the label would have same size as its superview. Make sure that the UIView has clipsToBounds set to YES/true.
Attach a UITapGestureRecognizer to either the label or the view to switch to "animating" state and back.
When entering animating state, make the label wide enough to have enough space for the entire string and animate its movement within the container, you can use NSString's boundingRectWithSize:options:attributes:context: to calculate the proper width.
Then just add an animation where label moves to the left until its right end is visible, probably best if it's repeating.
Just like attached screen shot, I want to show some text with a remove button associated.
Any library/guide or sources welcomed.
Don't hesitate to update the question title because I am unable to get it from google.
Its simple
1) Create a UIView and set the backgroundColor to blue or whatever color you need.
2) create a UILabel with your text and add a subView to the view you made in step 1.
3) create a UIButton with text x and add it as the subView to the first view.
Done. I hope it helps.
Subclass UIView. Add UILabel for the text and UIButton for remove (x) button.
is there a way to add inset in an existing UITextField which was created in the storyboard? I added the constraints, an IBOutlet and I use :
fromTF.textRectForBounds(CGRectInset(fromTF.bounds, 50, 0))
But there is not the inset. Is it only possible to add inset if the textfield is created dynamically?
There might be a way to accomplish what you're trying to do by subclassing the textField. (this might help if you go this route: Text inset for UITextField?)
Alternatively, when I run into this problem, I do a few things in storyboard to give the appearance of text inset:
1.) Set the styling in the attributes inspector for the UITextField so that it doesn't have a border or anything - should just be a clear box that you can tap and enter text in.
2.) Then create a container UIView behind it to the full width that you want your text field area to be.
3.) Decrease the width of your UItextfield and align it to the right edge of your container view so that you have the appropriate amount of space on the left.
4.) Then just add some constraints for your container and your UItextfield.
textRectForBounds: is a function, not a property, you can't "set" it like you are trying to do. Check the documentation for UITextField.
You should not call this method directly. If you want to customize the
drawing rectangle for the text, you can override this method and
return a different rectangle.
If you want the appearance of an inset solely through interface builder, do as Adama has said and place a UIView behind the text field, make the textfield background clear, and set the UIView's background color such that it looks like it is the background of the textfield.
On the storyboard I have two UIButtons. In my UIViewController subclass I have one IBAction method for this buttons. One of this buttons has an image and hasn't got a title, another has the title but hasn't got the image. So, it's simply image that behaves as a button and ordinary button.
Both of this buttons call a method. The problem is that if I push one button, another doesn't highlight. Another problem that this buttons have padding and for me will be better if this padding will be touchable (I mean space between image button and title button to be clickable).
I know that I can subclass UIButton and make my buttons as I want, but maybe there is a way to make what I want without subclassing?
Thanks.
1)
Assign both buttons to properties.
When an IBAction fires, you can set one or both buttons to highlighted via those controls' "highlighted" properties, or via setHighlighted:.
2)
As for making space between the buttons touchable, set the alignment for the button graphic to left aligned or right aligned (the latter is what I've done in my example below) and these two separate buttons have edges that are touching.
3) Or...
You can cheat and have simply one button and put some space between your image and your button text, like this:
Yes, that's a whole bunch of spaces (which I've highlighted in green) before the word "Button".
Subclassing is a good thing. If you need a button with both an image and a title, then by all means create a subclass of UIButton that does what you want. You do that once, and then use it anywhere that you want. The alternative is to muck around with stacked duplicate buttons everywhere you want this look.
I found the most suitable variant for me and I think for all, who encountered the same problem. If you have one UIButton for image and another UIButton for text(title label), you can combine this two UIButtons to one Custom type UIButton. Then you can set image and title label text to this custom button. After that, use Insets to layout your image and title label.
For example, to set title label under image you must set Inset Left parameter as negative value and Inset Left parameter as positive value that is greater than image height. With Insets you can set proper image size too.
Using this way you don't need to handle highlight property changing by yourself.
How to make UILabel text editable on UILongPressGestureRecognizer.
So that on long press it converts to editable and after removing focus from uilabel it become readonly.
You can't do that. You'd have to exchange controls between UILabel and UITextField or make a subclass of UIControl to do that for you.
uilabels are not editable by the user, use textfield instead. You can adjust the properties of the textfield to make it look like a label.
Then simply set the interaction to disabled to simulate a label and add "something" on the area like an invisible view or button or something to detect the longpressgesture in this part. then when it does programatically set the focus to it. when the user pushes return the focus will be lost and it wont it will go back to being a "label"