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