add button in textview -xcode - ios

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

Related

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.

Adding UITextField inline UILabel (Fill in the blanks)

I am trying to achieve something like this for iOS in Objective C.
The fill in the blanks(UITextField) should be inline, and should be able to have its own inputType.
Also, each View is a type of cell.contentView of a UITableViewCell.
My current approach is to find the length of string and also calculate the wrapping content length to the next line. Calculate the x's and y's for UITextField and add another UILabel after the UTextField
Is there any other approach other than this?
As EmilioPelaez says, this is not exactly an answer to your question, but a suggestion:
You can use a collection view with an horizontal flow for each "sequence" (i.e. UILabel-UItextfield-etc...)
That collection view has 2 kind of cell:
One with a uilabel with the number of line set to "1"
and the correct layout to fit the cell.
Another with a uitextfield and the correct layout
Coupled with:
My current approach is to find the length of string and also calculate the wrapping content length to the next line.
You may be able to easily adjust the width of the different cells, hide a uitextfield (if needed) and display a more dynamic "sequence" (if needed)
This is not exactly an answer to your question, instead it's a suggestion for a different interaction.
I think that instead of using inline textFields, you could use a UILabel with an attributed string, and in where the textFields would be, you add a different character with a different color that you can tap (For example, this character ✚).
When tapped, you can show an overlay with a text input, and once that input is completed, you update the label with the text (still tappable, and with a different color).
I think this answer might also be relevant: Detecting taps on attributed text in a UITextView in iOS
I think your solution of separating the UILabels and calculating their required positions is a good one for versions lower than iOS9, but if you can count on iOS, UIStackView can dramatically simplify the process for you. You can read more about the process in this tutorial:
UIStackView Tutorial
Good luck!

Making some parts of a `UITextField` un-selectable

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

iOS: Combine two UIButtons to one UIButton

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 do I have padding at the bottom of my UITextView like in Mail.app so the text isn't scrunched?

In Mail.app, if I go to create a new message and type all the way so the text is at the last line of the UITextView there is still a decent amount of space between the last line of text and the keyboard if you scroll down. But you can't type here. It's just nice padding so you can read it without it being scrunched against the keyboard, and if you select the text, the selection handles don't extend under the keyboard.
My question is, how do I mimic this with a traditional UITextView?
I think you can do this by setting the contentInset property of your UITextView:
myTextView.contentInset = UIEdgeInsetsMake(-4,-8,0,0);
btw... the values above are not specific to your situation. You will want to adjust these to get the inset you are looking for.

Resources