Why I cant programmatically change the width of UITextField? - ios

I am trying to programmatically change the width of a UITextField using the following code:
myText.frame = CGRectMake(myText.frame.origin.x, myText.frame.origin.y, 280 ,myText.frame.size.height);
The code above is executed on a "touch up inside" button action.
myText has its border style property set to UITextBorderStyleNone
Any ideas are welcome!

if you are trying to set frame programmatically just disable the autolayout for your .xib

My first thought is that either you never linked the IBAction to the UIButton, or there is no IBOutlet pointing to the UITextField.
You can verify that the IBAction is getting called on button press simply by adding an NSLog statement to it, or you can check that both are linked from the "Connections Inspector" in Interface Builder.
Side note Since your text field is set to have no border, it is possible that you just can't tell it is growing. To verify, I would trying setting the text field's background color to something obvious.

Related

Why is type always UIView when I drag and drop UIButton or UILabel to ViewController.swift file

Below is the screenshot. This is the problem I face when I drag and drop either a UILabel or UIButton.
Please help me I am a beginner and I have just started. I just added a button and a label and want to change text in label upon button click but I am facing this problem.
When dragging from the storyboard to the class file to create an IBOutlet, if the containing view is selected in the navigator (as shown in your screenshot), then that will be linked to the outlet.
To make sure you're connecting to the correct object either
Select it first by clicking on it, either in the navigator or on the storyboard itself
or
Drag directly from the navigator
Seems you are trying to create an outlet for Label Message View not Label that why its always UIView

Make multiline UILabel wrap a button

I am using storyboard to design my UIView. But I've stuck at the task: I have a multiline UILabel, UIView and UIButton. I want to make UILabel to wrap my button - the fist line of the text has a trailing constraint to UIButton another one to it's super view. And if my UILabel has no text I got a view at the bottom of label and I need to make this view trailing constraint to UIButton but if I got a free space - to it's superview.
Screenshot example:
I want to jump second line word after 'pyat' . Sorry for my poor english, hope that picture could help to explain my question.
Is it possible to make it directly in IB?
Perhaps instead of using UILabel you can try UITextView by using its textView.textContainer.exclusionPaths property to define a button container area to exclude.
Have a look at the sample code with a reported issue for selected and editable case.
As I remember you must limit the app deployment target minimum to 7 or later if using this property.
Hope that helps!
What you want to do is not supported out of the box and will take a lot of work because it will require subclassing UILabel and overriding drawRect.
An easy solution is to set attributed text on the label and use that to format the specific word to look like a button. UILabel attributed text supports hyperlink like behavior.

Swift: UIButton on UILabel

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.

add inset to textfield created in storyboard?

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.

How to toggle a UILabel between editable and non-editable

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"

Resources