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"
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.
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.
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.
I have a UITextField that will represent an integer number with a fraction (numerator and denominator). As an example: "27 3/16". I want to make the denominator "/16" at the end both un-editable and also un-selectable.
I can use the delegate method textField:shouldChangeCharactersInRange:replacementString: to prevent the "/16?" from being edited with an approach a bit like this.
Is there some way that I can also prevent the "/16" from being selectable at all? So, the caret can't be moved in to it, and the selection marque can't be made around it.
If this isn't possible, is there a hook so that once the user finishes placing their selection, I can update the selection and move the caret to just before this piece of the text.
Thanks.
You can have a UILabel and UITextField constrained via autolayout next to each other, the UILabel containing the denumerator and the UITextField the numerator.
Here is an answered question for how to do that: Using Auto Layout to have UILabel and UITextField next to each other (the essence is, you need to adjust the content hugging priority of the UITextField to make it always as wide as the contained text.
If your text field gets too small to be tapped, you can apply the code of this answer to a UITextView and make the tappable area of your view bigger: UIButton: Making the hit area larger than the default hit area (but better use method swizzling than overriding a method in a category like in the answer! Or a subclass.)
How can I add a button in textview? for example..
textView.text=#"text here\n\n text also here\n\n text here aswell";
How can I add a button after each "\n\n" or along the text?
You can create a object of UIButton and add it as subview to yout textview.
You need to preset the frame of the button So the text must be statis and you must know the position of the button in advance.
so your \n\n creates a paragraphs
You can do it without static text just split your text in separate textviews by \n\n, add them to your scrollview. You have to measure your text paragraphs height to set the right frame for textviews and leave gaps for buttons. I would rather use tableview so you dont have to worry about that. Add buttons in between, it is doable.
you may reconsider adding buttons along the text this way (you can but it is really convoluted way of doing things), use webview for that as already stated here by others