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.
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.
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.
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.
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.
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.